| Conditions | 13 |
| Paths | 14 |
| Total Lines | 58 |
| Code Lines | 36 |
| Lines | 28 |
| Ratio | 48.28 % |
| 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 |
||
| 104 | public function enterNode(Node $node) |
||
| 105 | { |
||
| 106 | if (!$node instanceof Node\Stmt\Throw_ |
||
| 107 | || !$node->expr instanceof Node\Expr\New_) { |
||
| 108 | $this->previousNode = $node; |
||
| 109 | |||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | $exceptionClass = $node->expr->class->parts[0]; |
||
| 114 | if (!in_array(strtolower($exceptionClass), array_map('strtolower', $this->exceptionsToExtractFrom))) { |
||
| 115 | $this->previousNode = $node; |
||
| 116 | |||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | $node = $node->expr; |
||
| 121 | $ignore = false; |
||
| 122 | $desc = $meaning = null; |
||
| 123 | View Code Duplication | if (null !== $docComment = $this->getDocCommentForNode($node)) { |
|
|
|
|||
| 124 | if ($docComment instanceof Doc) { |
||
| 125 | $docComment = $docComment->getText(); |
||
| 126 | } |
||
| 127 | foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) { |
||
| 128 | if ($annot instanceof Ignore) { |
||
| 129 | $ignore = true; |
||
| 130 | } elseif ($annot instanceof Desc) { |
||
| 131 | $desc = $annot->text; |
||
| 132 | } elseif ($annot instanceof Meaning) { |
||
| 133 | $meaning = $annot->text; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | View Code Duplication | if (!$node->args[0]->value instanceof String_) { |
|
| 139 | if ($ignore) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); |
||
| 144 | |||
| 145 | if ($this->logger) { |
||
| 146 | $this->logger->error($message); |
||
| 147 | |||
| 148 | return; |
||
| 149 | } |
||
| 150 | |||
| 151 | throw new RuntimeException($message); |
||
| 152 | } |
||
| 153 | |||
| 154 | $id = $node->args[0]->value->value; |
||
| 155 | |||
| 156 | $message = new Message($id, $this->defaultDomain); |
||
| 157 | $message->setDesc($desc); |
||
| 158 | $message->setMeaning($meaning); |
||
| 159 | $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine())); |
||
| 160 | $this->catalogue->add($message); |
||
| 161 | } |
||
| 162 | |||
| 240 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.