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 |
||
17 | View Code Duplication | final class Variant implements CreatableFromArray |
|
|
|||
18 | { |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $id; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $code; |
||
28 | |||
29 | /** |
||
30 | * @var string[][] |
||
31 | */ |
||
32 | private $translations; |
||
33 | |||
34 | /** |
||
35 | * @var string[][] |
||
36 | */ |
||
37 | private $channelPricings; |
||
38 | |||
39 | /** |
||
40 | * Variant constructor. |
||
41 | * |
||
42 | * @param string[][] $translations |
||
43 | */ |
||
44 | 1 | private function __construct( |
|
55 | |||
56 | /** |
||
57 | * @return Variant |
||
58 | */ |
||
59 | 1 | public static function createFromArray(array $data): self |
|
83 | |||
84 | 1 | public function getId(): int |
|
88 | |||
89 | public function getCode(): string |
||
93 | |||
94 | /** |
||
95 | * @return \string[][] |
||
96 | */ |
||
97 | public function getTranslations(): array |
||
101 | |||
102 | /** |
||
103 | * @return \string[][] |
||
104 | */ |
||
105 | public function getChannelPricings(): array |
||
109 | } |
||
110 |
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.