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 |
||
18 | View Code Duplication | final class TaxonCollection implements CreatableFromArray |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $page; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $limit; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $pages; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $total; |
||
39 | |||
40 | /** |
||
41 | * @var Model[] |
||
42 | */ |
||
43 | private $items; |
||
44 | |||
45 | private function __construct( |
||
60 | |||
61 | /** |
||
62 | * @return ProductCollection |
||
63 | */ |
||
64 | public static function createFromArray(array $data): self |
||
96 | |||
97 | public function getPage(): int |
||
101 | |||
102 | public function getLimit(): int |
||
106 | |||
107 | public function getPages(): int |
||
111 | |||
112 | public function getTotal(): int |
||
116 | |||
117 | /** |
||
118 | * @return Model[] |
||
119 | */ |
||
120 | public function getItems(): array |
||
124 | } |
||
125 |
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.