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 |
||
13 | abstract class InnerNode extends ArrayNode |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * An array of all the children. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $children = []; |
||
22 | |||
23 | /** |
||
24 | * Sets the encoding class to this node and propagates it |
||
25 | * to all its children. |
||
26 | * |
||
27 | * @param Encode $encode |
||
28 | * @return void |
||
29 | */ |
||
30 | public function propagateEncoding(Encode $encode) |
||
41 | |||
42 | /** |
||
43 | * Checks if this node has children. |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function hasChildren() |
||
51 | |||
52 | /** |
||
53 | * Returns the child by id. |
||
54 | * |
||
55 | * @param int $id |
||
56 | * @return AbstractNode |
||
57 | * @throws ChildNotFoundException |
||
58 | */ |
||
59 | public function getChild($id) |
||
67 | |||
68 | /** |
||
69 | * Returns a new array of child nodes |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getChildren() |
||
88 | |||
89 | /** |
||
90 | * Counts children |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | public function countChildren() |
||
98 | |||
99 | /** |
||
100 | * Adds a child node to this node and returns the id of the child for this |
||
101 | * parent. |
||
102 | * |
||
103 | * @param AbstractNode $child |
||
104 | * @return bool |
||
105 | * @throws CircularException |
||
106 | */ |
||
107 | public function addChild(AbstractNode $child) |
||
146 | |||
147 | /** |
||
148 | * Removes the child by id. |
||
149 | * |
||
150 | * @param int $id |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function removeChild($id) |
||
177 | |||
178 | /** |
||
179 | * Attempts to get the next child. |
||
180 | * |
||
181 | * @param int $id |
||
182 | * @return AbstractNode |
||
183 | * @uses $this->getChild() |
||
184 | * @throws ChildNotFoundException |
||
185 | */ |
||
186 | View Code Duplication | public function nextChild($id) |
|
193 | |||
194 | /** |
||
195 | * Attempts to get the previous child. |
||
196 | * |
||
197 | * @param int $id |
||
198 | * @return AbstractNode |
||
199 | * @uses $this->getChild() |
||
200 | * @throws ChildNotFoundException |
||
201 | */ |
||
202 | View Code Duplication | public function previousChild($id) |
|
209 | |||
210 | /** |
||
211 | * Checks if the given node id is a child of the |
||
212 | * current node. |
||
213 | * |
||
214 | * @param int $id |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function isChild($id) |
||
227 | |||
228 | /** |
||
229 | * Removes the child with id $childId and replace it with the new child |
||
230 | * $newChild. |
||
231 | * |
||
232 | * @param int $childId |
||
233 | * @param AbstractNode $newChild |
||
234 | * @throws ChildNotFoundException |
||
235 | */ |
||
236 | public function replaceChild($childId, AbstractNode $newChild) |
||
246 | |||
247 | /** |
||
248 | * Checks if the given node id is a descendant of the |
||
249 | * current node. |
||
250 | * |
||
251 | * @param int $id |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function isDescendant($id) |
||
273 | |||
274 | /** |
||
275 | * Sets the parent node. |
||
276 | * |
||
277 | * @param InnerNode $parent |
||
278 | * @return $this |
||
279 | * @throws CircularException |
||
280 | */ |
||
281 | public function setParent(InnerNode $parent) |
||
290 | } |
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.