|
| 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\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\Rules\RuleErrorBuilder; |
| 10 | +use PHPStan\Type\FileTypeMapper; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use function array_map; |
| 13 | +use function array_merge; |
| 14 | +use function array_shift; |
| 15 | +use function count; |
| 16 | +use function in_array; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<Node\Stmt\ClassMethod> |
| 21 | + */ |
| 22 | +class ClassMethodCoversExistsRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * Covers helper. |
| 27 | + * |
| 28 | + * @var CoversHelper |
| 29 | + */ |
| 30 | + private $coversHelper; |
| 31 | + |
| 32 | + /** |
| 33 | + * The file type mapper. |
| 34 | + * |
| 35 | + * @var FileTypeMapper |
| 36 | + */ |
| 37 | + private $fileTypeMapper; |
| 38 | + |
| 39 | + public function __construct( |
| 40 | + CoversHelper $coversHelper, |
| 41 | + FileTypeMapper $fileTypeMapper |
| 42 | + ) |
| 43 | + { |
| 44 | + $this->coversHelper = $coversHelper; |
| 45 | + $this->fileTypeMapper = $fileTypeMapper; |
| 46 | + } |
| 47 | + |
| 48 | + public function getNodeType(): string |
| 49 | + { |
| 50 | + return Node\Stmt\ClassMethod::class; |
| 51 | + } |
| 52 | + |
| 53 | + public function processNode(Node $node, Scope $scope): array |
| 54 | + { |
| 55 | + $classReflection = $scope->getClassReflection(); |
| 56 | + |
| 57 | + if ($classReflection === null) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + if (!$classReflection->isSubclassOf(TestCase::class)) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + $errors = []; |
| 66 | + $classPhpDoc = $classReflection->getResolvedPhpDoc(); |
| 67 | + [$classCovers, $classCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($classPhpDoc); |
| 68 | + |
| 69 | + $classCoversStrings = array_map(static function (PhpDocTagNode $covers): string { |
| 70 | + return (string) $covers->value; |
| 71 | + }, $classCovers); |
| 72 | + |
| 73 | + $docComment = $node->getDocComment(); |
| 74 | + if ($docComment === null) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + $coversDefaultClass = count($classCoversDefaultClasses) === 1 |
| 79 | + ? array_shift($classCoversDefaultClasses) |
| 80 | + : null; |
| 81 | + |
| 82 | + $methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 83 | + $scope->getFile(), |
| 84 | + $classReflection->getName(), |
| 85 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 86 | + $node->name->toString(), |
| 87 | + $docComment->getText() |
| 88 | + ); |
| 89 | + |
| 90 | + [$methodCovers, $methodCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($methodPhpDoc); |
| 91 | + |
| 92 | + $errors = []; |
| 93 | + |
| 94 | + if (count($methodCoversDefaultClasses) > 0) { |
| 95 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 96 | + '@coversDefaultClass defined on class method %s.', |
| 97 | + $node->name |
| 98 | + ))->build(); |
| 99 | + } |
| 100 | + |
| 101 | + foreach ($methodCovers as $covers) { |
| 102 | + if (in_array((string) $covers->value, $classCoversStrings, true)) { |
| 103 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 104 | + 'Class already @covers %s so the method @covers is redundant.', |
| 105 | + $covers->value |
| 106 | + ))->build(); |
| 107 | + } |
| 108 | + |
| 109 | + $errors = array_merge( |
| 110 | + $errors, |
| 111 | + $this->coversHelper->processCovers($node, $covers, $coversDefaultClass) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return $errors; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments