Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | final class NodeGenerator |
||
27 | { |
||
28 | /** |
||
29 | * @var PhpGenerator |
||
30 | */ |
||
31 | private $phpGenerator; |
||
32 | |||
33 | /** |
||
34 | * @param PhpGenerator $phpGenerator |
||
35 | */ |
||
36 | public function __construct(PhpGenerator $phpGenerator) |
||
40 | |||
41 | /** |
||
42 | * @param $query |
||
43 | * @return string |
||
44 | */ |
||
45 | public function generateByJson($query): string |
||
109 | |||
110 | /** |
||
111 | * @param string $code |
||
112 | * @return string |
||
113 | */ |
||
114 | private function structureCode(string $code): string |
||
115 | { |
||
116 | $codeWithLinebreaks = str_replace('->add', "\n->add", $code); |
||
117 | |||
118 | $lines = explode("\n", $codeWithLinebreaks); |
||
119 | |||
120 | $position = 0; |
||
121 | |||
122 | $structuredLines = []; |
||
123 | |||
124 | foreach ($lines as $i => $line) { |
||
125 | if (0 === strpos($line, '->add') && false === strpos($structuredLines[count($structuredLines) - 1], ' )') && false === strpos($structuredLines[count($structuredLines) - 1], 'ScalarNode')) { |
||
126 | $position++; |
||
127 | } |
||
128 | $lineLength = count($lines) - 1 !== $i ? strlen($line) -1 : strlen($line) - 2; |
||
129 | $braceCount = 0; |
||
130 | while(')' === $line[$lineLength--]) { |
||
131 | $braceCount++; |
||
132 | } |
||
133 | $prefix = str_pad('', $position * 4); |
||
134 | if ($braceCount > 2) { |
||
135 | $structuredLines[] = $prefix . substr($line, 0, - ($braceCount - 2)); |
||
136 | } else { |
||
137 | $structuredLines[] = $prefix . $line; |
||
138 | } |
||
139 | |||
140 | while ($braceCount-- > 2) { |
||
141 | $position--; |
||
142 | $structuredLines[] = str_pad('', $position * 4) . ')'; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | $structuredLines[count($structuredLines) - 1] .= ';'; |
||
147 | |||
148 | return implode("\n", $structuredLines); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return Expr |
||
153 | */ |
||
154 | private function createObjectNode(): Expr |
||
158 | |||
159 | /** |
||
160 | * @return Expr |
||
161 | */ |
||
162 | private function createArrayNode(): Expr |
||
166 | |||
167 | /** |
||
168 | * @param string|float|int|bool|null $value |
||
169 | * @return Expr |
||
170 | */ |
||
171 | View Code Duplication | private function createScalarNode($value): Expr |
|
187 | |||
188 | /** |
||
189 | * @param array $data |
||
190 | * @return Expr |
||
191 | */ |
||
192 | private function appendChildrenToArrayNode(array $data) |
||
216 | |||
217 | /** |
||
218 | * @param \stdClass $data |
||
219 | * @return Expr |
||
220 | */ |
||
221 | private function appendChildrenToObjectNode(\stdClass $data) |
||
245 | } |
||
246 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.