Repository navigation
Fix "Property is never read, only written" false positive, when initialized by ref #7253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+592
−1
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a40c114
Fix "Property is never read, only written" false positive, when initi…
staabm 39ed1e5
Fix "Property is never read, only written" false positive, when initi…
staabm d522fdb
Fix "Property is never read, only written" false positive, when initi…
staabm 0a9ab22
fix build
staabm 845e12c
fix by array-offset
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Node\Property; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\Assign; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\NodeFinder; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\ClassMethod; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use function in_array; | ||
| use function is_string; | ||
| use function spl_object_id; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * Decides whether binding a property by reference (`$this->prop = &$expr`) | ||
| * lets its value be read through the other side of the reference. | ||
| * | ||
| * Called from the native ClassStatementsGatherer in turbo-ext as well. | ||
| */ | ||
| final class PropertyReferenceBinding | ||
| { | ||
|
|
||
| private const VARIABLE_READING_FUNCTIONS = ['compact', 'get_defined_vars']; | ||
|
|
||
| /** | ||
| * @param array<ClassMethod> $methods methods gathered so far in the class | ||
| */ | ||
| public static function isReadThroughReference(Expr $referencedExpr, Scope $scope, array $methods): bool | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like this AST based node-finder deep seek, but since it only happens for by-ref parameters/promoted-properties, I think it is fine |
||
| { | ||
| if (!$referencedExpr instanceof Variable || !is_string($referencedExpr->name)) { | ||
| return true; | ||
| } | ||
| if ($scope->isInAnonymousFunction()) { | ||
| return true; | ||
| } | ||
|
|
||
| $function = $scope->getFunction(); | ||
| if (!$function instanceof MethodReflection) { | ||
| return true; | ||
| } | ||
|
|
||
| $methodName = strtolower($function->getName()); | ||
| $methodNode = null; | ||
| foreach ($methods as $method) { | ||
| if ($method->getNode()->name->toLowerString() !== $methodName) { | ||
| continue; | ||
| } | ||
| $methodNode = $method->getNode(); | ||
| } | ||
| if ($methodNode === null || $methodNode->stmts === null) { | ||
| return true; | ||
| } | ||
|
|
||
| $variableName = $referencedExpr->name; | ||
| foreach ($methodNode->params as $param) { | ||
| if (!$param->var instanceof Variable || $param->var->name !== $variableName) { | ||
| continue; | ||
| } | ||
|
|
||
| // the caller holds the other side of a by-ref parameter | ||
| return $param->byRef; | ||
| } | ||
|
|
||
| // a local variable keeps the value alive only while something else in the method uses it | ||
| // (global/static declarations, closure uses, further reads, passing it on, ...) | ||
| $overwrittenVariables = []; | ||
| $nodeFinder = new NodeFinder(); | ||
| foreach ($nodeFinder->findInstanceOf($methodNode->stmts, Assign::class) as $assign) { | ||
| $overwrittenVariables[spl_object_id($assign->var)] = true; | ||
| } | ||
|
|
||
| return $nodeFinder->findFirst($methodNode->stmts, static function (Node $node) use ($referencedExpr, $variableName, $overwrittenVariables): bool { | ||
| if ($node instanceof Variable) { | ||
| if (!is_string($node->name)) { | ||
| return true; | ||
| } | ||
|
|
||
| return $node->name === $variableName | ||
| && $node !== $referencedExpr | ||
| && !isset($overwrittenVariables[spl_object_id($node)]); | ||
| } | ||
|
|
||
| return $node instanceof FuncCall | ||
| && $node->name instanceof Node\Name | ||
| && in_array($node->name->toLowerString(), self::VARIABLE_READING_FUNCTIONS, true); | ||
| }) !== null; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/PHPStan/Rules/DeadCode/data/bug-15886-array-offset.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15886ArrayOffset; | ||
|
|
||
| class ArrayOffset | ||
| { | ||
| /** @var array<string, int> */ | ||
| private array $counters = []; | ||
|
|
||
| public function bind(int &$counter): void | ||
| { | ||
| $this->counters['requests'] = &$counter; | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| $this->counters['requests'] = 0; | ||
| } | ||
| } | ||
|
|
||
| class NestedArrayOffset | ||
| { | ||
| /** @var array<string, array<string, int>> */ | ||
| private array $counters = []; | ||
|
|
||
| public function bind(int &$counter): void | ||
| { | ||
| $this->counters['http']['requests'] = &$counter; | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| $this->counters['http']['requests'] = 0; | ||
| } | ||
| } | ||
|
|
||
| class StaticArrayOffset | ||
| { | ||
| /** @var array<string, int> */ | ||
| private static array $counters = []; | ||
|
|
||
| public static function bind(int &$counter): void | ||
| { | ||
| self::$counters['requests'] = &$counter; | ||
| } | ||
|
|
||
| public static function reset(): void | ||
| { | ||
| self::$counters['requests'] = 0; | ||
| } | ||
| } | ||
|
|
||
| class ArrayOffsetThrowawayLocal | ||
| { | ||
| /** @var array<string, int> */ | ||
| private array $counters = []; | ||
|
|
||
| public function bind(): void | ||
| { | ||
| $counter = 0; | ||
| $this->counters['requests'] = &$counter; | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| $this->counters['requests'] = 0; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dunno if there is a better name/place for this service...