| Conditions | 5 |
| Paths | 9 |
| Total Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 40 | public function processNode(Node $node, Scope $scope): array |
||
| 41 | { |
||
| 42 | // Let's only apply the filter to \Exception, \RuntimeException or \Throwable |
||
| 43 | $exceptionType = null; |
||
| 44 | foreach ($node->types as $type) { |
||
|
|
|||
| 45 | if (in_array((string)$type, [Exception::class, RuntimeException::class, Throwable::class], true)) { |
||
| 46 | $exceptionType = (string)$type; |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($exceptionType === null) { |
||
| 52 | return []; |
||
| 53 | } |
||
| 54 | |||
| 55 | $exceptionVarName = $node->var->name; |
||
| 56 | |||
| 57 | // Let's visit and find a throw. |
||
| 58 | $visitor = new class($exceptionVarName) extends NodeVisitorAbstract { |
||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private $throwFound = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | private $throwFoundProbably = false; |
||
| 68 | |||
| 69 | private $exceptionVarName; |
||
| 70 | |||
| 71 | public function __construct(string $exceptionVarName) |
||
| 72 | { |
||
| 73 | $this->exceptionVarName = $exceptionVarName; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function leaveNode(Node $node) |
||
| 77 | { |
||
| 78 | // Only rethrow through static methods are allowed |
||
| 79 | if ($node instanceof StaticCall) { |
||
| 80 | $this->throwFoundProbably = $this->isProbablyAThrow($node); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($node instanceof Node\Stmt\Throw_) { |
||
| 84 | $this->throwFound = true; |
||
| 85 | } |
||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function isThrowFound(): bool |
||
| 93 | { |
||
| 94 | return $this->throwFound; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function isThrowProbablyFound(): bool |
||
| 101 | { |
||
| 102 | return $this->throwFoundProbably; |
||
| 103 | } |
||
| 104 | |||
| 105 | private function isProbablyAThrow(Node $node) |
||
| 106 | { |
||
| 107 | if (!$args = $node->args) { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | $varArgs = array_filter($args, function ($arg) { |
||
| 112 | return $arg->value instanceof Variable && $arg->value->name === $this->exceptionVarName ; |
||
| 113 | }); |
||
| 114 | |||
| 115 | return 0 !== count($varArgs); |
||
| 116 | } |
||
| 117 | }; |
||
| 118 | |||
| 119 | $traverser = new NodeTraverser(); |
||
| 120 | |||
| 121 | $traverser->addVisitor($visitor); |
||
| 122 | |||
| 123 | $traverser->traverse($node->stmts); |
||
| 124 | |||
| 125 | $errors = []; |
||
| 126 | |||
| 127 | if (!$visitor->isThrowFound() && !$visitor->isThrowProbablyFound()) { |
||
| 128 | $errors[] = 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); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $errors; |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: