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 |
||
9 | class QueryBuilder |
||
10 | { |
||
11 | /** |
||
12 | * @var ObjectNode |
||
13 | */ |
||
14 | protected $rootNode; |
||
15 | |||
16 | /** |
||
17 | * @var AbstractNode |
||
18 | */ |
||
19 | protected $node; |
||
20 | |||
21 | 16 | public function __construct() |
|
26 | |||
27 | /** |
||
28 | * @param AbstractNode $node |
||
29 | * @param bool $allowDefault |
||
30 | * |
||
31 | * @return $this |
||
32 | * |
||
33 | * @throws \Exception |
||
34 | */ |
||
35 | 3 | View Code Duplication | public function addToArrayNode(AbstractNode $node, $allowDefault = false) |
46 | |||
47 | /** |
||
48 | * @param string $key |
||
49 | * @param AbstractNode $node |
||
50 | * @param bool $allowDefault |
||
51 | * |
||
52 | * @return $this |
||
53 | * |
||
54 | * @throws \Exception |
||
55 | */ |
||
56 | 15 | View Code Duplication | public function addToObjectNode($key, AbstractNode $node, $allowDefault = false) |
67 | |||
68 | /** |
||
69 | * @param AbstractNode $node |
||
70 | */ |
||
71 | 15 | protected function reassignParent(AbstractNode $node) |
|
77 | |||
78 | /** |
||
79 | * @return $this |
||
80 | */ |
||
81 | 1 | public function end() |
|
87 | |||
88 | /** |
||
89 | * @return \stdClass|null |
||
90 | */ |
||
91 | 14 | public function serialize() |
|
95 | |||
96 | /** |
||
97 | * @param boolean $beautify |
||
98 | * @return string |
||
99 | */ |
||
100 | 14 | public function json($beautify = false) |
|
112 | } |
||
113 |
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.