Fix #13904: narrow scope on version_compare( PHP_VERSION, '8.4', '<' )#4950
Open
phpstan-bot wants to merge 2 commits into2.1.xfrom
Open
Fix #13904: narrow scope on version_compare( PHP_VERSION, '8.4', '<' )#4950phpstan-bot wants to merge 2 commits into2.1.xfrom
version_compare( PHP_VERSION, '8.4', '<' )#4950phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
When version_compare(PHP_VERSION, '8.4', '<') or similar calls are used in conditions, PHPStan now narrows the PHP_VERSION_ID type in the corresponding scope branches, matching the existing behavior of direct PHP_VERSION_ID comparisons. Supports all three-argument operators (<, <=, >, >=, lt, le, gt, ge, ==, =, eq, !=, <>, ne), the two-argument form compared with ===, !==, <, <=, >=, >, and PHP_VERSION in either argument position. Closes phpstan/phpstan#13904
Closes phpstan/phpstan#13904 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Closes phpstan/phpstan#13904
This PR adds PHP version scope narrowing when
version_compare()is used withPHP_VERSIONin conditions:version_compare(PHP_VERSION, '8.4', '<')now narrowsPHP_VERSION_IDin both the truthy and falsey branches, supporting all operators (<,<=,>,>=,==,!=and their aliaseslt,le,gt,ge,eq,ne)version_compare(PHP_VERSION, '8.0') === 1,version_compare(PHP_VERSION, '8.0') < 0, etc. narrowPHP_VERSION_IDbased on the comparison resultversion_compare('8.0', PHP_VERSION, '<')is handled by reversing the comparison directionversion_compare(PHP_VERSION, '8.4.1', '>=')correctly narrows toint<80401, max>Implementation
VersionCompareFunctionTypeSpecifyingExtensionhandles the three-argument form by converting the version string to aPHP_VERSION_IDinteger and building a synthetic comparison expression that is processed by the existingTypeSpecifiernarrowing logicTypeSpecifier(in both theSmaller/SmallerOrEqualsection andresolveNormalizedIdentical) by mapping the comparison result set toPHP_VERSION_IDtype constraintsTest plan
tests/PHPStan/Analyser/nsrt/version-compare-scope-narrowing.phpcovers all formsversion-compare-php8.phptest continues to pass