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 |
||
4 | class DependencySolver |
||
5 | { |
||
6 | |||
7 | private $items = array(); |
||
8 | |||
9 | /** |
||
10 | * @param string $item |
||
11 | * @param string[] $dependencies |
||
12 | */ |
||
13 | public function add($item, $dependencies = array()) |
||
17 | |||
18 | /** |
||
19 | * @return array |
||
20 | */ |
||
21 | public function getLoadOrder() |
||
42 | |||
43 | /** |
||
44 | * @param $item |
||
45 | * @param array $seen |
||
46 | * @return array |
||
47 | */ |
||
48 | private function getDependents($item, $seen = array()) |
||
80 | |||
81 | /** |
||
82 | * @param $item |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function itemExists($item) |
||
93 | |||
94 | /** |
||
95 | * @param $item |
||
96 | * @return bool |
||
97 | */ |
||
98 | private function hasDependents($item) |
||
106 | |||
107 | /** |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getFailedItems() |
||
128 | } |
||
129 |
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.