-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathEmptyExceptionRule.php
64 lines (56 loc) · 1.56 KB
/
EmptyExceptionRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace TheCodingMachine\PHPStan\Rules\Exceptions;
use PhpParser\Node;
use PhpParser\Node\Stmt\Catch_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use function strpos;
/**
* @implements Rule<Catch_>
*/
class EmptyExceptionRule implements Rule
{
public function getNodeType(): string
{
return Catch_::class;
}
/**
* @param \PhpParser\Node\Stmt\Catch_ $node
* @param \PHPStan\Analyser\Scope $scope
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->isEmpty($node->stmts)) {
return [
RuleErrorBuilder::message('Empty catch block.')
->tip('If you are sure this is meant to be empty, please add a "// @ignoreException" comment in the catch block.')
->file($scope->getFile())
->line($node->getStartLine())
->build(),
];
}
return [];
}
/**
* @param Node[] $stmts
* @return bool
*/
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;
}
}
}
}
return true;
}
}