Report impossible class_exists/interface_exists/trait_exists/enum_exists#5754
Conversation
|
You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x. |
3dc802f to
480fc5b
Compare
95f21da to
d04d50c
Compare
|
This pull request has been marked as ready for review. |
| $argsCount = count($args); | ||
| if ($node->name instanceof Node\Name) { | ||
| $functionName = strtolower((string) $node->name); | ||
| if (in_array($functionName, [ |
There was a problem hiding this comment.
Given the fact this condition is now useless for 200 lines of code, I feel like it would be better moving the onlyReportFalse declaration.
This way we have
if ($node instanceof FuncCall) {
// specific logic which could be moved into a private or a dedicated service
}
$onlyReportFalse = ...;
// genericLogic
Maybe one day the
$onlyReportFalse = ...;
will become a
$onlyReportValue = // true/false/null
WDYT @staabm ?
There was a problem hiding this comment.
I think we might get a more readable result by refactoring this into a separate method, which takes $expr and the to-be-returned ?bool.
the new method can decide based on the $expr whether the ?bool can/should be returned or not.
so
return $rootExprType->getValue(); gets return $this->decideSpecifiedTypeWasFound($expr, $rootExprType->getValue());
and
return $result->maybe() ? null : $result->yes(); gets return $this->decideSpecifiedTypeWasFound($expr, $result->maybe() ? null : $result->yes());
(name of the new method tbd)
There was a problem hiding this comment.
Done in d201db6 — extracted into decideSpecifiedTypeWasFound() (name open to bikeshedding). This drops the $onlyReportFalse flag entirely, so it also covers @VincentLanglet's point about the early declaration.
— Claude
|
please rebase on 2.2.x |
Detect via reflection when the constant-string argument names a struct of the wrong kind (e.g. class_exists() on an interface name) and narrow the argument to never in the truthy context. Drop the ImpossibleCheckTypeHelper bailout for these four functions so the rule can pick up the impossibility, while still suppressing "always true" since runtime autoload can fail. Closes phpstan/phpstan#14683
d9ad04e to
9a08e13
Compare
Replace the early-declared $onlyReportFalse flag with a method that post-processes the candidate result at each return site, deciding based on the expression whether the "always true" result must be suppressed. Co-Authored-By: Claude Code
| return $result; | ||
| } | ||
|
|
||
| private function isAutoloadableExistenceCheck(Expr $node): bool |
There was a problem hiding this comment.
I would inline this function, but otherwise this LGTM
Co-Authored-By: Claude Code
|
Thank you! |
Detect via reflection when the constant-string argument names a struct of the wrong kind (e.g. class_exists() on an interface name) and narrow the argument to never in the truthy context. Drop the ImpossibleCheckTypeHelper bailout for these four functions so the rule can pick up the impossibility, while still suppressing "always true" since runtime autoload can fail.
Closes phpstan/phpstan#14683