Repository navigation
ClassStatementsGatherer blindly collects all 2-element arrays into methodCalls, causing UnusedPrivateMethodRule to evaluate isCallable() and trigger class reflection for data arrays #6759
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
Open
alfredbez
wants to merge
1
commit into
phpstan:2.3.x
Choose a base branch
from
alfredbez:fix/unused-private-method-gatherer-callable-array
base: 2.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−2
Open
Changes from all commits
Commits
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,80 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Node; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Array_; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\IdentifierRuleError; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<Rule<ClassMethodsNode>> | ||
| */ | ||
| class ClassStatementsGathererTest extends RuleTestCase | ||
| { | ||
|
|
||
| /** | ||
| * @return Rule<ClassMethodsNode> | ||
| */ | ||
| protected function getRule(): Rule | ||
| { | ||
| return new /** @implements Rule<ClassMethodsNode> */ class implements Rule { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return ClassMethodsNode::class; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethodsNode $node | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $errors = []; | ||
| foreach ($node->getMethodCalls() as $methodCall) { | ||
| $callNode = $methodCall->getNode(); | ||
| if (!$callNode instanceof Array_) { | ||
| continue; | ||
| } | ||
|
|
||
| $errors[] = RuleErrorBuilder::message( | ||
| sprintf('Gathered callable array on line %d.', $callNode->getStartLine()), | ||
| )->line($callNode->getStartLine()) | ||
| ->identifier('tests.gatheredCallableArray') | ||
| ->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| public function testGatherCallableArrays(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/class-statements-gatherer-callable-array.php'], [ | ||
| [ | ||
| 'Gathered callable array on line 10.', | ||
| 10, | ||
| ], | ||
| [ | ||
| 'Gathered callable array on line 11.', | ||
| 11, | ||
| ], | ||
| [ | ||
| 'Gathered callable array on line 12.', | ||
| 12, | ||
| ], | ||
| [ | ||
| 'Gathered callable array on line 13.', | ||
| 13, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
tests/PHPStan/Node/data/class-statements-gatherer-callable-array.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,22 @@ | ||
| <?php | ||
|
|
||
| namespace ClassStatementsGathererCallableArray; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function test(): void | ||
| { | ||
| $a = [$this, 'method']; | ||
| $b = [self::class, 'method']; | ||
| $c = [static::class, 'method']; | ||
| $d = [Foo::class, 'method']; | ||
|
|
||
| $x1 = ['SomeClass', 'method']; | ||
| $x2 = [\stdClass::class, 'method']; | ||
| $x3 = ['foo', 'bar']; | ||
| $x4 = [1, 2]; | ||
| [, $x5] = ['foo', 'bar']; | ||
| } | ||
|
|
||
| } | ||
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
43 changes: 43 additions & 0 deletions
43
tests/PHPStan/Rules/DeadCode/data/unused-private-method-callable-arrays.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,43 @@ | ||
| <?php | ||
|
|
||
| namespace UnusedPrivateMethodCallableArrays; | ||
|
|
||
| class BigContainer | ||
| { | ||
|
|
||
| public static function statusKey(): void | ||
| { | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| private static function doSelf(): void | ||
| { | ||
| } | ||
|
|
||
| private static function doStatic(): void | ||
| { | ||
| } | ||
|
|
||
| private static function doClassName(): void | ||
| { | ||
| } | ||
|
|
||
| private static function statusKey(): void | ||
| { | ||
| } | ||
|
|
||
| public function run(): void | ||
| { | ||
| $a = [self::class, 'doSelf']; | ||
| $b = [static::class, 'doStatic']; | ||
| $c = [Foo::class, 'doClassName']; | ||
|
|
||
| $data = [BigContainer::class, 'statusKey']; | ||
| $data2 = ['UnusedPrivateMethodCallableArrays\BigContainer', 'statusKey']; | ||
| } | ||
|
|
||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
doesn't work for
$a = [$this, rand(0, 1) ? 'method' : 'someOther'];or