|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\InClassMethodNode; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use function count; |
| 12 | +use function is_numeric; |
| 13 | +use function method_exists; |
| 14 | +use function sprintf; |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements Rule<InClassMethodNode> |
| 18 | + */ |
| 19 | +class AttributeRequiresPhpVersionRule implements Rule |
| 20 | +{ |
| 21 | + |
| 22 | + private PHPUnitVersion $PHPUnitVersion; |
| 23 | + |
| 24 | + private TestMethodsHelper $testMethodsHelper; |
| 25 | + |
| 26 | + /** |
| 27 | + * When phpstan-deprecation-rules is installed, it reports deprecated usages. |
| 28 | + */ |
| 29 | + private bool $deprecationRulesInstalled; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + PHPUnitVersion $PHPUnitVersion, |
| 33 | + TestMethodsHelper $testMethodsHelper, |
| 34 | + bool $deprecationRulesInstalled |
| 35 | + ) |
| 36 | + { |
| 37 | + $this->PHPUnitVersion = $PHPUnitVersion; |
| 38 | + $this->testMethodsHelper = $testMethodsHelper; |
| 39 | + $this->deprecationRulesInstalled = $deprecationRulesInstalled; |
| 40 | + } |
| 41 | + |
| 42 | + public function getNodeType(): string |
| 43 | + { |
| 44 | + return InClassMethodNode::class; |
| 45 | + } |
| 46 | + |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + $classReflection = $scope->getClassReflection(); |
| 50 | + if ($classReflection === null || $classReflection->is(TestCase::class) === false) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + $reflectionMethod = $this->testMethodsHelper->getTestMethodReflection($classReflection, $node->getMethodReflection(), $scope); |
| 55 | + if ($reflectionMethod === null) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + /** @phpstan-ignore function.alreadyNarrowedType */ |
| 60 | + if (!method_exists($reflectionMethod, 'getAttributes')) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + $errors = []; |
| 65 | + foreach ($reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) { |
| 66 | + $args = $attr->getArguments(); |
| 67 | + if (count($args) !== 1) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + if ( |
| 72 | + !is_numeric($args[0]) |
| 73 | + ) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if ($this->PHPUnitVersion->notSupportsPhpversionAttributeWithoutOperator()->yes()) { |
| 78 | + $errors[] = RuleErrorBuilder::message( |
| 79 | + sprintf('Version requirement is missing operator.'), |
| 80 | + ) |
| 81 | + ->identifier('phpunit.attributeRequiresPhpVersion') |
| 82 | + ->build(); |
| 83 | + } elseif ( |
| 84 | + $this->deprecationRulesInstalled |
| 85 | + && $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes() |
| 86 | + ) { |
| 87 | + $errors[] = RuleErrorBuilder::message( |
| 88 | + sprintf('Version requirement without operator is deprecated.'), |
| 89 | + ) |
| 90 | + ->identifier('phpunit.attributeRequiresPhpVersion') |
| 91 | + ->build(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + return $errors; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments