Conditions | 2 |
Paths | 2 |
Total Lines | 59 |
Code Lines | 25 |
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 |
||
31 | public function processNode(Node $node, Scope $scope): array |
||
32 | { |
||
33 | $visitor = new class($node->var) extends NodeVisitorAbstract { |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $catchedVariableName; |
||
38 | private $exceptionUsedCount = 0; |
||
39 | private $unusedThrows = []; |
||
40 | |||
41 | public function __construct(string $catchedVariableName) |
||
42 | { |
||
43 | |||
44 | $this->catchedVariableName = $catchedVariableName; |
||
45 | } |
||
46 | |||
47 | public function leaveNode(Node $node) { |
||
48 | if ($node instanceof Node\Expr\Variable) { |
||
|
|||
49 | if ($node->name === $this->catchedVariableName) { |
||
50 | $this->exceptionUsedCount++; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | // If the variable is used in the context of a method call (like $e->getMessage()), the exception is not passed as a "previous exception". |
||
55 | if ($node instanceof Node\Expr\MethodCall) { |
||
56 | if ($node->var instanceof Node\Expr\Variable && $node->var->name === $this->catchedVariableName) { |
||
57 | $this->exceptionUsedCount--; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | if ($node instanceof Node\Stmt\Throw_ && $this->exceptionUsedCount === 0) { |
||
62 | $this->unusedThrows[] = $node; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return array |
||
68 | */ |
||
69 | public function getUnusedThrows(): array |
||
70 | { |
||
71 | return $this->unusedThrows; |
||
72 | } |
||
73 | |||
74 | }; |
||
75 | |||
76 | $traverser = new NodeTraverser(); |
||
77 | |||
78 | $traverser->addVisitor($visitor); |
||
79 | |||
80 | $traverser->traverse($node->stmts); |
||
81 | |||
82 | $errors = []; |
||
83 | |||
84 | foreach ($visitor->getUnusedThrows() as $throw) { |
||
85 | $errors[] = sprintf('Thrown exceptions in a catch block must bundle the previous exception (see throw statement line %d)', $throw->getLine()); |
||
86 | } |
||
87 | |||
88 | return $errors; |
||
89 | } |
||
90 | } |
||
91 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.