|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\PHPStan; |
| 13 | + |
| 14 | +use PhpParser\Node; |
| 15 | +use PhpParser\Node\AttributeGroup; |
| 16 | +use PhpParser\Node\Stmt\Class_; |
| 17 | +use PhpParser\Node\Stmt\ClassMethod; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\Rules\Rule; |
| 20 | +use PHPStan\Rules\RuleErrorBuilder; |
| 21 | + |
| 22 | +/** |
| 23 | + * PHPStan rule that forbids usage of test coverage attributes in tests. |
| 24 | + * |
| 25 | + * This rule enforces that Large, Small, Medium, CoversClass and UsesClass attributes |
| 26 | + * should not be used in test files. |
| 27 | + * |
| 28 | + * @author Oskar Stark <[email protected]> |
| 29 | + * |
| 30 | + * @implements Rule<Node> |
| 31 | + */ |
| 32 | +final class ForbidTestCoverageAttributesRule implements Rule |
| 33 | +{ |
| 34 | + private const FORBIDDEN_ATTRIBUTES = [ |
| 35 | + 'Large', |
| 36 | + 'Small', |
| 37 | + 'Medium', |
| 38 | + 'CoversClass', |
| 39 | + 'UsesClass', |
| 40 | + ]; |
| 41 | + |
| 42 | + public function getNodeType(): string |
| 43 | + { |
| 44 | + return Node::class; |
| 45 | + } |
| 46 | + |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + // Only check test files |
| 50 | + if (!str_ends_with($scope->getFile(), 'Test.php')) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + $errors = []; |
| 55 | + |
| 56 | + if ($node instanceof Class_ || $node instanceof ClassMethod) { |
| 57 | + foreach ($node->attrGroups as $attrGroup) { |
| 58 | + $errors = array_merge($errors, $this->checkAttributeGroup($attrGroup)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return $errors; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return array<\PHPStan\Rules\RuleError> |
| 67 | + */ |
| 68 | + private function checkAttributeGroup(AttributeGroup $attrGroup): array |
| 69 | + { |
| 70 | + $errors = []; |
| 71 | + |
| 72 | + foreach ($attrGroup->attrs as $attr) { |
| 73 | + $attributeName = $attr->name->toString(); |
| 74 | + |
| 75 | + // Handle both fully qualified and short names |
| 76 | + $shortName = $attributeName; |
| 77 | + if (str_contains($attributeName, '\\')) { |
| 78 | + $shortName = substr($attributeName, strrpos($attributeName, '\\') + 1); |
| 79 | + } |
| 80 | + |
| 81 | + if (\in_array($shortName, self::FORBIDDEN_ATTRIBUTES, true)) { |
| 82 | + $errors[] = RuleErrorBuilder::message( |
| 83 | + \sprintf('Usage of #[%s] attribute is forbidden in test files. Remove the attribute.', $shortName) |
| 84 | + ) |
| 85 | + ->line($attr->getLine()) |
| 86 | + ->identifier('symfonyAi.forbidTestCoverageAttributes') |
| 87 | + ->tip(\sprintf('Remove the #[%s] attribute from the test.', $shortName)) |
| 88 | + ->build(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $errors; |
| 93 | + } |
| 94 | +} |
0 commit comments