88use PHPStan \Php \PhpMinorVersionIterator ;
99use PHPStan \Rules \IdentifierRuleError ;
1010use PHPStan \Rules \RuleErrorBuilder ;
11+ use SebastianBergmann \VersionRequirement \InvalidVersionOperatorException ;
1112use SebastianBergmann \VersionRequirement \InvalidVersionRequirementException ;
1213use SebastianBergmann \VersionRequirement \Requirement ;
1314use function count ;
14- use function is_numeric ;
1515use function sprintf ;
1616use function strpos ;
17- use function substr_count ;
1817
1918final class AttributeVersionRequirementHelper
2019{
@@ -66,79 +65,77 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
6665 continue ;
6766 }
6867
69- // the following block is mimicing PHPUnit version parsing
70- // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
71-
7268 $ versionRequirement = $ args [0 ];
7369
74- if ($ this ->warnAboutIncompleteVersion ($ versionRequirement )) {
70+ try {
71+ $ requirement = Requirement ::from ($ versionRequirement );
72+ } catch (InvalidVersionOperatorException $ e ) {
73+ if ($ this ->PHPUnitVersion ->requiresPhpversionAttributeWithOperator ()->yes ()) {
74+ $ errors [] = RuleErrorBuilder ::message (
75+ sprintf ('Version requirement is missing operator. ' ),
76+ )
77+ ->identifier ('phpunit.attributeRequiresPhpVersion ' )
78+ ->build ();
79+ } elseif (
80+ $ this ->deprecationRulesInstalled
81+ && $ this ->PHPUnitVersion ->deprecatesPhpversionAttributeWithoutOperator ()->yes ()
82+ ) {
83+ $ errors [] = RuleErrorBuilder ::message (
84+ sprintf ('Version requirement without operator is deprecated. ' ),
85+ )
86+ ->identifier ('phpunit.attributeRequiresPhpVersion ' )
87+ ->build ();
88+ }
89+
90+ continue ;
91+ } catch (InvalidVersionRequirementException $ e ) {
7592 $ errors [] = RuleErrorBuilder ::message (
76- sprintf (' Version requirement is incomplete. ' ),
93+ sprintf ($ e -> getMessage () ),
7794 )
7895 ->identifier ('phpunit.attributeRequiresPhpVersion ' )
7996 ->build ();
80- }
8197
82- if (
83- !is_numeric ($ versionRequirement )
84- ) {
85- if (!$ this ->bleedingEdge ) {
86- continue ;
87- }
98+ continue ;
99+ }
88100
89- $ versionStrings = strpos ($ attr ->getName (), 'RequiresPhpunit ' ) !== false
90- ? $ this ->PHPUnitVersion ->getMinMaxVersion ()
91- : $ this ->getAnalyzedPhpVersions ();
92- if ($ versionStrings === []) {
101+ if (!$ requirement ->isComplete ()) {
102+ if (!$ this ->warnAboutIncompleteVersion ) {
93103 continue ;
94104 }
95105
96- try {
97- // check composer like version constraints, e.g. ^1 or ~2
98- $ requirement = Requirement ::from ($ versionRequirement );
99-
100- foreach ($ versionStrings as $ versionString ) {
101- if ($ requirement ->isSatisfiedBy ($ versionString )) {
102- // one of the versions within range matched, check next attribute
103- continue 2 ;
104- }
105- }
106- } catch (InvalidVersionRequirementException $ e ) {
107- $ errors [] = RuleErrorBuilder ::message (
108- sprintf ($ e ->getMessage ()),
109- )
110- ->identifier ('phpunit.attributeRequiresPhpVersion ' )
111- ->build ();
112-
106+ if (!$ this ->PHPUnitVersion ->warnsAboutIncompleteVersion ()->yes ()) {
113107 continue ;
114108 }
115109
116110 $ errors [] = RuleErrorBuilder ::message (
117- sprintf ('Version requirement will always evaluate to false . ' ),
111+ sprintf ('Version requirement is incomplete . ' ),
118112 )
119113 ->identifier ('phpunit.attributeRequiresPhpVersion ' )
120114 ->build ();
121115
122116 continue ;
123117 }
124118
125- if ($ this ->PHPUnitVersion ->requiresPhpversionAttributeWithOperator ()->yes ()) {
126- $ errors [] = RuleErrorBuilder ::message (
127- sprintf ('Version requirement is missing operator. ' ),
128- )
129- ->identifier ('phpunit.attributeRequiresPhpVersion ' )
130- ->build ();
131- } elseif (
132- $ this ->deprecationRulesInstalled
133- && $ this ->PHPUnitVersion ->deprecatesPhpversionAttributeWithoutOperator ()->yes ()
134- ) {
135- $ errors [] = RuleErrorBuilder ::message (
136- sprintf ('Version requirement without operator is deprecated. ' ),
137- )
138- ->identifier ('phpunit.attributeRequiresPhpVersion ' )
139- ->build ();
119+ $ versionStrings = strpos ($ attr ->getName (), 'RequiresPhpunit ' ) !== false
120+ ? $ this ->PHPUnitVersion ->getMinMaxVersion ()
121+ : $ this ->getAnalyzedPhpVersions ();
122+ if ($ versionStrings === []) {
123+ continue ;
124+ }
125+ foreach ($ versionStrings as $ versionString ) {
126+ if ($ requirement ->isSatisfiedBy ($ versionString )) {
127+ // one of the versions within range matched, check next attribute
128+ continue 2 ;
129+ }
140130 }
131+
132+ $ errors [] = RuleErrorBuilder ::message (
133+ sprintf ('Version requirement will always evaluate to false. ' ),
134+ )
135+ ->identifier ('phpunit.attributeRequiresPhpVersion ' )
136+ ->build ();
141137 }
138+
142139 return $ errors ;
143140 }
144141
@@ -164,22 +161,4 @@ private function getAnalyzedPhpVersions(): array
164161 return [];
165162 }
166163
167- // see https://github.com/sebastianbergmann/phpunit/issues/6451
168- private function warnAboutIncompleteVersion (string $ versionRequirement ): bool
169- {
170- if (!$ this ->bleedingEdge ) {
171- return false ;
172- }
173-
174- if (!$ this ->warnAboutIncompleteVersion ) {
175- return false ;
176- }
177-
178- if (!$ this ->PHPUnitVersion ->warnsAboutIncompleteVersion ()->yes ()) {
179- return false ;
180- }
181-
182- return substr_count ($ versionRequirement , '. ' ) !== 2 ;
183- }
184-
185164}
0 commit comments