Fix phpstan/phpstan#14384: Result of function_exists() can't be "cached"#5303
Merged
staabm merged 1 commit intophpstan:2.1.xfrom Mar 26, 2026
Merged
Conversation
…e not narrowing scope - Added FuncCall to the allowed expression types in processSureTypesForConditionalExpressionsAfterAssign and processSureNotTypesForConditionalExpressionsAfterAssign - Previously only Variable, PropertyFetch, and ArrayDimFetch expressions were propagated through variable assignments as ConditionalExpressionHolders - New regression test in tests/PHPStan/Rules/Functions/data/bug-14384.php
staabm
approved these changes
Mar 26, 2026
Contributor
|
I wonder if this wasn't done on purpose. But i have nothing against this bugfix... |
VincentLanglet
approved these changes
Mar 26, 2026
Contributor
|
I believe i recently ran into a similar issue with file_exists |
Contributor
|
@thg2k if you find a problem please reproduce on the playground and file an issue |
Contributor
|
I know the drill :-) too much work, also cause i'm still on 1.12 and didn't have time to check first if it was fixed in 2.x. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When storing the result of
function_exists()in a variable and using that variable as a condition later, PHPStan reportedfunction.notFoundbecause the function existence narrowing was not propagated through the variable assignment.Changes
FuncCallto the allowed expression types inprocessSureTypesForConditionalExpressionsAfterAssign()andprocessSureNotTypesForConditionalExpressionsAfterAssign()insrc/Analyser/ExprHandler/AssignHandler.phptests/PHPStan/Rules/Functions/data/bug-14384.phpRoot cause
FunctionExistsFunctionTypeSpecifyingExtensioncreates a syntheticFuncCallexpression as the target of type narrowing (e.g.,function_exists('some_func')mapped toConstantBooleanType(true)). When assigning$var = function_exists('some_func'), theAssignHandlerprocesses the specified types to createConditionalExpressionHolderobjects that link the variable's truthiness to the narrowed expressions. However, the filter inprocessSureTypesForConditionalExpressionsAfterAssignonly allowedVariable,PropertyFetch, andArrayDimFetchexpressions through, silently droppingFuncCallexpressions. This meant the function existence narrowing was never stored as a conditional expression, soif ($var)didn't know the function existed.The same fix also applies to
class_exists()/interface_exists()/trait_exists()/enum_exists()which use the same pattern with syntheticFuncCallexpressions.Test
Added
tests/PHPStan/Rules/Functions/data/bug-14384.phpwhich storesfunction_exists()result in a variable and calls the function conditionally. The test expects no errors (false positive fix).Fixes phpstan/phpstan#14384