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 function-call on a separate line with possibly pure function #3020

Merged
merged 4 commits into from
Apr 17, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Apr 17, 2024

similar to 281a87d

public function processNode(Node $node, Scope $scope)
{
$function = $node->getFunctionReflection();
if (!$function->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.

We should also eliminate void function here.

Copy link
Member

Choose a reason for hiding this comment

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

Thinking about it, we should simply ask about hasSideEffects().

return null;
}

$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
Copy link
Member

Choose a reason for hiding this comment

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

We should also have this condition:

if (!$function->hasSideEffects()->maybe() {
    return null;
}

Because:

  1. If it's no() it's already covered by CallToFunctionStatementWithoutSideEffectsRule
  2. if it's yes() then it's a valid call and should not be reported in this rule.

The distinction between hasSideEffects() and isPure essentially boils down to function being void and function being marked as @phpstan-pure and @phpstan-impure.

Here we're dealing with the uncertainty so we need both of these to be maybe().

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

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit a976987 into phpstan:1.11.x Apr 17, 2024
444 of 446 checks passed
@ondrejmirtes
Copy link
Member

Awesome, thank you!

@staabm
Copy link
Contributor Author

staabm commented Apr 17, 2024

Cool. Will do method call variants tomorrow

@ondrejmirtes
Copy link
Member

Needed to fix this: 57b0ae0

If we looked at issue-bot it would tell us, but no worries :)

@ondrejmirtes
Copy link
Member

This is a false positive: https://phpstan.org/r/eac46a37-248f-41a1-88c8-510241e06b6b

Because it works: https://3v4l.org/5XkR6

Please fix it before moving forward, thanks :)

@staabm staabm deleted the func branch April 17, 2024 20:31
@ondrejmirtes
Copy link
Member

Oh god, we don't have to care about that. It's some kind of weird array loophole. This isn't supported on PHP since 5.4.

@staabm
Copy link
Contributor Author

staabm commented Apr 18, 2024

To make sure we are on the same page: there is no more open todo and I can start with methods node-type?

@ondrejmirtes
Copy link
Member

Yes 😊

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