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 |
||
10 | View Code Duplication | class BrickHouse implements HouseInterface |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * @var int |
||
14 | */ |
||
15 | private $width; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $height; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $material; |
||
26 | |||
27 | 1 | public function __construct(int $width, int $height) |
|
33 | |||
34 | /** |
||
35 | * @return int |
||
36 | */ |
||
37 | 1 | public function getHeight(): int |
|
41 | |||
42 | /** |
||
43 | * @return int |
||
44 | */ |
||
45 | 1 | public function getWidth(): int |
|
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | public function getMaterial(): string |
||
57 | } |
||
58 |
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.