|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Build; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use function array_key_exists; |
| 11 | +use function in_array; |
| 12 | +use function sprintf; |
| 13 | +use function str_starts_with; |
| 14 | + |
| 15 | +/** |
| 16 | + * The single-pass engine must not price expressions through Scope::getType() |
| 17 | + * and friends - every walked node's type lives in its ExpressionResult |
| 18 | + * (threaded via ArgsResult, storage, or gathered statement scopes). A scope |
| 19 | + * read in engine code either re-walks a node on demand or silently diverges |
| 20 | + * from the stored result. The allowlist below names the documented seams |
| 21 | + * (rule-facing bridge asks and ask-ahead-of-walk reads). |
| 22 | + * |
| 23 | + * @implements Rule<MethodCall> |
| 24 | + */ |
| 25 | +final class NoScopeTypeReadInEngineRule implements Rule |
| 26 | +{ |
| 27 | + |
| 28 | + private const ENGINE_NAMESPACES = [ |
| 29 | + 'PHPStan\\Analyser\\NodeScopeResolver', |
| 30 | + 'PHPStan\\Analyser\\StmtHandler\\', |
| 31 | + 'PHPStan\\Analyser\\ExprHandler\\', |
| 32 | + ]; |
| 33 | + |
| 34 | + private const BANNED_METHODS = ['getType', 'getNativeType', 'getKeepVoidType']; |
| 35 | + |
| 36 | + private const ALLOWED_SEAMS = [ |
| 37 | + // rule-facing bridge asks: the scope carries no storage |
| 38 | + 'PHPStan\\Analyser\\ExprHandler\\Helper\\ClosureTypeResolver' => ['readExprType', 'resolveArrowFunctionReturnType'], |
| 39 | + 'PHPStan\\Analyser\\ExprHandler\\Helper\\CountNarrowingHelper' => ['isNormalCountCall'], |
| 40 | + // immediately invoked closures: the invocation args are walked after the closure |
| 41 | + 'PHPStan\\Analyser\\ExprHandler\\Helper\\ClosureTypeResolver#buildParametersAndAcceptors' => [], |
| 42 | + ]; |
| 43 | + |
| 44 | + public function getNodeType(): string |
| 45 | + { |
| 46 | + return MethodCall::class; |
| 47 | + } |
| 48 | + |
| 49 | + public function processNode(Node $node, Scope $scope): array |
| 50 | + { |
| 51 | + if (!$node->name instanceof Node\Identifier) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + $methodName = $node->name->toString(); |
| 55 | + if (!in_array($methodName, self::BANNED_METHODS, true)) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + $currentClassReflection = $scope->getClassReflection(); |
| 60 | + if ($currentClassReflection === null) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + $namespace = $currentClassReflection->getName(); |
| 64 | + $inEngine = false; |
| 65 | + foreach (self::ENGINE_NAMESPACES as $prefix) { |
| 66 | + if ($namespace === $prefix || str_starts_with($namespace, $prefix)) { |
| 67 | + $inEngine = true; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + if (!$inEngine) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + $calledOnType = $scope->getType($node->var); |
| 76 | + if ($calledOnType->getObjectClassNames() === []) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + $isScope = false; |
| 80 | + foreach ($calledOnType->getObjectClassReflections() as $classReflection) { |
| 81 | + if ($classReflection->is(Scope::class)) { |
| 82 | + $isScope = true; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + if (!$isScope) { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + $function = $scope->getFunction(); |
| 91 | + $functionName = $function !== null ? $function->getName() : null; |
| 92 | + if ( |
| 93 | + array_key_exists($namespace, self::ALLOWED_SEAMS) |
| 94 | + && $functionName !== null |
| 95 | + && in_array($functionName, self::ALLOWED_SEAMS[$namespace], true) |
| 96 | + ) { |
| 97 | + return []; |
| 98 | + } |
| 99 | + if ($functionName !== null && array_key_exists($namespace . '#' . $functionName, self::ALLOWED_SEAMS)) { |
| 100 | + return []; |
| 101 | + } |
| 102 | + |
| 103 | + return [ |
| 104 | + RuleErrorBuilder::message(sprintf( |
| 105 | + 'Engine code must not price expressions via Scope::%s() - read the expression\'s stored ExpressionResult (ArgsResult, storage, gathered scopes) instead.', |
| 106 | + $methodName, |
| 107 | + ))->identifier('phpstanBuild.scopeTypeReadInEngine')->build(), |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments