Skip to content

Commit

Permalink
ThrowsPhpDocRule: whitelisted method should not report `Missing @thro…
Browse files Browse the repository at this point in the history
…ws` (fixed #104)
  • Loading branch information
pepakriz committed Dec 5, 2019
1 parent 099d467 commit 218b271
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ public function processNode(Node $node, Scope $scope): array
return $this->processNode($node->getOriginalStatement(), $scope);
}

if ($node instanceof Node\FunctionLike) {
return $this->processFunction($node, $scope);
}

$method = $scope->getFunction();
$isMethodWhitelisted = $method instanceof MethodReflection && $this->isWhitelistedMethod($method);
if ($node instanceof FunctionEnd) {
if ($isMethodWhitelisted && $method instanceof MethodReflection) {
return $this->processWhitelistedMethod($method);
}

return $this->processFunctionEnd($scope);
}

if ($isMethodWhitelisted) {
return [];
}

if ($node instanceof TryCatch) {
return $this->processTryCatch($node);
}
Expand Down Expand Up @@ -193,20 +211,6 @@ public function processNode(Node $node, Scope $scope): array
return $this->processExprTraversing($node->expr, $scope, true);
}

if ($node instanceof Node\FunctionLike) {
return $this->processFunction($node, $scope);
}

if ($node instanceof FunctionEnd) {
$method = $scope->getFunction();

if ($method instanceof MethodReflection && $this->isWhitelistedMethod($method)) {
return $this->processWhitelistedMethod($method);
}

return $this->processFunctionEnd($scope);
}

if ($node instanceof Catch_) {
return $this->processCatch($node);
}
Expand Down Expand Up @@ -305,22 +309,8 @@ private function processTryCatchTryEnd(): array
private function processThrow(Throw_ $node, Scope $scope): array
{
$exceptionType = $scope->getType($node->expr);
$exceptionClassNames = TypeUtils::getDirectClassNames($exceptionType);
$exceptionClassNames = $this->throwsScope->filterExceptionsByUncaught($exceptionClassNames);
$exceptionClassNames = $this->checkedExceptionService->filterCheckedExceptions($exceptionClassNames);

$isInGlobalScope = $this->throwsScope->isInGlobalScope();
if (!$this->reportCheckedThrowsInGlobalScope && $isInGlobalScope) {
return [];
}

return array_map(static function (string $exceptionClassName) use ($isInGlobalScope): string {
if ($isInGlobalScope) {
return sprintf('Throwing checked exception %s in global scope is prohibited', $exceptionClassName);
}

return sprintf('Missing @throws %s annotation', $exceptionClassName);
}, $exceptionClassNames);
return $this->processThrowsTypes($exceptionType);
}

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

return array_map(static function (string $targetExceptionClass): string {
return sprintf('Missing @throws %s annotation', $targetExceptionClass);
$isInGlobalScope = $this->throwsScope->isInGlobalScope();
if (!$this->reportCheckedThrowsInGlobalScope && $isInGlobalScope) {
return [];
}

return array_map(static function (string $exceptionClassName) use ($isInGlobalScope): string {
if ($isInGlobalScope) {
return sprintf('Throwing checked exception %s in global scope is prohibited', $exceptionClassName);
}

return sprintf('Missing @throws %s annotation', $exceptionClassName);
}, $targetExceptionClasses);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/src/Rules/data/method-whitelisting.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function testBar(): void // error: Unused @throws RuntimeException annot
throw new RuntimeException();
}

public function testBar2(): void // I don't expect error here, you can annotate if you want
{
throw new RuntimeException();
$this->bar();
}

/**
* @throws RuntimeException
*/
Expand Down

0 comments on commit 218b271

Please sign in to comment.