Skip to content

Utilize phpVersion.min+max in VersionCompareFunctionDynamicReturnTypeExtension #3631

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

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ jobs:
- script: |
cd e2e/bug-11819
../../bin/phpstan
- script: |
cd e2e/composer-and-phpstan-version-config
composer install --ignore-platform-reqs
../../bin/phpstan analyze test.php --level=0
- script: |
cd e2e/composer-max-version
composer install
Expand Down
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,8 @@ services:

-
class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension
arguments:
configPhpVersion: %phpVersion%
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

Expand Down
2 changes: 2 additions & 0 deletions e2e/composer-and-phpstan-version-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions e2e/composer-and-phpstan-version-config/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php": "^7.0"
}
}
4 changes: 4 additions & 0 deletions e2e/composer-and-phpstan-version-config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
phpVersion:
min: 80103
max: 80304
11 changes: 11 additions & 0 deletions e2e/composer-and-phpstan-version-config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// constraint from composer.json is overruled by phpstan.neon config
\PHPStan\Testing\assertType('int<80103, 80304>', PHP_VERSION_ID);
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
\PHPStan\Testing\assertType('int<1, 3>', PHP_MINOR_VERSION);
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);

\PHPStan\Testing\assertType('1', version_compare(PHP_VERSION, '7.0.0'));
\PHPStan\Testing\assertType('false', version_compare(PHP_VERSION, '7.0.0', '<'));
\PHPStan\Testing\assertType('true', version_compare(PHP_VERSION, '7.0.0', '>'));
30 changes: 23 additions & 7 deletions src/Type/Php/VersionCompareFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\ComposerPhpVersionFactory;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -16,12 +17,19 @@
use PHPStan\Type\TypeCombinator;
use function array_filter;
use function count;
use function is_array;
use function version_compare;

final class VersionCompareFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(private ComposerPhpVersionFactory $composerPhpVersionFactory)
/**
* @param int|array{min: int, max: int}|null $configPhpVersion
*/
public function __construct(
private int|array|null $configPhpVersion,
private ComposerPhpVersionFactory $composerPhpVersionFactory,
)
{
}

Expand Down Expand Up @@ -93,13 +101,21 @@ private function getVersionStrings(Expr $expr, Scope $scope): array
if (
$expr instanceof Expr\ConstFetch
&& $expr->name->toString() === 'PHP_VERSION'
&& $this->composerPhpVersionFactory->getMinVersion() !== null
&& $this->composerPhpVersionFactory->getMaxVersion() !== null
) {
return [
new ConstantStringType($this->composerPhpVersionFactory->getMinVersion()->getVersionString()),
new ConstantStringType($this->composerPhpVersionFactory->getMaxVersion()->getVersionString()),
];
if (is_array($this->configPhpVersion)) {
$minVersion = new PhpVersion($this->configPhpVersion['min']);
$maxVersion = new PhpVersion($this->configPhpVersion['max']);
} else {
$minVersion = $this->composerPhpVersionFactory->getMinVersion();
$maxVersion = $this->composerPhpVersionFactory->getMaxVersion();
}

if ($minVersion !== null && $maxVersion !== null) {
return [
new ConstantStringType($minVersion->getVersionString()),
new ConstantStringType($maxVersion->getVersionString()),
];
}
}

return $scope->getType($expr)->getConstantStrings();
Expand Down
Loading