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 static method call on a separate line with possibly pure method #3023

Merged
merged 5 commits into from
Apr 18, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Apr 18, 2024

similar to 281a87d

X::impureFunc();
X::callingImpureFunc();

$a = X::myFunc();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not yet find a way to make the rule error on this Expr. the current PossiblyPureStaticCallCollector is based on Expression which this line is not.

Should I implement a separate Collector for Expr?

Copy link
Member

Choose a reason for hiding this comment

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

Why do you think this line should error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it should, because we do a similar error for functions: 57b0ae0

Copy link
Member

Choose a reason for hiding this comment

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

Nope, that was a fix so that this is NOT reported.

@ondrejmirtes
Copy link
Member

Also - the rules are not registered in config

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

This pull request has been marked as ready for review.


$methodName = $node->expr->name->toString();
$calledOnType = $scope->resolveTypeByName($node->expr->class);
$methodReflection = $scope->getMethodReflection($calledOnType, $methodName);
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to go through the type system at all. We have a class name and a method name as strings at hand, so we should go through ReflectionProvider and ClassReflection::getNativeMethod() instead. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

Oh scratch that, this actually avoids some other problems.

@ondrejmirtes ondrejmirtes merged commit df8e1f3 into phpstan:1.11.x Apr 18, 2024
443 of 445 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the static branch April 18, 2024 14:38
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