-
Notifications
You must be signed in to change notification settings - Fork 51
Sanity check #[RequiresPhp] value and range
#269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,16 @@ | |
|
|
||
| namespace PHPStan\Rules\PHPUnit; | ||
|
|
||
| use Composer\Semver\Constraint\ConstraintInterface; | ||
| use Composer\Semver\VersionParser; | ||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\InClassMethodNode; | ||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPUnit\Framework\TestCase; | ||
| use UnexpectedValueException; | ||
| use function count; | ||
| use function is_numeric; | ||
| use function method_exists; | ||
|
|
@@ -19,6 +23,8 @@ | |
| class AttributeRequiresPhpVersionRule implements Rule | ||
| { | ||
|
|
||
| private ConstraintInterface $phpstanVersionConstraint; | ||
|
|
||
| private PHPUnitVersion $PHPUnitVersion; | ||
|
|
||
| private TestMethodsHelper $testMethodsHelper; | ||
|
|
@@ -31,12 +37,16 @@ class AttributeRequiresPhpVersionRule implements Rule | |
| public function __construct( | ||
| PHPUnitVersion $PHPUnitVersion, | ||
| TestMethodsHelper $testMethodsHelper, | ||
| bool $deprecationRulesInstalled | ||
| bool $deprecationRulesInstalled, | ||
| PhpVersion $phpVersion | ||
| ) | ||
| { | ||
| $this->PHPUnitVersion = $PHPUnitVersion; | ||
| $this->testMethodsHelper = $testMethodsHelper; | ||
| $this->deprecationRulesInstalled = $deprecationRulesInstalled; | ||
|
|
||
| $parser = new VersionParser(); | ||
| $this->phpstanVersionConstraint = $parser->parseConstraints($phpVersion->getVersionString()); | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
|
|
@@ -62,6 +72,7 @@ public function processNode(Node $node, Scope $scope): array | |
| } | ||
|
|
||
| $errors = []; | ||
| $parser = new VersionParser(); | ||
| foreach ($reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) { | ||
| $args = $attr->getArguments(); | ||
| if (count($args) !== 1) { | ||
|
|
@@ -71,6 +82,29 @@ public function processNode(Node $node, Scope $scope): array | |
| if ( | ||
| !is_numeric($args[0]) | ||
| ) { | ||
|
|
||
| try { | ||
| $testPhpVersionConstraint = $parser->parseConstraints($args[0]); | ||
| } catch (UnexpectedValueException $e) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| sprintf($e->getMessage()), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure we want to 1:1 print the message from composer/semver as a phpstan rule
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alternative approach could be to somehow analyse |
||
| ) | ||
| ->identifier('phpunit.attributeRequiresPhpVersion') | ||
| ->build(); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if ($this->phpstanVersionConstraint->matches($testPhpVersionConstraint)) { | ||
| continue; | ||
| } | ||
|
|
||
| $errors[] = RuleErrorBuilder::message( | ||
| sprintf('Version requirement will always evaluate to false.'), | ||
| ) | ||
| ->identifier('phpunit.attributeRequiresPhpVersion') | ||
| ->build(); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -90,7 +124,6 @@ public function processNode(Node $node, Scope $scope): array | |
| ->identifier('phpunit.attributeRequiresPhpVersion') | ||
| ->build(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return $errors; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace RequiresPhpVersionMismatch; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| class InvalidConstraint extends TestCase | ||
| { | ||
| #[RequiresPhp('abc')] | ||
| public function testFoo(): void { | ||
|
|
||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace RequiresPhpVersionMismatch; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| class RequiresPhp5 extends TestCase | ||
| { | ||
| #[RequiresPhp('< 7.0')] | ||
| public function testFoo(): void { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| class RequiresPhp8 extends TestCase | ||
| { | ||
| #[RequiresPhp('>=8.0')] | ||
| public function testFoo(): void { | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should compare against the composer.json min version instead of the phpstan.neon version?