22
33namespace PHPStan \Rules \PHPUnit ;
44
5- use Composer \Semver \Constraint \ConstraintInterface ;
6- use Composer \Semver \VersionParser ;
75use PharIo \Version \UnsupportedVersionConstraintException ;
86use PharIo \Version \Version ;
9- use PharIo \Version \VersionConstraint ;
107use PharIo \Version \VersionConstraintParser ;
118use PhpParser \Node ;
129use PHPStan \Analyser \Scope ;
1512use PHPStan \Rules \Rule ;
1613use PHPStan \Rules \RuleErrorBuilder ;
1714use PHPUnit \Framework \TestCase ;
18- use UnexpectedValueException ;
1915use function count ;
2016use function is_numeric ;
2117use function method_exists ;
2218use function preg_match ;
2319use function sprintf ;
20+ use function version_compare ;
2421
2522/**
2623 * @implements Rule<InClassMethodNode>
2724 */
2825class AttributeRequiresPhpVersionRule implements Rule
2926{
30- private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]* \r?$/m " ;
3127
28+ private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]* \r?$/m " ;
3229
3330 private Version $ phpstanPhpVersion ;
3431
@@ -95,13 +92,7 @@ public function processNode(Node $node, Scope $scope): array
9592 continue ;
9693 }
9794 } catch (UnsupportedVersionConstraintException $ e ) {
98- if (preg_match (self ::VERSION_COMPARISON , $ args [0 ], $ matches ) > 0 ) {
99- $ operator = $ matches ['operator ' ] !== '' ? $ matches ['operator ' ] : '>= ' ;
100-
101- if (version_compare ($ this ->phpstanPhpVersion ->getVersionString (), $ matches ['version ' ], $ operator )) {
102- continue ;
103- }
104- } else {
95+ if (preg_match (self ::VERSION_COMPARISON , $ args [0 ], $ matches ) <= 0 ) {
10596 $ errors [] = RuleErrorBuilder::message (
10697 sprintf ($ e ->getMessage ()),
10798 )
@@ -110,6 +101,12 @@ public function processNode(Node $node, Scope $scope): array
110101
111102 continue ;
112103 }
104+
105+ $ operator = $ matches ['operator ' ] !== '' ? $ matches ['operator ' ] : '>= ' ;
106+
107+ if (version_compare ($ this ->phpstanPhpVersion ->getVersionString (), $ matches ['version ' ], $ operator )) {
108+ continue ;
109+ }
113110 }
114111
115112 $ errors [] = RuleErrorBuilder::message (
0 commit comments