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 QueryBuilderGenerator 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 QueryBuilderGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class QueryBuilderGenerator |
||
21 | { |
||
22 | /** |
||
23 | * @var PhpGenerator |
||
24 | */ |
||
25 | private $phpGenerator; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $useMethodName; |
||
31 | |||
32 | /** |
||
33 | * @param bool $useMethodName |
||
34 | * @param PhpGenerator $phpGenerator |
||
35 | */ |
||
36 | 14 | public function __construct(PhpGenerator $phpGenerator, bool $useMethodName = false) |
|
41 | |||
42 | /** |
||
43 | * @param $query |
||
44 | * @return string |
||
45 | */ |
||
46 | 14 | public function generateByJson($query): string |
|
62 | |||
63 | /** |
||
64 | * @return Expr |
||
65 | */ |
||
66 | 13 | private function createQueryBuilderNode(): Expr |
|
70 | |||
71 | /** |
||
72 | * @param Expr $expr |
||
73 | * @return Expr |
||
74 | */ |
||
75 | 13 | private function createObjectNode(Expr $expr): Expr |
|
79 | |||
80 | /** |
||
81 | * @param Expr $expr |
||
82 | * @return Expr |
||
83 | */ |
||
84 | 3 | private function createArrayNode(Expr $expr): Expr |
|
88 | |||
89 | /** |
||
90 | * @param Expr $expr |
||
91 | * @param string|float|int|bool|null $value |
||
92 | * @return Expr |
||
93 | */ |
||
94 | 12 | private function createScalarNode(Expr $expr, $value): Expr |
|
108 | |||
109 | /** |
||
110 | * @param Expr $queryBuilder |
||
111 | * @param Expr $expr |
||
112 | * @param array $data |
||
113 | * @return Expr |
||
114 | */ |
||
115 | 3 | private function appendChildrenToArrayNode(Expr $queryBuilder, Expr $expr, array $data) |
|
139 | |||
140 | /** |
||
141 | * @param Expr $queryBuilder |
||
142 | * @param Expr $expr |
||
143 | * @param \stdClass $data |
||
144 | * @return Expr |
||
145 | */ |
||
146 | 13 | private function appendChildrenToObjectNode(Expr $queryBuilder, Expr $expr, \stdClass $data) |
|
170 | |||
171 | /** |
||
172 | * @param string $code |
||
173 | * @return string |
||
174 | */ |
||
175 | 13 | private function structureCode(string $code): string |
|
190 | |||
191 | /** |
||
192 | * @param string $code |
||
193 | * @return array |
||
194 | */ |
||
195 | 13 | private function getLinesByCode(string $code): array |
|
202 | |||
203 | /** |
||
204 | * @param string $line |
||
205 | * @param string $lastLine |
||
206 | * @param int $position |
||
207 | * @param array $structuredLines |
||
208 | */ |
||
209 | 13 | private function structuredLine(string $line, string $lastLine, int &$position, array &$structuredLines) |
|
243 | } |
||
244 |
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.