22
33namespace PHPStan \Rules \PHPUnit ;
44
5- use PharIo \Version \UnsupportedVersionConstraintException ;
6- use PharIo \Version \Version ;
7- use PharIo \Version \VersionConstraintParser ;
85use PHPStan \Analyser \Scope ;
96use PHPStan \BetterReflection \Reflection \ReflectionAttribute ;
107use PHPStan \Php \ConfiguredPhpVersionRangeHelper ;
118use PHPStan \Php \PhpMinorVersionIterator ;
129use PHPStan \Rules \IdentifierRuleError ;
1310use PHPStan \Rules \RuleErrorBuilder ;
11+ use SebastianBergmann \VersionRequirement \InvalidVersionRequirementException ;
12+ use SebastianBergmann \VersionRequirement \Requirement ;
1413use function count ;
1514use function is_numeric ;
16- use function preg_match ;
1715use function sprintf ;
1816use function strpos ;
1917use function substr_count ;
20- use function version_compare ;
2118
2219final class AttributeVersionRequirementHelper
2320{
@@ -63,7 +60,6 @@ public function __construct(
6360 public function checkVersionRequirement (array $ attributes , Scope $ scope ): array
6461 {
6562 $ errors = [];
66- $ parser = new VersionConstraintParser ();
6763 foreach ($ attributes as $ attr ) {
6864 $ args = $ attr ->getArguments ();
6965 if (count ($ args ) !== 1 ) {
@@ -90,43 +86,31 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
9086 continue ;
9187 }
9288
93- $ pharIoVersions = strpos ($ attr ->getName (), 'RequiresPhpunit ' ) !== false
94- ? $ this ->PHPUnitVersion ->getPharIoVersions ()
89+ $ versionStrings = strpos ($ attr ->getName (), 'RequiresPhpunit ' ) !== false
90+ ? $ this ->PHPUnitVersion ->getMinMaxVersion ()
9591 : $ this ->getAnalyzedPhpVersions ();
96- if ($ pharIoVersions === []) {
92+ if ($ versionStrings === []) {
9793 continue ;
9894 }
9995
10096 try {
10197 // check composer like version constraints, e.g. ^1 or ~2
102- $ testPhpVersionConstraint = $ parser -> parse ($ versionRequirement );
98+ $ requirement = Requirement :: from ($ versionRequirement );
10399
104- foreach ($ pharIoVersions as $ pharIoVersion ) {
105- if ($ testPhpVersionConstraint -> complies ( $ pharIoVersion )) {
100+ foreach ($ versionStrings as $ versionString ) {
101+ if ($ requirement -> isSatisfiedBy ( $ versionString )) {
106102 // one of the versions within range matched, check next attribute
107103 continue 2 ;
108104 }
109105 }
110- } catch (UnsupportedVersionConstraintException $ e ) {
111- // test php-src builtin operators as in version_compare()
112- if (preg_match (self ::VERSION_COMPARISON , $ versionRequirement , $ matches ) <= 0 ) {
113- $ errors [] = RuleErrorBuilder ::message (
114- sprintf ($ e ->getMessage ()),
115- )
116- ->identifier ('phpunit.attributeRequiresPhpVersion ' )
117- ->build ();
118-
119- continue ;
120- }
121-
122- $ operator = $ matches ['operator ' ] !== '' ? $ matches ['operator ' ] : '>= ' ;
106+ } catch (InvalidVersionRequirementException $ e ) {
107+ $ errors [] = RuleErrorBuilder ::message (
108+ sprintf ($ e ->getMessage ()),
109+ )
110+ ->identifier ('phpunit.attributeRequiresPhpVersion ' )
111+ ->build ();
123112
124- foreach ($ pharIoVersions as $ pharIoVersion ) {
125- if (version_compare ($ pharIoVersion ->getVersionString (), $ matches ['version ' ], $ operator )) {
126- // one of the versions within range matched, check next attribute
127- continue 2 ;
128- }
129- }
113+ continue ;
130114 }
131115
132116 $ errors [] = RuleErrorBuilder ::message (
@@ -159,7 +143,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
159143 }
160144
161145 /**
162- * @return Version[]
146+ * @return list<string>
163147 */
164148 private function getAnalyzedPhpVersions (): array
165149 {
@@ -172,7 +156,7 @@ private function getAnalyzedPhpVersions(): array
172156 $ maxVersion ,
173157 );
174158 foreach ($ minorVersionIterator as $ phpstanVersion ) {
175- $ versions [] = new Version ( $ phpstanVersion ->getVersionString () );
159+ $ versions [] = $ phpstanVersion ->getVersionString ();
176160 }
177161 return $ versions ;
178162 }
0 commit comments