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 |
||
5 | abstract class AbstractParent extends AbstractElement |
||
6 | { |
||
7 | /** |
||
8 | * @var AbstractElement[] |
||
9 | */ |
||
10 | protected $nodes = array(); |
||
11 | |||
12 | /** |
||
13 | * @return AbstractElement[] |
||
14 | */ |
||
15 | public function getNodes() |
||
19 | |||
20 | /** |
||
21 | * @param int|string $index node name or index, depends on object or array |
||
22 | * @return AbstractElement|null |
||
23 | */ |
||
24 | public function getNode($index) |
||
28 | |||
29 | /** |
||
30 | * @param AbstractElement $node |
||
31 | * @return void |
||
32 | */ |
||
33 | abstract public function addNode(AbstractElement $node); |
||
34 | |||
35 | /** |
||
36 | * @param AbstractElement $node |
||
37 | * @return void |
||
38 | */ |
||
39 | abstract public function removeNode(AbstractElement $node); |
||
40 | |||
41 | /** |
||
42 | * @param AbstractElement $node |
||
43 | * @throws \Exception |
||
44 | */ |
||
45 | View Code Duplication | protected function checkNode(AbstractElement $node) |
|
55 | |||
56 | /** |
||
57 | * @param AbstractElement $node |
||
58 | */ |
||
59 | protected function removeNodeFromParent(AbstractElement $node) |
||
65 | } |
||
66 |
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.