|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LTS\PHPQA\PHPStan\Rules; |
| 6 | + |
| 7 | +use PhpParser\Comment; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | + |
| 14 | +/** |
| 15 | + * Bans inline PHPStan suppression annotations in source code. |
| 16 | + * |
| 17 | + * Inline suppression annotations hide type errors instead of fixing them. |
| 18 | + * Fix the underlying type issue using type guards, array shapes, or Safe functions. |
| 19 | + * |
| 20 | + * If a suppression is truly irreducible, it must be managed in phpstan.neon |
| 21 | + * with a specific identifier and path instead of hidden inline. |
| 22 | + * |
| 23 | + * Skips test files and qaConfig files. |
| 24 | + * |
| 25 | + * @implements Rule<Stmt> |
| 26 | + */ |
| 27 | +final class ForbidInlinePhpstanIgnoreRule implements Rule |
| 28 | +{ |
| 29 | + /** Matches the PHPStan inline suppression annotation pattern */ |
| 30 | + private const string PATTERN = '/@phpstan\x2dignore/'; |
| 31 | + |
| 32 | + public function getNodeType(): string |
| 33 | + { |
| 34 | + return Stmt::class; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return list<\PHPStan\Rules\IdentifierRuleError> |
| 39 | + */ |
| 40 | + public function processNode(Node $node, Scope $scope): array |
| 41 | + { |
| 42 | + if ($this->isTestContext($scope)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + $namespace = $scope->getNamespace(); |
| 47 | + if (null !== $namespace && str_starts_with($namespace, 'QaConfig')) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + $comments = $node->getComments(); |
| 52 | + if ([] === $comments) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + $errors = []; |
| 57 | + |
| 58 | + foreach ($comments as $comment) { |
| 59 | + if (!$this->containsSuppressionAnnotation($comment)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + $errors[] = RuleErrorBuilder::message( |
| 64 | + 'Inline PHPStan suppression annotations are forbidden. ' |
| 65 | + . 'Fix the underlying type issue instead ' |
| 66 | + . '(use type guards, array shapes, \\Safe\\ functions, etc.). ' |
| 67 | + . 'If truly irreducible, manage via ignoreErrors in phpstan.neon ' |
| 68 | + . 'with a specific identifier and path.', |
| 69 | + )->identifier('phpqaci.inlinePhpstanIgnore')->line($comment->getStartLine())->build(); |
| 70 | + } |
| 71 | + |
| 72 | + return $errors; |
| 73 | + } |
| 74 | + |
| 75 | + private function containsSuppressionAnnotation(Comment $comment): bool |
| 76 | + { |
| 77 | + $matchCount = \preg_match(self::PATTERN, $comment->getText()); |
| 78 | + |
| 79 | + return \is_int($matchCount) && $matchCount > 0; |
| 80 | + } |
| 81 | + |
| 82 | + private function isTestContext(Scope $scope): bool |
| 83 | + { |
| 84 | + $namespace = $scope->getNamespace(); |
| 85 | + if (null !== $namespace && str_contains($namespace, 'Tests')) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + return str_contains($scope->getFile(), '/tests/'); |
| 90 | + } |
| 91 | +} |
0 commit comments