Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a @ignoreException annotation to override empty catch block rule #44

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}