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 |
||
16 | final class QueryBuilder implements QueryBuilderInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var ObjectNode |
||
20 | */ |
||
21 | private $rootNode; |
||
22 | |||
23 | /** |
||
24 | * @var AbstractNode |
||
25 | */ |
||
26 | private $node; |
||
27 | |||
28 | 19 | public function __construct() |
|
33 | |||
34 | /** |
||
35 | * @param AbstractNode $node |
||
36 | * @param bool $allowDefault |
||
37 | * |
||
38 | * @return QueryBuilderInterface |
||
39 | * |
||
40 | * @throws \Exception |
||
41 | */ |
||
42 | 5 | View Code Duplication | public function addToArrayNode(AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface |
53 | |||
54 | /** |
||
55 | * @param string $key |
||
56 | * @param AbstractNode $node |
||
57 | * @param bool $allowDefault |
||
58 | * |
||
59 | * @return QueryBuilderInterface |
||
60 | * |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | 17 | View Code Duplication | public function addToObjectNode(string $key, AbstractNode $node, bool $allowDefault = false): QueryBuilderInterface |
74 | |||
75 | /** |
||
76 | * @param AbstractNode $node |
||
77 | */ |
||
78 | 17 | private function reassignParent(AbstractNode $node) |
|
84 | |||
85 | /** |
||
86 | * @return QueryBuilderInterface |
||
87 | * |
||
88 | * @throws \Exception |
||
89 | */ |
||
90 | 2 | public function end(): QueryBuilderInterface |
|
98 | |||
99 | /** |
||
100 | * @return ArrayNode |
||
101 | */ |
||
102 | 5 | public function arrayNode(): ArrayNode |
|
106 | |||
107 | /** |
||
108 | * @param bool|null |
||
109 | * @return BoolNode |
||
110 | */ |
||
111 | 1 | public function boolNode($value = null): BoolNode |
|
115 | |||
116 | /** |
||
117 | * @param float|null |
||
118 | * @return FloatNode |
||
119 | */ |
||
120 | 1 | public function floatNode($value = null): FloatNode |
|
124 | |||
125 | /** |
||
126 | * @param int|null |
||
127 | * @return IntNode |
||
128 | */ |
||
129 | 4 | public function intNode($value = null): IntNode |
|
133 | |||
134 | /** |
||
135 | * @return ObjectNode |
||
136 | */ |
||
137 | 16 | public function objectNode(): ObjectNode |
|
141 | |||
142 | /** |
||
143 | * @deprecated use boolNode|floatNode|intNode|stringNode |
||
144 | * @param string|float|int|bool|null $value |
||
145 | * @return ScalarNode |
||
146 | */ |
||
147 | 1 | public function scalarNode($value = null): ScalarNode |
|
151 | |||
152 | /** |
||
153 | * @param string|null |
||
154 | * @return StringNode |
||
155 | */ |
||
156 | 13 | public function stringNode($value = null): StringNode |
|
160 | |||
161 | /** |
||
162 | * @return \stdClass|null |
||
163 | */ |
||
164 | 16 | public function serialize() |
|
168 | |||
169 | /** |
||
170 | * @param boolean $beautify |
||
171 | * @return string |
||
172 | */ |
||
173 | 16 | public function json(bool $beautify = false): string |
|
185 | } |
||
186 |
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.