|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Node\Property; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Expr\Variable; |
| 10 | +use PhpParser\NodeFinder; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Node\ClassMethod; |
| 13 | +use PHPStan\Reflection\MethodReflection; |
| 14 | +use function in_array; |
| 15 | +use function is_string; |
| 16 | +use function spl_object_id; |
| 17 | +use function strtolower; |
| 18 | + |
| 19 | +/** |
| 20 | + * Decides whether binding a property by reference (`$this->prop = &$expr`) |
| 21 | + * lets its value be read through the other side of the reference. |
| 22 | + * |
| 23 | + * Called from the native ClassStatementsGatherer in turbo-ext as well. |
| 24 | + */ |
| 25 | +final class PropertyReferenceBinding |
| 26 | +{ |
| 27 | + |
| 28 | + private const VARIABLE_READING_FUNCTIONS = ['compact', 'get_defined_vars']; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param array<ClassMethod> $methods methods gathered so far in the class |
| 32 | + */ |
| 33 | + public static function isReadThroughReference(Expr $referencedExpr, Scope $scope, array $methods): bool |
| 34 | + { |
| 35 | + if (!$referencedExpr instanceof Variable || !is_string($referencedExpr->name)) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + if ($scope->isInAnonymousFunction()) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + $function = $scope->getFunction(); |
| 43 | + if (!$function instanceof MethodReflection) { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + $methodName = strtolower($function->getName()); |
| 48 | + $methodNode = null; |
| 49 | + foreach ($methods as $method) { |
| 50 | + if ($method->getNode()->name->toLowerString() !== $methodName) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + $methodNode = $method->getNode(); |
| 54 | + } |
| 55 | + if ($methodNode === null || $methodNode->stmts === null) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + $variableName = $referencedExpr->name; |
| 60 | + foreach ($methodNode->params as $param) { |
| 61 | + if (!$param->var instanceof Variable || $param->var->name !== $variableName) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // the caller holds the other side of a by-ref parameter |
| 66 | + return $param->byRef; |
| 67 | + } |
| 68 | + |
| 69 | + // a local variable keeps the value alive only while something else in the method uses it |
| 70 | + // (global/static declarations, closure uses, further reads, passing it on, ...) |
| 71 | + $overwrittenVariables = []; |
| 72 | + $nodeFinder = new NodeFinder(); |
| 73 | + foreach ($nodeFinder->findInstanceOf($methodNode->stmts, Assign::class) as $assign) { |
| 74 | + $overwrittenVariables[spl_object_id($assign->var)] = true; |
| 75 | + } |
| 76 | + |
| 77 | + return $nodeFinder->findFirst($methodNode->stmts, static function (Node $node) use ($referencedExpr, $variableName, $overwrittenVariables): bool { |
| 78 | + if ($node instanceof Variable) { |
| 79 | + if (!is_string($node->name)) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + return $node->name === $variableName |
| 84 | + && $node !== $referencedExpr |
| 85 | + && !isset($overwrittenVariables[spl_object_id($node)]); |
| 86 | + } |
| 87 | + |
| 88 | + return $node instanceof FuncCall |
| 89 | + && $node->name instanceof Node\Name |
| 90 | + && in_array($node->name->toLowerString(), self::VARIABLE_READING_FUNCTIONS, true); |
| 91 | + }) !== null; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments