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 |
||
20 | final class QueryBuilderGenerator |
||
21 | { |
||
22 | /** |
||
23 | * @var PhpGenerator |
||
24 | */ |
||
25 | private $phpGenerator; |
||
26 | |||
27 | /** |
||
28 | * @param PhpGenerator $phpGenerator |
||
29 | */ |
||
30 | public function __construct(PhpGenerator $phpGenerator) |
||
31 | { |
||
32 | $this->phpGenerator = $phpGenerator; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param $query |
||
37 | * @return string |
||
38 | */ |
||
39 | public function generateByJson($query): string |
||
40 | { |
||
41 | $data = json_decode($query, false); |
||
42 | if (JSON_ERROR_NONE !== json_last_error()) { |
||
43 | throw new \InvalidArgumentException(sprintf('Message: %s, query: %s', json_last_error_msg(), $query)); |
||
44 | } |
||
45 | |||
46 | $queryBuilder = new Variable('queryBuilder'); |
||
47 | |||
48 | $stmts = []; |
||
49 | |||
50 | $stmts[] = $this->createQueryBuilderNode(); |
||
51 | $stmts[] = $this->appendChildrenToObjectNode($queryBuilder, $queryBuilder, $data); |
||
52 | |||
53 | return $this->structureCode($this->phpGenerator->prettyPrint($stmts)); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $code |
||
58 | * @return string |
||
59 | */ |
||
60 | private function structureCode(string $code): string |
||
61 | { |
||
62 | $codeWithLinebreaks = str_replace('->add', "\n->add", $code); |
||
63 | $codeWithLinebreaks = str_replace('->end', "\n->end", $codeWithLinebreaks); |
||
64 | |||
65 | $lines = explode("\n", $codeWithLinebreaks); |
||
66 | |||
67 | $position = 0; |
||
68 | |||
69 | $structuredLines = []; |
||
70 | |||
71 | foreach ($lines as $i => $line) { |
||
72 | $lastLine = $lines[$i-1] ?? ''; |
||
73 | if (0 === strpos($line, '->add')) { |
||
74 | if (false === strpos($lastLine, '->end') && false === strpos($lastLine, '->scalarNode')) { |
||
75 | $position++; |
||
76 | } |
||
77 | $structuredLines[] = str_pad('', $position * 4) . $line; |
||
78 | } elseif (0 === strpos($line, '->end')) { |
||
79 | if (strpos($lastLine, '->objectNode') || strpos($lastLine, '->arrayNode')) { |
||
80 | $structuredLines[count($structuredLines) - 1] .= '->end()'; |
||
81 | } else { |
||
82 | $position--; |
||
83 | $structuredLines[] = str_pad('', $position * 4) . $line; |
||
84 | } |
||
85 | } else { |
||
86 | $structuredLines[] = $line; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return implode("\n", $structuredLines); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return Expr |
||
95 | */ |
||
96 | private function createQueryBuilderNode(): Expr |
||
100 | |||
101 | /** |
||
102 | * @param Expr $expr |
||
103 | * @return Expr |
||
104 | */ |
||
105 | private function createObjectNode(Expr $expr): Expr |
||
109 | |||
110 | /** |
||
111 | * @param Expr $expr |
||
112 | * @return Expr |
||
113 | */ |
||
114 | private function createArrayNode(Expr $expr): Expr |
||
118 | |||
119 | /** |
||
120 | * @param Expr $expr |
||
121 | * @param string|float|int|bool|null $value |
||
122 | * @return Expr |
||
123 | */ |
||
124 | View Code Duplication | private function createScalarNode(Expr $expr, $value): Expr |
|
140 | |||
141 | /** |
||
142 | * @param Expr $queryBuilder |
||
143 | * @param Expr $expr |
||
144 | * @param array $data |
||
145 | * @return Expr |
||
146 | */ |
||
147 | private function appendChildrenToArrayNode(Expr $queryBuilder, Expr $expr, array $data) |
||
169 | |||
170 | /** |
||
171 | * @param Expr $queryBuilder |
||
172 | * @param Expr $expr |
||
173 | * @param \stdClass $data |
||
174 | * @return Expr |
||
175 | */ |
||
176 | private function appendChildrenToObjectNode(Expr $queryBuilder, Expr $expr, \stdClass $data) |
||
198 | } |
||
199 |
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.