-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMustRethrowRule.php
95 lines (80 loc) · 2.65 KB
/
MustRethrowRule.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace TheCodingMachine\PHPStan\Rules\Exceptions;
use Exception;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use function in_array;
use PhpParser\Node;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use RuntimeException;
use TheCodingMachine\PHPStan\Utils\PrefixGenerator;
use Throwable;
/**
* When catching \Exception, \RuntimeException or \Throwable, the exception MUST be thrown again
* (unless you are developing an exception handler...)
*
* @implements Rule<Catch_>
*/
class MustRethrowRule implements Rule
{
public function getNodeType(): string
{
return Catch_::class;
}
/**
* @param Catch_ $node
* @param \PHPStan\Analyser\Scope $scope
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
// Let's only apply the filter to \Exception, \RuntimeException or \Throwable
$exceptionType = null;
foreach ($node->types as $type) {
if (in_array((string)$type, [Exception::class, RuntimeException::class, Throwable::class], true)) {
$exceptionType = (string)$type;
break;
}
}
if ($exceptionType === null) {
return [];
}
// Let's visit and find a throw.
$visitor = new class() extends NodeVisitorAbstract {
/**
* @var bool
*/
private $throwFound = false;
public function leaveNode(Node $node)
{
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
$this->throwFound = true;
}
return null;
}
/**
* @return bool
*/
public function isThrowFound(): bool
{
return $this->throwFound;
}
};
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$traverser->traverse($node->stmts);
$errors = [];
if (!$visitor->isThrowFound()) {
$message = sprintf('%scaught "%s" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud', PrefixGenerator::generatePrefix($scope), $exceptionType);
$errors[] = RuleErrorBuilder::message($message)
->line($node->getStartLine())
->file($scope->getFile())
->build();
}
return $errors;
}
}