@@ -33,22 +33,28 @@ class AttributeRequiresPhpVersionRule implements Rule
3333 private TestMethodsHelper $ testMethodsHelper ;
3434
3535 /**
36- * When phpstan-deprecation-rules is installed, it reports deprecated usages.
36+ * When phpstan-deprecation-rules is installed, rule reports deprecated usages.
3737 */
3838 private bool $ deprecationRulesInstalled ;
3939
40+ /**
41+ * Whether warnings about incomplete versions are allowed to be emitted
42+ */
43+ private bool $ warnAboutIncompleteVersion ;
44+
4045 public function __construct (
4146 PHPUnitVersion $ PHPUnitVersion ,
4247 TestMethodsHelper $ testMethodsHelper ,
4348 bool $ deprecationRulesInstalled ,
44- PhpVersion $ phpVersion
49+ PhpVersion $ phpVersion ,
50+ bool $ warnAboutIncompleteVersion = true
4551 )
4652 {
4753 $ this ->PHPUnitVersion = $ PHPUnitVersion ;
4854 $ this ->testMethodsHelper = $ testMethodsHelper ;
4955 $ this ->deprecationRulesInstalled = $ deprecationRulesInstalled ;
50-
5156 $ this ->phpstanPhpVersion = new Version ($ phpVersion ->getVersionString ());
57+ $ this ->warnAboutIncompleteVersion = $ warnAboutIncompleteVersion ;
5258 }
5359
5460 public function getNodeType (): string
@@ -80,6 +86,15 @@ public function processNode(Node $node, Scope $scope): array
8086 // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
8187
8288 $ versionRequirement = $ args [0 ];
89+
90+ if ($ this ->warnAboutIncompleteVersion ($ versionRequirement )) {
91+ $ errors [] = RuleErrorBuilder::message (
92+ sprintf ('Version requirement is incomplete. ' ),
93+ )
94+ ->identifier ('phpunit.incompletePhpVersion ' )
95+ ->build ();
96+ }
97+
8398 if (
8499 !is_numeric ($ versionRequirement )
85100 ) {
@@ -139,4 +154,18 @@ public function processNode(Node $node, Scope $scope): array
139154 return $ errors ;
140155 }
141156
157+ // see https://github.com/sebastianbergmann/phpunit/issues/6451
158+ function warnAboutIncompleteVersion (string $ versionRequirement ): bool
159+ {
160+ if (!$ this ->warnAboutIncompleteVersion ) {
161+ return false ;
162+ }
163+
164+ if (!$ this ->PHPUnitVersion ->warnsAboutIncompleteVersion ()->yes ()) {
165+ return false ;
166+ }
167+
168+ return substr_count ($ versionRequirement , '. ' ) !== 2 ;
169+ }
170+
142171}
0 commit comments