77use PharIo \Version \VersionConstraintParser ;
88use PHPStan \Analyser \Scope ;
99use PHPStan \BetterReflection \Reflection \ReflectionAttribute ;
10+ use PHPStan \Php \ConfiguredPhpVersionRangeHelper ;
1011use PHPStan \Php \PhpMinorVersionIterator ;
11- use PHPStan \Php \PhpVersion ;
1212use PHPStan \Rules \IdentifierRuleError ;
1313use PHPStan \Rules \RuleErrorBuilder ;
14- use PHPStan \Type \Constant \ConstantIntegerType ;
15- use PHPStan \Type \IntegerRangeType ;
1614use function count ;
1715use function is_numeric ;
1816use function preg_match ;
1917use function sprintf ;
20- use function strpos ;
2118use function substr_count ;
2219use function version_compare ;
2320
@@ -28,8 +25,6 @@ final class AttributeVersionRequirementHelper
2825
2926 private PHPUnitVersion $ PHPUnitVersion ;
3027
31- private PhpVersion $ fallbackPhpVersion ;
32-
3328 /**
3429 * When phpstan-deprecation-rules is installed, rule reports deprecated usages.
3530 */
@@ -42,19 +37,21 @@ final class AttributeVersionRequirementHelper
4237
4338 private bool $ bleedingEdge ;
4439
40+ private ConfiguredPhpVersionRangeHelper $ phpVersionRangeHelper ;
41+
4542 public function __construct (
4643 PHPUnitVersion $ PHPUnitVersion ,
47- PhpVersion $ phpVersion ,
44+ ConfiguredPhpVersionRangeHelper $ phpVersionRangeHelper ,
4845 bool $ deprecationRulesInstalled = false ,
4946 bool $ bleedingEdge = false ,
5047 bool $ warnAboutIncompleteVersion = true
5148 )
5249 {
5350 $ this ->PHPUnitVersion = $ PHPUnitVersion ;
5451 $ this ->deprecationRulesInstalled = $ deprecationRulesInstalled ;
55- $ this ->fallbackPhpVersion = $ phpVersion ;
5652 $ this ->warnAboutIncompleteVersion = $ warnAboutIncompleteVersion ;
5753 $ this ->bleedingEdge = $ bleedingEdge ;
54+ $ this ->phpVersionRangeHelper = $ phpVersionRangeHelper ;
5855 }
5956
6057 /**
@@ -64,7 +61,7 @@ public function __construct(
6461 */
6562 public function checkVersionRequirement (array $ attributes , Scope $ scope ): array
6663 {
67- $ phpstanPharIoVersions = $ this ->getAnalyzedPhpVersions ($ scope );
64+ $ phpstanPharIoVersions = $ this ->getAnalyzedPhpVersions ();
6865 if ($ phpstanPharIoVersions === []) {
6966 return [];
7067 }
@@ -97,18 +94,11 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
9794 continue ;
9895 }
9996
100- $ pharIoVersions = strpos ($ attr ->getName (), 'RequiresPhpunit ' ) !== false
101- ? $ this ->PHPUnitVersion ->getPharIoVersions ()
102- : $ phpstanPharIoVersions ;
103- if ($ pharIoVersions === []) {
104- continue ;
105- }
106-
10797 try {
10898 // check composer like version constraints, e.g. ^1 or ~2
10999 $ testPhpVersionConstraint = $ parser ->parse ($ versionRequirement );
110100
111- foreach ($ pharIoVersions as $ pharIoVersion ) {
101+ foreach ($ phpstanPharIoVersions as $ pharIoVersion ) {
112102 if ($ testPhpVersionConstraint ->complies ($ pharIoVersion )) {
113103 // one of the versions within range matched, check next attribute
114104 continue 2 ;
@@ -128,7 +118,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
128118
129119 $ operator = $ matches ['operator ' ] !== '' ? $ matches ['operator ' ] : '>= ' ;
130120
131- foreach ($ pharIoVersions as $ pharIoVersion ) {
121+ foreach ($ phpstanPharIoVersions as $ pharIoVersion ) {
132122 if (version_compare ($ pharIoVersion ->getVersionString (), $ matches ['version ' ], $ operator )) {
133123 // one of the versions within range matched, check next attribute
134124 continue 2 ;
@@ -168,29 +158,22 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
168158 /**
169159 * @return Version[]
170160 */
171- private function getAnalyzedPhpVersions (Scope $ scope ): array
161+ private function getAnalyzedPhpVersions (): array
172162 {
173- $ scopePhpVersion = $ scope ->getPhpVersion ()->getType ();
174- if ($ scopePhpVersion instanceof ConstantIntegerType) {
175- $ v = new PhpVersion ($ scopePhpVersion ->getValue ());
176- return [new Version ($ v ->getVersionString ())];
177- } elseif ($ scopePhpVersion instanceof IntegerRangeType) {
178- if ($ scopePhpVersion ->getMin () === null || $ scopePhpVersion ->getMax () === null ) {
179- return [];
180- }
181-
163+ [$ minVersion , $ maxVersion ] = $ this ->phpVersionRangeHelper ->getVersionRange ();
164+ if ($ minVersion !== null && $ maxVersion !== null ) {
182165 $ versions = [];
183166 $ minorVersionIterator = new PhpMinorVersionIterator (
184- new PhpVersion ( $ scopePhpVersion -> getMin ()) ,
185- new PhpVersion ( $ scopePhpVersion -> getMax ()) ,
167+ $ minVersion ,
168+ $ maxVersion ,
186169 );
187170 foreach ($ minorVersionIterator as $ phpstanVersion ) {
188171 $ versions [] = new Version ($ phpstanVersion ->getVersionString ());
189172 }
190173 return $ versions ;
191174 }
192175
193- return [new Version ( $ this -> fallbackPhpVersion -> getVersionString ()) ];
176+ return [];
194177 }
195178
196179 // see https://github.com/sebastianbergmann/phpunit/issues/6451
0 commit comments