Skip to content

Commit 9717564

Browse files
authored
Utilize phpVersion.min+max in VersionCompareFunctionDynamicReturnTypeExtension
1 parent 5a132f1 commit 9717564

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

Diff for: .github/workflows/e2e-tests.yml

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ jobs:
296296
- script: |
297297
cd e2e/bug-11819
298298
../../bin/phpstan
299+
- script: |
300+
cd e2e/composer-and-phpstan-version-config
301+
composer install --ignore-platform-reqs
302+
../../bin/phpstan analyze test.php --level=0
299303
- script: |
300304
cd e2e/composer-max-version
301305
composer install

Diff for: conf/config.neon

+2
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,8 @@ services:
16831683

16841684
-
16851685
class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension
1686+
arguments:
1687+
configPhpVersion: %phpVersion%
16861688
tags:
16871689
- phpstan.broker.dynamicFunctionReturnTypeExtension
16881690

Diff for: e2e/composer-and-phpstan-version-config/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"php": "^7.0"
4+
}
5+
}

Diff for: e2e/composer-and-phpstan-version-config/phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
phpVersion:
3+
min: 80103
4+
max: 80304

Diff for: e2e/composer-and-phpstan-version-config/test.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// constraint from composer.json is overruled by phpstan.neon config
4+
\PHPStan\Testing\assertType('int<80103, 80304>', PHP_VERSION_ID);
5+
\PHPStan\Testing\assertType('8', PHP_MAJOR_VERSION);
6+
\PHPStan\Testing\assertType('int<1, 3>', PHP_MINOR_VERSION);
7+
\PHPStan\Testing\assertType('int<0, max>', PHP_RELEASE_VERSION);
8+
9+
\PHPStan\Testing\assertType('1', version_compare(PHP_VERSION, '7.0.0'));
10+
\PHPStan\Testing\assertType('false', version_compare(PHP_VERSION, '7.0.0', '<'));
11+
\PHPStan\Testing\assertType('true', version_compare(PHP_VERSION, '7.0.0', '>'));

Diff for: src/Type/Php/VersionCompareFunctionDynamicReturnTypeExtension.php

+23-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\FuncCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Php\ComposerPhpVersionFactory;
9+
use PHPStan\Php\PhpVersion;
910
use PHPStan\Reflection\FunctionReflection;
1011
use PHPStan\Type\BooleanType;
1112
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -16,12 +17,19 @@
1617
use PHPStan\Type\TypeCombinator;
1718
use function array_filter;
1819
use function count;
20+
use function is_array;
1921
use function version_compare;
2022

2123
final class VersionCompareFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
2224
{
2325

24-
public function __construct(private ComposerPhpVersionFactory $composerPhpVersionFactory)
26+
/**
27+
* @param int|array{min: int, max: int}|null $configPhpVersion
28+
*/
29+
public function __construct(
30+
private int|array|null $configPhpVersion,
31+
private ComposerPhpVersionFactory $composerPhpVersionFactory,
32+
)
2533
{
2634
}
2735

@@ -93,13 +101,21 @@ private function getVersionStrings(Expr $expr, Scope $scope): array
93101
if (
94102
$expr instanceof Expr\ConstFetch
95103
&& $expr->name->toString() === 'PHP_VERSION'
96-
&& $this->composerPhpVersionFactory->getMinVersion() !== null
97-
&& $this->composerPhpVersionFactory->getMaxVersion() !== null
98104
) {
99-
return [
100-
new ConstantStringType($this->composerPhpVersionFactory->getMinVersion()->getVersionString()),
101-
new ConstantStringType($this->composerPhpVersionFactory->getMaxVersion()->getVersionString()),
102-
];
105+
if (is_array($this->configPhpVersion)) {
106+
$minVersion = new PhpVersion($this->configPhpVersion['min']);
107+
$maxVersion = new PhpVersion($this->configPhpVersion['max']);
108+
} else {
109+
$minVersion = $this->composerPhpVersionFactory->getMinVersion();
110+
$maxVersion = $this->composerPhpVersionFactory->getMaxVersion();
111+
}
112+
113+
if ($minVersion !== null && $maxVersion !== null) {
114+
return [
115+
new ConstantStringType($minVersion->getVersionString()),
116+
new ConstantStringType($maxVersion->getVersionString()),
117+
];
118+
}
103119
}
104120

105121
return $scope->getType($expr)->getConstantStrings();

0 commit comments

Comments
 (0)