-
Notifications
You must be signed in to change notification settings - Fork 51
Implement AttributeRequiresPhpVersionRule #268
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PHPUnit; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\InClassMethodNode; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPUnit\Framework\TestCase; | ||
| use function count; | ||
| use function is_numeric; | ||
| use function method_exists; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @implements Rule<InClassMethodNode> | ||
| */ | ||
| class AttributeRequiresPhpVersionRule implements Rule | ||
| { | ||
|
|
||
| private PHPUnitVersion $PHPUnitVersion; | ||
|
|
||
| private TestMethodsHelper $testMethodsHelper; | ||
|
|
||
| /** | ||
| * When phpstan-deprecation-rules is installed, it reports deprecated usages. | ||
| */ | ||
| private bool $deprecationRulesInstalled; | ||
|
|
||
| public function __construct( | ||
| PHPUnitVersion $PHPUnitVersion, | ||
| TestMethodsHelper $testMethodsHelper, | ||
| bool $deprecationRulesInstalled | ||
| ) | ||
| { | ||
| $this->PHPUnitVersion = $PHPUnitVersion; | ||
| $this->testMethodsHelper = $testMethodsHelper; | ||
| $this->deprecationRulesInstalled = $deprecationRulesInstalled; | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return InClassMethodNode::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $classReflection = $scope->getClassReflection(); | ||
| if ($classReflection === null || $classReflection->is(TestCase::class) === false) { | ||
| return []; | ||
| } | ||
|
|
||
| $reflectionMethod = $this->testMethodsHelper->getTestMethodReflection($classReflection, $node->getMethodReflection(), $scope); | ||
| if ($reflectionMethod === null) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @phpstan-ignore function.alreadyNarrowedType */ | ||
| if (!method_exists($reflectionMethod, 'getAttributes')) { | ||
| return []; | ||
| } | ||
|
|
||
| $errors = []; | ||
| foreach ($reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) { | ||
| $args = $attr->getArguments(); | ||
| if (count($args) !== 1) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( | ||
| !is_numeric($args[0]) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| sprintf('Version requirement is missing operator.'), | ||
| ) | ||
| ->identifier('phpunit.attributeRequiresPhpVersion') | ||
| ->build(); | ||
| } elseif ( | ||
| $this->deprecationRulesInstalled | ||
| && $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes() | ||
| ) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| sprintf('Version requirement without operator is deprecated.'), | ||
| ) | ||
| ->identifier('phpunit.attributeRequiresPhpVersion') | ||
| ->build(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PHPUnit; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPStan\Type\FileTypeMapper; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<AttributeRequiresPhpVersionRule> | ||
| */ | ||
| final class AttributeRequiresPhpVersionRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| private ?int $phpunitMajorVersion; | ||
|
|
||
| private ?int $phpunitMinorVersion; | ||
|
|
||
| private bool $deprecationRulesInstalled = true; | ||
|
|
||
| public function testRuleOnPHPUnitUnknown(): void | ||
| { | ||
| $this->phpunitMajorVersion = null; | ||
| $this->phpunitMinorVersion = null; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], []); | ||
| } | ||
|
|
||
| public function testRuleOnPHPUnit115(): void | ||
| { | ||
| $this->phpunitMajorVersion = 11; | ||
| $this->phpunitMinorVersion = 5; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], []); | ||
| } | ||
|
|
||
| public function testRuleOnPHPUnit123(): void | ||
| { | ||
| $this->phpunitMajorVersion = 12; | ||
| $this->phpunitMinorVersion = 3; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], []); | ||
| } | ||
|
|
||
| public function testRuleOnPHPUnit124DeprecationsOn(): void | ||
| { | ||
| $this->phpunitMajorVersion = 12; | ||
| $this->phpunitMinorVersion = 4; | ||
| $this->deprecationRulesInstalled = true; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], [ | ||
| [ | ||
| 'Version requirement without operator is deprecated.', | ||
| 12, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testRuleOnPHPUnit124DeprecationsOff(): void | ||
| { | ||
| $this->phpunitMajorVersion = 12; | ||
| $this->phpunitMinorVersion = 4; | ||
| $this->deprecationRulesInstalled = false; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], []); | ||
| } | ||
|
|
||
| public function testRuleOnPHPUnit13(): void | ||
| { | ||
| $this->phpunitMajorVersion = 13; | ||
| $this->phpunitMinorVersion = 0; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/requires-php-version.php'], [ | ||
| [ | ||
| 'Version requirement is missing operator.', | ||
| 12, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); | ||
|
|
||
| return new AttributeRequiresPhpVersionRule( | ||
| $phpunitVersion, | ||
| new TestMethodsHelper( | ||
| self::getContainer()->getByType(FileTypeMapper::class), | ||
| $phpunitVersion, | ||
| ), | ||
| $this->deprecationRulesInstalled, | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace RequiresPhpVersion; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| class DeprecatedVersionFormat extends TestCase | ||
| { | ||
| #[RequiresPhp('8.0')] | ||
| public function testDeprecatedFormat(): void { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| class AllGoodTest extends TestCase | ||
| { | ||
| #[RequiresPhp('>=8.0')] | ||
| public function testHappyPath(): void { | ||
|
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
in a followup I want to emit errors when the version contstraint used within
#[RequiresPhp('8.0')]does not fit into the php version used for analysis (detecting tests which require php-versions no longer supported in the project)