-
Notifications
You must be signed in to change notification settings - Fork 552
Narrow PHP_VERSION_ID on version_compare() #4863
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
staabm
wants to merge
14
commits into
phpstan:2.1.x
Choose a base branch
from
staabm:narrowvers
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b196d18
Narrow PHP_VERSION_ID on version_compare()
staabm 14d03b1
cs
staabm fc98ecb
Update MutatingScope.php
staabm 6b6b83e
WIP: Create VersionCompareFunctionTypeSpecifyingExtension.php
staabm 4be0c38
Update VersionCompareFunctionTypeSpecifyingExtension.php
staabm 3ce44dd
Update VersionCompareFunctionTypeSpecifyingExtension.php
staabm 2f7d098
Create narrow-phpversionid-on-version-compare-global-scope.php
staabm cfdc348
Update narrow-phpversionid-on-version-compare.php
staabm 3a58f64
move test into e2e
staabm f0940c1
adjust
staabm 3577566
fix
staabm eaebcaa
cleanup
staabm 9b9df99
Update narrow-phpversionid-on-version-compare.php
staabm 4f03cb8
mv
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /vendor/ | ||
| composer.lock |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "require": { | ||
| "php": "^8" | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
e2e/version-compare-phpversionid/narrow-phpversionid-on-version-compare-global-scope.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| // intentional without namespace | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function narrowPhpVersionIdViaVersionComapre(): void { | ||
| if (PHP_VERSION_ID < 80000) { | ||
| return; | ||
| } | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, max>', $x); | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '<' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80399>', $x); | ||
| } | ||
| } | ||
|
|
164 changes: 164 additions & 0 deletions
164
e2e/version-compare-phpversionid/narrow-phpversionid-on-version-compare.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| <?php | ||
|
|
||
| namespace Bug162; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
| use const PHP_VERSION; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| function lower(): void | ||
| { | ||
| // add a upper bound, so we don't need to adjust | ||
| // the test when PHPStan adds support for PHP8.6+ | ||
| if (PHP_VERSION_ID > 80599) { | ||
| return; | ||
| } | ||
|
|
||
| // lower limit inferred from composer.json | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80599>', $x); | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '<' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80399>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'lt' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80399>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '<=' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80400>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'le' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80400>', $x); | ||
| } | ||
| } | ||
|
|
||
| function greater(): void | ||
| { | ||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '>' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80401, max>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'gt' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80401, max>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '>=' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80400, max>', $x); | ||
| } | ||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'ge' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80400, max>', $x); | ||
| } | ||
| } | ||
|
|
||
| function equal(): void | ||
| { | ||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '=' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('80400', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '==' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('80400', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'eq' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('80400', $x); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function not(): void | ||
| { | ||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '!=' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<min, 80399>|int<80401, max>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '<>' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<min, 80399>|int<80401, max>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', 'ne' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<min, 80399>|int<80401, max>', $x); | ||
| } | ||
| } | ||
|
|
||
| function inverseOperandLower(): void | ||
| { | ||
| if ( | ||
| version_compare( '8.3.12', PHP_VERSION, '<' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<min, 80311>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( '8.3.12', PHP_VERSION, '>=' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80312, max>', $x); | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( '8.3.12', PHP_VERSION, '>' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80313, max>', $x); | ||
| } | ||
| } | ||
|
|
||
| function narrow(): void { | ||
| if (PHP_VERSION_ID < 80000) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| version_compare( PHP_VERSION, '8.4', '<' ) | ||
| ) { | ||
| $x = PHP_VERSION_ID; | ||
| assertType('int<80000, 80399>', $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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Php; | ||
|
|
||
| use Nette\Utils\Strings; | ||
| use function sprintf; | ||
|
|
||
| final class SimplePhpVersionParser | ||
| { | ||
|
|
||
| public static function parseVersion(string $version): ?PhpVersion | ||
| { | ||
| $matches = Strings::match($version, '#^(\d+)\.(\d+)(?:\.(\d+))?#'); | ||
| if ($matches === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $major = $matches[1]; | ||
| $minor = $matches[2] ?? 0; | ||
| $patch = $matches[3] ?? 0; | ||
| $versionId = (int) sprintf('%d%02d%02d', $major, $minor, $patch); | ||
|
|
||
| return new PhpVersion($versionId); | ||
| } | ||
|
|
||
| } |
141 changes: 141 additions & 0 deletions
141
src/Type/Php/VersionCompareFunctionTypeSpecifyingExtension.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\ConstFetch; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Scalar\String_; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Analyser\SpecifiedTypes; | ||
| use PHPStan\Analyser\TypeSpecifier; | ||
| use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
| use PHPStan\Analyser\TypeSpecifierContext; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Php\SimplePhpVersionParser; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
| use PHPStan\Type\IntegerRangeType; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use function count; | ||
| use function in_array; | ||
| use function strtolower; | ||
|
|
||
| #[AutowiredService] | ||
| final class VersionCompareFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
| { | ||
|
|
||
| private TypeSpecifier $typeSpecifier; | ||
|
|
||
| public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
| { | ||
| $this->typeSpecifier = $typeSpecifier; | ||
| } | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool | ||
| { | ||
| return strtolower($functionReflection->getName()) === 'version_compare' && $context->true(); | ||
| } | ||
|
|
||
| public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes | ||
| { | ||
| if (!$node->name instanceof Name) { | ||
| return new SpecifiedTypes(); | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
| if (count($args) < 2) { | ||
| return new SpecifiedTypes(); | ||
| } | ||
|
|
||
| $version1 = $args[0]->value; | ||
| $version2 = $args[1]->value; | ||
|
|
||
| if ( | ||
| $version1 instanceof ConstFetch | ||
| && $version1->name->name === 'PHP_VERSION' | ||
| && $version2 instanceof String_ | ||
| ) { | ||
| $integerVersionRange = $this->getVersionCompareType($version2->value, isset($args[2]) ? $args[2]->value : null, $scope); | ||
|
|
||
| if ($integerVersionRange !== null) { | ||
| $narrowedVersion = TypeCombinator::intersect($scope->getPhpVersion()->getType(), $integerVersionRange); | ||
| return $this->typeSpecifier->create( | ||
| new ConstFetch(new Name('\\PHP_VERSION_ID')), | ||
| $narrowedVersion, | ||
| $context, | ||
| $scope, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| $version2 instanceof ConstFetch | ||
| && $version2->name->name === 'PHP_VERSION' | ||
| && $version1 instanceof String_ | ||
| ) { | ||
| $integerVersionRange = $this->getVersionCompareType($version1->value, isset($args[2]) ? $args[2]->value : null, $scope); | ||
| if ($integerVersionRange !== null) { | ||
| $narrowedVersion = TypeCombinator::intersect($scope->getPhpVersion()->getType(), $integerVersionRange); | ||
| return $this->typeSpecifier->create( | ||
| new ConstFetch(new Name('\\PHP_VERSION_ID')), | ||
| $narrowedVersion, | ||
| $context, | ||
| $scope, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return new SpecifiedTypes(); | ||
| } | ||
|
|
||
| private function getVersionCompareType(string $value, ?Expr $operator, Scope $scope): ?Type | ||
| { | ||
| $parsedVersion = SimplePhpVersionParser::parseVersion($value); | ||
| if ($parsedVersion === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($operator !== null) { | ||
| $operators = $scope->getType($operator)->getConstantStrings(); | ||
| if (count($operators) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $operatorString = $operators[0]->getValue(); | ||
| } else { | ||
| $operatorString = '<'; | ||
| } | ||
|
|
||
| if (!in_array($operatorString, VersionCompareFunctionDynamicReturnTypeExtension::VALID_OPERATORS, true)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (in_array($operatorString, ['<', 'lt'], true)) { | ||
| return IntegerRangeType::fromInterval(null, $parsedVersion->getVersionId() - 1); | ||
| } | ||
| if (in_array($operatorString, ['<=', 'le'], true)) { | ||
| return IntegerRangeType::fromInterval(null, $parsedVersion->getVersionId()); | ||
| } | ||
|
|
||
| if (in_array($operatorString, ['>', 'gt'], true)) { | ||
| return IntegerRangeType::fromInterval($parsedVersion->getVersionId() + 1, null); | ||
| } | ||
| if (in_array($operatorString, ['>=', 'ge'], true)) { | ||
| return IntegerRangeType::fromInterval($parsedVersion->getVersionId(), null); | ||
| } | ||
|
|
||
| if ( | ||
| in_array($operatorString, ['==', '=', 'eq'], true) | ||
| ) { | ||
| return new ConstantIntegerType($parsedVersion->getVersionId()); | ||
| } | ||
|
|
||
| return TypeCombinator::remove(new IntegerType(), new ConstantIntegerType($parsedVersion->getVersionId())); | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel I am hitting a bug here, as depending on how I name the constant here
PHP_VERSION_IDvs.\\PHP_VERSION_IDthe inference does not work as expected(and it seems it depends on whether the code beeing analyzed is namespaced or not)