Skip to content

Commit c1f6ad0

Browse files
authored
Utilize ConfiguredPhpVersionRangeHelper (#6017)
1 parent 4dfa529 commit c1f6ad0

5 files changed

Lines changed: 22 additions & 50 deletions

File tree

‎src/Analyser/ConstantResolver.php‎

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use PhpParser\Node\Name;
66
use PHPStan\DependencyInjection\AutowiredService;
77
use PHPStan\DependencyInjection\Container;
8-
use PHPStan\Php\ComposerPhpVersionFactory;
9-
use PHPStan\Php\PhpVersion;
8+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
109
use PHPStan\PhpDoc\TypeStringResolver;
1110
use PHPStan\Reflection\NamespaceAnswerer;
1211
use PHPStan\Reflection\ReflectionProvider;
1312
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
14-
use PHPStan\ShouldNotHappenException;
1513
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
1614
use PHPStan\Type\ArrayType;
1715
use PHPStan\Type\Constant\ConstantArrayType;
@@ -28,8 +26,6 @@
2826
use PHPStan\Type\UnionType;
2927
use function array_key_exists;
3028
use function in_array;
31-
use function is_array;
32-
use function is_int;
3329
use function max;
3430
use function sprintf;
3531
use const INF;
@@ -50,13 +46,11 @@ final class ConstantResolver
5046

5147
/**
5248
* @param string[] $dynamicConstantNames
53-
* @param int|array{min: int, max: int}|null $phpVersion
5449
*/
5550
public function __construct(
5651
private ReflectionProviderProvider $reflectionProviderProvider,
5752
private array $dynamicConstantNames,
58-
private int|array|null $phpVersion,
59-
private ComposerPhpVersionFactory $composerPhpVersionFactory,
53+
private ConfiguredPhpVersionRangeHelper $configuredPhpVersionRangeHelper,
6054
private ?Container $container,
6155
)
6256
{
@@ -104,8 +98,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
10498
$minPhpVersion = null;
10599
$maxPhpVersion = null;
106100
if (in_array($resolvedConstantName, ['PHP_VERSION_ID', 'PHP_MAJOR_VERSION', 'PHP_MINOR_VERSION', 'PHP_RELEASE_VERSION'], true)) {
107-
$minPhpVersion = $this->getMinPhpVersion();
108-
$maxPhpVersion = $this->getMaxPhpVersion();
101+
[$minPhpVersion, $maxPhpVersion] = $this->configuredPhpVersionRangeHelper->getVersionRange();
109102
}
110103

111104
if ($resolvedConstantName === 'PHP_MAJOR_VERSION') {
@@ -383,40 +376,6 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
383376
return null;
384377
}
385378

386-
private function getMinPhpVersion(): ?PhpVersion
387-
{
388-
if (is_int($this->phpVersion)) {
389-
return null;
390-
}
391-
392-
if (is_array($this->phpVersion)) {
393-
if ($this->phpVersion['max'] < $this->phpVersion['min']) {
394-
throw new ShouldNotHappenException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
395-
}
396-
397-
return new PhpVersion($this->phpVersion['min']);
398-
}
399-
400-
return $this->composerPhpVersionFactory->getMinVersion();
401-
}
402-
403-
private function getMaxPhpVersion(): ?PhpVersion
404-
{
405-
if (is_int($this->phpVersion)) {
406-
return null;
407-
}
408-
409-
if (is_array($this->phpVersion)) {
410-
if ($this->phpVersion['max'] < $this->phpVersion['min']) {
411-
throw new ShouldNotHappenException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
412-
}
413-
414-
return new PhpVersion($this->phpVersion['max']);
415-
}
416-
417-
return $this->composerPhpVersionFactory->getMaxVersion();
418-
}
419-
420379
public function getConfiguredGlobalConstantType(string $constantName): ?Type
421380
{
422381
if (array_key_exists($constantName, $this->configuredTypesCache)) {

‎src/Analyser/ConstantResolverFactory.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\DependencyInjection\AutowiredService;
66
use PHPStan\DependencyInjection\Container;
77
use PHPStan\Php\ComposerPhpVersionFactory;
8+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
89
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
910

1011
#[AutowiredService]
@@ -25,8 +26,10 @@ public function create(): ConstantResolver
2526
return new ConstantResolver(
2627
$this->reflectionProviderProvider,
2728
$this->container->getParameter('dynamicConstantNames'),
28-
$this->container->getParameter('phpVersion'),
29-
$composerFactory,
29+
new ConfiguredPhpVersionRangeHelper(
30+
$this->container->getParameter('phpVersion'),
31+
$composerFactory,
32+
),
3033
$this->container,
3134
);
3235
}

‎src/DependencyInjection/ValidateIgnoredErrorsExtension.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\DependencyInjection\Type\UnaryOperatorTypeSpecifyingExtensionRegistryProvider;
1616
use PHPStan\File\FileExcluder;
1717
use PHPStan\Php\ComposerPhpVersionFactory;
18+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
1819
use PHPStan\Php\PhpVersion;
1920
use PHPStan\PhpDoc\DirectTypeNodeResolverExtensionRegistryProvider;
2021
use PHPStan\PhpDoc\TypeNodeResolver;
@@ -90,7 +91,7 @@ public function loadConfiguration(): void
9091

9192
try {
9293
$composerPhpVersionFactory = new ComposerPhpVersionFactory([]);
93-
$constantResolver = new ConstantResolver($reflectionProviderProvider, [], phpVersion: null, composerPhpVersionFactory: $composerPhpVersionFactory, container: null);
94+
$constantResolver = new ConstantResolver($reflectionProviderProvider, [], new ConfiguredPhpVersionRangeHelper(null, $composerPhpVersionFactory), container: null);
9495

9596
$phpDocParserConfig = new ParserConfig([]);
9697
$ignoredRegexValidator = new IgnoredRegexValidator(

‎src/Php/ConfiguredPhpVersionRangeHelper.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use PHPStan\DependencyInjection\AutowiredParameter;
66
use PHPStan\DependencyInjection\AutowiredService;
7+
use PHPStan\ShouldNotHappenException;
78
use function is_array;
9+
use function is_int;
810

911
#[AutowiredService]
10-
class ConfiguredPhpVersionRangeHelper
12+
final class ConfiguredPhpVersionRangeHelper
1113
{
1214

1315
/**
@@ -29,7 +31,13 @@ public function __construct(
2931
*/
3032
public function getVersionRange(): array
3133
{
32-
if (is_array($this->configPhpVersion)) {
34+
if (is_int($this->configPhpVersion)) {
35+
return [null, null];
36+
} elseif (is_array($this->configPhpVersion)) {
37+
if ($this->configPhpVersion['max'] < $this->configPhpVersion['min']) {
38+
throw new ShouldNotHappenException('Invalid PHP version range: phpVersion.max should be greater or equal to phpVersion.min.');
39+
}
40+
3341
$minVersion = new PhpVersion($this->configPhpVersion['min']);
3442
$maxVersion = new PhpVersion($this->configPhpVersion['max']);
3543
} else {

‎src/Testing/PHPStanTestCase.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Node\Printer\ExprPrinter;
1717
use PHPStan\Parser\Parser;
1818
use PHPStan\Php\ComposerPhpVersionFactory;
19+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
1920
use PHPStan\Php\PhpVersion;
2021
use PHPStan\PhpDoc\TypeNodeResolver;
2122
use PHPStan\PhpDoc\TypeStringResolver;
@@ -95,7 +96,7 @@ public static function createScopeFactory(ReflectionProvider $reflectionProvider
9596

9697
$reflectionProviderProvider = new DirectReflectionProviderProvider($reflectionProvider);
9798
$composerPhpVersionFactory = $container->getByType(ComposerPhpVersionFactory::class);
98-
$constantResolver = new ConstantResolver($reflectionProviderProvider, $dynamicConstantNames, phpVersion: null, composerPhpVersionFactory: $composerPhpVersionFactory, container: $container);
99+
$constantResolver = new ConstantResolver($reflectionProviderProvider, $dynamicConstantNames, new ConfiguredPhpVersionRangeHelper(null, $composerPhpVersionFactory), container: $container);
99100

100101
$initializerExprTypeResolver = new InitializerExprTypeResolver(
101102
$constantResolver,

0 commit comments

Comments
 (0)