|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Pepakriz\PHPStanExceptionRules\Rules; |
| 4 | + |
| 5 | +use Pepakriz\PHPStanExceptionRules\ThrowsAnnotationReader; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\FunctionLike; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PhpParser\Node\Stmt\Function_; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Broker\Broker; |
| 12 | +use PHPStan\Broker\ClassNotFoundException; |
| 13 | +use PHPStan\Broker\FunctionNotFoundException; |
| 14 | +use PHPStan\Reflection\MissingMethodFromReflectionException; |
| 15 | +use PHPStan\Rules\Rule; |
| 16 | +use PHPStan\ShouldNotHappenException; |
| 17 | +use function array_keys; |
| 18 | +use function ltrim; |
| 19 | +use function sprintf; |
| 20 | +use function uksort; |
| 21 | + |
| 22 | +class UselessThrowsPhpDocRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Broker |
| 27 | + */ |
| 28 | + private $broker; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ThrowsAnnotationReader |
| 32 | + */ |
| 33 | + private $throwsAnnotationReader; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + Broker $broker, |
| 37 | + ThrowsAnnotationReader $throwsAnnotationReader |
| 38 | + ) |
| 39 | + { |
| 40 | + $this->broker = $broker; |
| 41 | + $this->throwsAnnotationReader = $throwsAnnotationReader; |
| 42 | + } |
| 43 | + |
| 44 | + public function getNodeType(): string |
| 45 | + { |
| 46 | + return FunctionLike::class; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string[] |
| 51 | + */ |
| 52 | + public function processNode(Node $node, Scope $scope): array |
| 53 | + { |
| 54 | + $docComment = $node->getDocComment(); |
| 55 | + if ($docComment === null) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + if ($node instanceof ClassMethod) { |
| 60 | + $classReflection = $scope->getClassReflection(); |
| 61 | + if ($classReflection === null) { |
| 62 | + throw new ShouldNotHappenException(); |
| 63 | + } |
| 64 | + |
| 65 | + $methodName = $node->name->toString(); |
| 66 | + try { |
| 67 | + $functionReflection = $classReflection->getMethod($methodName, $scope); |
| 68 | + } catch (MissingMethodFromReflectionException $e) { |
| 69 | + throw new ShouldNotHappenException(); |
| 70 | + } |
| 71 | + |
| 72 | + } elseif ($node instanceof Function_) { |
| 73 | + $functionName = ltrim($scope->getNamespace() . '\\' . $node->name->toString(), '\\'); |
| 74 | + try { |
| 75 | + $functionReflection = $this->broker->getFunction(new Node\Name\FullyQualified($functionName), $scope); |
| 76 | + } catch (FunctionNotFoundException $e) { |
| 77 | + throw new ShouldNotHappenException(); |
| 78 | + } |
| 79 | + |
| 80 | + } else { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + $throwsAnnotations = $this->throwsAnnotationReader->readByReflection($functionReflection, $scope); |
| 85 | + |
| 86 | + try { |
| 87 | + return $this->checkUselessThrows($throwsAnnotations); |
| 88 | + } catch (ClassNotFoundException $exception) { |
| 89 | + return []; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string[][] $throwsAnnotations |
| 95 | + * @return string[] |
| 96 | + * |
| 97 | + * @throws ClassNotFoundException |
| 98 | + */ |
| 99 | + private function checkUselessThrows(array $throwsAnnotations): array |
| 100 | + { |
| 101 | + /** @var string[] $errors */ |
| 102 | + $errors = []; |
| 103 | + |
| 104 | + $this->sortThrowsAnnotationsHierarchically($throwsAnnotations); |
| 105 | + |
| 106 | + /** @var bool[] $usefulThrows */ |
| 107 | + $usefulThrows = []; |
| 108 | + foreach ($throwsAnnotations as $exceptionClass => $descriptions) { |
| 109 | + foreach ($descriptions as $description) { |
| 110 | + if ( |
| 111 | + isset($usefulThrows[$exceptionClass]) |
| 112 | + || ( |
| 113 | + $description === '' && $this->isSubtypeOfUsefulThrows($exceptionClass, array_keys($usefulThrows)) |
| 114 | + ) |
| 115 | + ) { |
| 116 | + $errors[] = sprintf('Useless @throws %s annotation', $exceptionClass); |
| 117 | + } |
| 118 | + |
| 119 | + $usefulThrows[$exceptionClass] = true; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return $errors; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param string[] $usefulThrows |
| 128 | + * |
| 129 | + * @throws ClassNotFoundException |
| 130 | + */ |
| 131 | + private function isSubtypeOfUsefulThrows(string $exceptionClass, array $usefulThrows): bool |
| 132 | + { |
| 133 | + $classReflection = $this->broker->getClass($exceptionClass); |
| 134 | + |
| 135 | + foreach ($usefulThrows as $usefulThrow) { |
| 136 | + if ($classReflection->isSubclassOf($usefulThrow)) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param string[][] $throwsAnnotations |
| 146 | + * |
| 147 | + * @throws ClassNotFoundException |
| 148 | + */ |
| 149 | + private function sortThrowsAnnotationsHierarchically(array &$throwsAnnotations): void |
| 150 | + { |
| 151 | + uksort($throwsAnnotations, function (string $leftClass, string $rightClass): int { |
| 152 | + $leftReflection = $this->broker->getClass($leftClass); |
| 153 | + $rightReflection = $this->broker->getClass($rightClass); |
| 154 | + |
| 155 | + // Ensure canonical class names |
| 156 | + $leftClass = $leftReflection->getName(); |
| 157 | + $rightClass = $rightReflection->getName(); |
| 158 | + |
| 159 | + if ($leftClass === $rightClass) { |
| 160 | + return 0; |
| 161 | + } |
| 162 | + |
| 163 | + if ($leftReflection->isSubclassOf($rightClass)) { |
| 164 | + return 1; |
| 165 | + } |
| 166 | + |
| 167 | + if ($rightReflection->isSubclassOf($leftClass)) { |
| 168 | + return -1; |
| 169 | + } |
| 170 | + |
| 171 | + // Doesn't matter, sort consistently on classname |
| 172 | + return $leftClass <=> $rightClass; |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments