Skip to content

Commit

Permalink
Merge pull request #44 from moufmouf/empty_catch_annotation
Browse files Browse the repository at this point in the history
Adding a @IgnoreException annotation to override empty catch block rule
  • Loading branch information
moufmouf authored Jan 18, 2019
2 parents 8611705 + 4734a38 commit 663fad8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Rules/Exceptions/EmptyExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
use function strpos;

class EmptyExceptionRule implements Rule
{
Expand All @@ -25,7 +26,7 @@ public function processNode(Node $node, Scope $scope): array
{
if ($this->isEmpty($node->stmts)) {
return [
'Empty catch block'
'Empty catch block. If you are sure this is meant to be empty, please add a "// @ignoreException" comment in the catch block.'
];
}

Expand All @@ -41,6 +42,12 @@ private function isEmpty(array $stmts): bool
foreach ($stmts as $stmt) {
if (!$stmt instanceof Node\Stmt\Nop) {
return false;
} else {
foreach ($stmt->getComments() as $comment) {
if (strpos($comment->getText(), '@ignoreException') !== false) {
return false;
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Rules/Exceptions/EmptyExceptionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function testCheckCatchedException()
{
$this->analyse([__DIR__ . '/data/catch.php'], [
[
'Empty catch block',
'Empty catch block. If you are sure this is meant to be empty, please add a "// @ignoreException" comment in the catch block.',
14,
],
[
'Empty catch block',
'Empty catch block. If you are sure this is meant to be empty, please add a "// @ignoreException" comment in the catch block.',
24,
],
]);
Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/Exceptions/data/catch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ class MyCatchException extends \Exception
} catch (\Exception $e) {
// Do nothing
}

try {
} catch (\Exception $e) {
// @ignoreException
}

0 comments on commit 663fad8

Please sign in to comment.