Skip to content

Commit 49a29c7

Browse files
authored
ThrowsPhpDocRule: whitelisted method should not report Missing @throws (fixed #104) (#107)
1 parent 099d467 commit 49a29c7

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

src/Rules/ThrowsPhpDocRule.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ public function processNode(Node $node, Scope $scope): array
165165
return $this->processNode($node->getOriginalStatement(), $scope);
166166
}
167167

168+
if ($node instanceof Node\FunctionLike) {
169+
return $this->processFunction($node, $scope);
170+
}
171+
172+
$method = $scope->getFunction();
173+
$isMethodWhitelisted = $method instanceof MethodReflection && $this->isWhitelistedMethod($method);
174+
if ($node instanceof FunctionEnd) {
175+
if ($isMethodWhitelisted && $method instanceof MethodReflection) {
176+
return $this->processWhitelistedMethod($method);
177+
}
178+
179+
return $this->processFunctionEnd($scope);
180+
}
181+
182+
if ($isMethodWhitelisted) {
183+
return [];
184+
}
185+
168186
if ($node instanceof TryCatch) {
169187
return $this->processTryCatch($node);
170188
}
@@ -193,20 +211,6 @@ public function processNode(Node $node, Scope $scope): array
193211
return $this->processExprTraversing($node->expr, $scope, true);
194212
}
195213

196-
if ($node instanceof Node\FunctionLike) {
197-
return $this->processFunction($node, $scope);
198-
}
199-
200-
if ($node instanceof FunctionEnd) {
201-
$method = $scope->getFunction();
202-
203-
if ($method instanceof MethodReflection && $this->isWhitelistedMethod($method)) {
204-
return $this->processWhitelistedMethod($method);
205-
}
206-
207-
return $this->processFunctionEnd($scope);
208-
}
209-
210214
if ($node instanceof Catch_) {
211215
return $this->processCatch($node);
212216
}
@@ -305,22 +309,8 @@ private function processTryCatchTryEnd(): array
305309
private function processThrow(Throw_ $node, Scope $scope): array
306310
{
307311
$exceptionType = $scope->getType($node->expr);
308-
$exceptionClassNames = TypeUtils::getDirectClassNames($exceptionType);
309-
$exceptionClassNames = $this->throwsScope->filterExceptionsByUncaught($exceptionClassNames);
310-
$exceptionClassNames = $this->checkedExceptionService->filterCheckedExceptions($exceptionClassNames);
311-
312-
$isInGlobalScope = $this->throwsScope->isInGlobalScope();
313-
if (!$this->reportCheckedThrowsInGlobalScope && $isInGlobalScope) {
314-
return [];
315-
}
316-
317-
return array_map(static function (string $exceptionClassName) use ($isInGlobalScope): string {
318-
if ($isInGlobalScope) {
319-
return sprintf('Throwing checked exception %s in global scope is prohibited', $exceptionClassName);
320-
}
321312

322-
return sprintf('Missing @throws %s annotation', $exceptionClassName);
323-
}, $exceptionClassNames);
313+
return $this->processThrowsTypes($exceptionType);
324314
}
325315

326316
/**
@@ -798,8 +788,17 @@ private function processThrowsTypes(Type $targetThrowType): array
798788
$targetExceptionClasses = $this->throwsScope->filterExceptionsByUncaught($targetExceptionClasses);
799789
$targetExceptionClasses = $this->checkedExceptionService->filterCheckedExceptions($targetExceptionClasses);
800790

801-
return array_map(static function (string $targetExceptionClass): string {
802-
return sprintf('Missing @throws %s annotation', $targetExceptionClass);
791+
$isInGlobalScope = $this->throwsScope->isInGlobalScope();
792+
if (!$this->reportCheckedThrowsInGlobalScope && $isInGlobalScope) {
793+
return [];
794+
}
795+
796+
return array_map(static function (string $exceptionClassName) use ($isInGlobalScope): string {
797+
if ($isInGlobalScope) {
798+
return sprintf('Throwing checked exception %s in global scope is prohibited', $exceptionClassName);
799+
}
800+
801+
return sprintf('Missing @throws %s annotation', $exceptionClassName);
803802
}, $targetExceptionClasses);
804803
}
805804

tests/src/Rules/data/method-whitelisting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function testBar(): void // error: Unused @throws RuntimeException annot
1919
throw new RuntimeException();
2020
}
2121

22+
public function testBar2(): void // I don't expect error here, you can annotate if you want
23+
{
24+
throw new RuntimeException();
25+
$this->bar();
26+
}
27+
2228
/**
2329
* @throws RuntimeException
2430
*/

0 commit comments

Comments
 (0)