Skip to content

Do not report function_exists as always-true conditions#5739

Merged
staabm merged 7 commits into
phpstan:2.1.xfrom
staabm:fix23
May 24, 2026
Merged

Do not report function_exists as always-true conditions#5739
staabm merged 7 commits into
phpstan:2.1.xfrom
staabm:fix23

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented May 23, 2026

Closes phpstan/phpstan#8980

extracted from #5737 which went too far

@staabm staabm requested a review from VincentLanglet May 23, 2026 11:41
Copy link
Copy Markdown
Contributor Author

@staabm staabm left a comment

Choose a reason for hiding this comment

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

why did it work before this PR when only a single constant string was given to function_exists?

https://phpstan.org/r/7479cbe6-cdfe-45bf-bada-dab223873fa1

@VincentLanglet
Copy link
Copy Markdown
Contributor

why did it work before this PR when only a single constant string was given to function_exists?

phpstan.org/r/7479cbe6-cdfe-45bf-bada-dab223873fa1

Either

return $expr instanceof FuncCall
&& !$expr->isFirstClassCallable()
&& $expr->name instanceof FullyQualified
&& $expr->name->toLowerString() === 'function_exists'
&& isset($expr->getArgs()[0])
&& count($this->getType($expr->getArgs()[0]->value)->getConstantStrings()) === 1
&& $type->isTrue()->yes();

or
if ($argType instanceof ConstantStringType) {

Should it be simplified/worked ?

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented May 24, 2026

that was a great hint, thank you.

Comment on lines +8 to +13
if (!function_exists($func)) {
throw new \Exception();
}

funcA();
funcB();
Copy link
Copy Markdown
Contributor Author

@staabm staabm May 24, 2026

Choose a reason for hiding this comment

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

in this case, the function_exists() will only assure one of the functions to exist.
therefore - to be defensive - I am using only count($constantStrings) === 1 in the src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php so we still expectedly error about function exists

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be worth adding it as a coment in this file ?

@staabm staabm merged commit d613b54 into phpstan:2.1.x May 24, 2026
654 of 660 checks passed
@staabm staabm deleted the fix23 branch May 24, 2026 11:34
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.

2 participants