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 | class PackagesCollection extends ArrayObject |
||
17 | { |
||
18 | |||
19 | /** @var array */ |
||
20 | private $indexedBy; |
||
21 | |||
22 | /** |
||
23 | * @param string $name |
||
24 | * |
||
25 | * @return Package |
||
26 | */ |
||
27 | public function getByName(string $name): Package |
||
34 | |||
35 | /** |
||
36 | * @param string $namespace |
||
37 | * |
||
38 | * @return Package |
||
39 | */ |
||
40 | public function getByNamespace(string $namespace): Package |
||
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | public function hasByName(string $name): bool |
||
57 | |||
58 | /** |
||
59 | * @param string $namespace |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function hasByNamespace($namespace): bool |
||
67 | |||
68 | public function offsetSet($index, $package) |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | View Code Duplication | private function getIndexedByName(): array |
|
94 | |||
95 | /** |
||
96 | * @return array |
||
97 | */ |
||
98 | View Code Duplication | private function getIndexedByNamespace(): array |
|
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.