Skip to content

Prevent false positive on method_exists() and function_exists() #2277

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

Draft
wants to merge 2 commits into
base: 1.10.x
Choose a base branch
from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Mar 7, 2023

Pre-existing tests have different expectations regarding the "is always true/false" results, then what was discussed in the linked issue.
I wonder when/how we should/can differentiate whether we expect this "is always true/false" error or not.

I just did the trivial version, so we can see how this violates existing expectations and start discussion/feedback from here.

closes phpstan/phpstan#8980

@staabm
Copy link
Contributor Author

staabm commented Mar 8, 2023

Maybe there should be a config switch to enable/disable this rule for function/method_exists?

@staabm staabm marked this pull request as ready for review March 9, 2023 10:57
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Comment on lines +88 to +98
if ($functionReflection instanceof NativeFunctionReflection) {
// native functions existance depends php configuration,
// therefore don't judge them regarding will always exist or not
return null;
}
Copy link
Contributor Author

@staabm staabm Mar 9, 2023

Choose a reason for hiding this comment

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

let me propose to not judge about "always exist yes/no", when function_exists() is called on a php-src native/php-extension function - these functions might not exist at runtime and code trying to detect such optional dependency should not get flagged IMO.

wdyt?


going one step further I think it might also make sense to prevent the "always exists yes/no" error for method_exists(), when the class involved is located in the composer vendor/ folder.

the assumption would be that the current project might support different versions of a external dependency and therefore needs to feauture detect the existance of methods.

wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ondrejmirtes do you have a opinion on the above comment?

@westonruter
Copy link

Should this also account for property_exists()?

@staabm staabm marked this pull request as draft July 29, 2023 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

function_exists() always evaluate to true.
3 participants