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:
Complex classes like NodeGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NodeGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class NodeGenerator |
||
21 | { |
||
22 | /** |
||
23 | * @var PhpGenerator |
||
24 | */ |
||
25 | private $phpGenerator; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $useQueryBuilderFactory; |
||
31 | |||
32 | /** |
||
33 | * @param PhpGenerator $phpGenerator |
||
34 | * @param bool $useQueryBuilderFactory |
||
35 | */ |
||
36 | 15 | public function __construct(PhpGenerator $phpGenerator, bool $useQueryBuilderFactory = false) |
|
41 | |||
42 | /** |
||
43 | * @param $query |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 15 | public function generateByJson($query): string |
|
64 | |||
65 | /** |
||
66 | * @return Expr |
||
67 | */ |
||
68 | 13 | View Code Duplication | private function createObjectNode(): Expr |
76 | |||
77 | /** |
||
78 | * @return Expr |
||
79 | */ |
||
80 | 4 | View Code Duplication | private function createArrayNode(): Expr |
88 | |||
89 | /** |
||
90 | * @param string|float|int|bool|null $value |
||
91 | * |
||
92 | * @return Expr |
||
93 | */ |
||
94 | 13 | private function createScalarNode($value): Expr |
|
102 | |||
103 | /** |
||
104 | * @param string|float|int|bool|null $value |
||
105 | * |
||
106 | * @return Expr |
||
107 | */ |
||
108 | 12 | View Code Duplication | private function createScalarNodeDefault($value): Expr |
122 | |||
123 | /** |
||
124 | * @param string|float|int|bool|null $value |
||
125 | * |
||
126 | * @return Expr |
||
127 | */ |
||
128 | 1 | View Code Duplication | private function createScalarNodeQueryBuilderFactory($value): Expr |
142 | |||
143 | /** |
||
144 | * @param array $data |
||
145 | * |
||
146 | * @return Expr |
||
147 | */ |
||
148 | 4 | private function appendChildrenToArrayNode(array $data) |
|
172 | |||
173 | /** |
||
174 | * @param \stdClass $data |
||
175 | * |
||
176 | * @return Expr |
||
177 | */ |
||
178 | 13 | private function appendChildrenToObjectNode(\stdClass $data) |
|
202 | |||
203 | /** |
||
204 | * @param string $code |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 14 | private function structureCode(string $code): string |
|
225 | |||
226 | /** |
||
227 | * @param string $code |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 14 | private function getLinesByCode(string $code): array |
|
237 | |||
238 | /** |
||
239 | * @param string $line |
||
240 | * @param string $lastStructuredLine |
||
241 | * @param int $position |
||
242 | * @param array $structuredLines |
||
243 | */ |
||
244 | 14 | private function structuredLine(string $line, string $lastStructuredLine, int &$position, array &$structuredLines) |
|
276 | } |
||
277 |
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.