Skip to content
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

Detect unused method call on a separate line with possibly pure method #3022

Merged
merged 8 commits into from
Apr 18, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Apr 18, 2024

similar to 281a87d

}

$classType = $scope->getType($node->expr->var);
foreach ($classType->getObjectClassReflections() as $classReflection) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use $scope->getMethodReflection() instead of all of this.

UnionTypeMethodReflection returns $this->methods[0]->getDeclaringClass() as declaring class but we can solve that later. Thanks.

}

$methodReflection = $classReflection->getMethod($methodName, $scope);
if (!$methodReflection->isPure()->maybe()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip if method is not final. Child class (polymorphic call) might have side effects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If method is not final, it's sufficient that $classReflection is final. Also please write test cases for these.

@staabm staabm marked this pull request as ready for review April 18, 2024 09:39
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Apr 18, 2024

I cannot reproduce the stale-result-cache errors locally.

remaining build errors seem unrelated

@ondrejmirtes
Copy link
Member

FYI I added a commit 024c32a - I think these cases can be reported as well

@ondrejmirtes
Copy link
Member

(FYI We won't need to care about finality for StaticCall as we know exactly the class+method we're calling, there's no polymorphism at hand.)

@ondrejmirtes ondrejmirtes merged commit 47f837b into phpstan:1.11.x Apr 18, 2024
15 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the method-calls branch April 18, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants