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 |
||
23 | final class QueryBuilderGenerator |
||
24 | { |
||
25 | /** |
||
26 | * @var PhpGenerator |
||
27 | */ |
||
28 | private $phpGenerator; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $useMethodName; |
||
34 | |||
35 | /** |
||
36 | * @param bool $useMethodName |
||
37 | * @param PhpGenerator $phpGenerator |
||
38 | */ |
||
39 | 14 | public function __construct(PhpGenerator $phpGenerator, bool $useMethodName = false) |
|
46 | |||
47 | /** |
||
48 | * @param $query |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | 14 | public function generateByJson($query): string |
|
68 | |||
69 | /** |
||
70 | * @return Expr |
||
71 | */ |
||
72 | 13 | private function createQueryBuilderNode(): Expr |
|
76 | |||
77 | /** |
||
78 | * @param Expr $expr |
||
79 | * |
||
80 | * @return Expr |
||
81 | */ |
||
82 | 13 | private function createObjectNode(Expr $expr): Expr |
|
86 | |||
87 | /** |
||
88 | * @param Expr $expr |
||
89 | * |
||
90 | * @return Expr |
||
91 | */ |
||
92 | 3 | private function createArrayNode(Expr $expr): Expr |
|
96 | |||
97 | /** |
||
98 | * @param Expr $expr |
||
99 | * @param string|float|int|bool|null $value |
||
100 | * |
||
101 | * @return Expr |
||
102 | */ |
||
103 | 12 | private function createScalarNode(Expr $expr, $value): Expr |
|
117 | |||
118 | /** |
||
119 | * @param Expr $queryBuilder |
||
120 | * @param Expr $expr |
||
121 | * @param array $data |
||
122 | * |
||
123 | * @return Expr |
||
124 | */ |
||
125 | 3 | private function appendChildrenToArrayNode(Expr $queryBuilder, Expr $expr, array $data) |
|
149 | |||
150 | /** |
||
151 | * @param Expr $queryBuilder |
||
152 | * @param Expr $expr |
||
153 | * @param \stdClass $data |
||
154 | * |
||
155 | * @return Expr |
||
156 | */ |
||
157 | 13 | private function appendChildrenToObjectNode(Expr $queryBuilder, Expr $expr, \stdClass $data) |
|
181 | |||
182 | /** |
||
183 | * @param string $code |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | 13 | private function structureCode(string $code): string |
|
202 | |||
203 | /** |
||
204 | * @param string $code |
||
205 | * |
||
206 | * @return array |
||
207 | */ |
||
208 | 13 | private function getLinesByCode(string $code): array |
|
215 | |||
216 | /** |
||
217 | * @param string $line |
||
218 | * @param string $lastLine |
||
219 | * @param int $position |
||
220 | * @param array $structuredLines |
||
221 | */ |
||
222 | 13 | private function structuredLine(string $line, string $lastLine, int &$position, array &$structuredLines) |
|
256 | } |
||
257 |
If you suppress an error, we recommend checking for the error condition explicitly: