|
3 | 3 | namespace PHPStan\Rules\PHPUnit; |
4 | 4 |
|
5 | 5 | use PhpParser\Node; |
| 6 | +use PhpParser\Node\Name; |
6 | 7 | use PHPStan\Analyser\Scope; |
7 | 8 | use PHPStan\Node\InClassNode; |
| 9 | +use PHPStan\Reflection\ClassReflection; |
8 | 10 | use PHPStan\Reflection\ReflectionProvider; |
| 11 | +use PHPStan\Rules\IdentifierRuleError; |
9 | 12 | use PHPStan\Rules\Rule; |
10 | 13 | use PHPStan\Rules\RuleErrorBuilder; |
11 | 14 | use PHPUnit\Framework\TestCase; |
12 | 15 | use function array_merge; |
13 | 16 | use function array_shift; |
14 | 17 | use function count; |
| 18 | +use function is_string; |
15 | 19 | use function sprintf; |
16 | 20 |
|
17 | 21 | /** |
@@ -85,7 +89,114 @@ public function processNode(Node $node, Scope $scope): array |
85 | 89 | ); |
86 | 90 | } |
87 | 91 |
|
| 92 | + return array_merge($errors, $this->processCoversAttributes($classReflection)); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks the #[CoversClass], #[CoversTrait], #[CoversMethod] and #[CoversFunction] attributes. |
| 97 | + * |
| 98 | + * @return list<IdentifierRuleError> |
| 99 | + */ |
| 100 | + private function processCoversAttributes(ClassReflection $classReflection): array |
| 101 | + { |
| 102 | + $betterReflection = $classReflection->getNativeReflection()->getBetterReflection(); |
| 103 | + $errors = []; |
| 104 | + |
| 105 | + foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversClass') as $attribute) { |
| 106 | + $className = $this->getStringArgument($attribute->getArguments(), 0, 'className'); |
| 107 | + if ($className === null) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 112 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 113 | + '#[CoversClass] value %s references an invalid class.', |
| 114 | + $className, |
| 115 | + ))->identifier('phpunit.coversClass')->build(); |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (!$this->reflectionProvider->getClass($className)->isInterface()) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 124 | + '#[CoversClass] value %s references an interface.', |
| 125 | + $className, |
| 126 | + ))->identifier('phpunit.coversInterface')->build(); |
| 127 | + } |
| 128 | + |
| 129 | + foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversTrait') as $attribute) { |
| 130 | + $traitName = $this->getStringArgument($attribute->getArguments(), 0, 'traitName'); |
| 131 | + if ($traitName === null) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if ($this->reflectionProvider->hasClass($traitName) && $this->reflectionProvider->getClass($traitName)->isTrait()) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 140 | + '#[CoversTrait] value %s references an invalid trait.', |
| 141 | + $traitName, |
| 142 | + ))->identifier('phpunit.coversTrait')->build(); |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversMethod') as $attribute) { |
| 146 | + $arguments = $attribute->getArguments(); |
| 147 | + $className = $this->getStringArgument($arguments, 0, 'className'); |
| 148 | + $methodName = $this->getStringArgument($arguments, 1, 'methodName'); |
| 149 | + if ($className === null || $methodName === null) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 154 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 155 | + '#[CoversMethod] value %s::%s references an invalid class.', |
| 156 | + $className, |
| 157 | + $methodName, |
| 158 | + ))->identifier('phpunit.coversMethod')->build(); |
| 159 | + continue; |
| 160 | + } |
| 161 | + |
| 162 | + if ($this->reflectionProvider->getClass($className)->hasMethod($methodName)) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 167 | + '#[CoversMethod] value %s::%s references an invalid method.', |
| 168 | + $className, |
| 169 | + $methodName, |
| 170 | + ))->identifier('phpunit.coversMethod')->build(); |
| 171 | + } |
| 172 | + |
| 173 | + foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversFunction') as $attribute) { |
| 174 | + $functionName = $this->getStringArgument($attribute->getArguments(), 0, 'functionName'); |
| 175 | + if ($functionName === null) { |
| 176 | + continue; |
| 177 | + } |
| 178 | + |
| 179 | + if ($this->reflectionProvider->hasFunction(new Name($functionName), null)) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 184 | + '#[CoversFunction] value %s references an invalid function.', |
| 185 | + $functionName, |
| 186 | + ))->identifier('phpunit.coversFunction')->build(); |
| 187 | + } |
| 188 | + |
88 | 189 | return $errors; |
89 | 190 | } |
90 | 191 |
|
| 192 | + /** |
| 193 | + * @param array<int|string, mixed> $arguments |
| 194 | + */ |
| 195 | + private function getStringArgument(array $arguments, int $position, string $name): ?string |
| 196 | + { |
| 197 | + $value = $arguments[$position] ?? $arguments[$name] ?? null; |
| 198 | + |
| 199 | + return is_string($value) && $value !== '' ? $value : null; |
| 200 | + } |
| 201 | + |
91 | 202 | } |
0 commit comments