55use PhpParser \Node \Name ;
66use PHPStan \DependencyInjection \AutowiredService ;
77use PHPStan \DependencyInjection \Container ;
8- use PHPStan \Php \ComposerPhpVersionFactory ;
9- use PHPStan \Php \PhpVersion ;
8+ use PHPStan \Php \ConfiguredPhpVersionRangeHelper ;
109use PHPStan \PhpDoc \TypeStringResolver ;
1110use PHPStan \Reflection \NamespaceAnswerer ;
1211use PHPStan \Reflection \ReflectionProvider ;
1312use PHPStan \Reflection \ReflectionProvider \ReflectionProviderProvider ;
14- use PHPStan \ShouldNotHappenException ;
1513use PHPStan \Type \Accessory \AccessoryNonFalsyStringType ;
1614use PHPStan \Type \ArrayType ;
1715use PHPStan \Type \Constant \ConstantArrayType ;
2826use PHPStan \Type \UnionType ;
2927use function array_key_exists ;
3028use function in_array ;
31- use function is_array ;
32- use function is_int ;
3329use function max ;
3430use function sprintf ;
3531use 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 )) {
0 commit comments