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 | class Wrapper |
||
19 | { |
||
20 | /** @var int */ |
||
21 | private $width = DimensionsInterface::DEFAULT_WIDTH; |
||
22 | |||
23 | /** |
||
24 | * Wrapper constructor. |
||
25 | * |
||
26 | * @param int $width |
||
27 | */ |
||
28 | public function __construct($width) |
||
32 | |||
33 | /** |
||
34 | * @return int |
||
35 | */ |
||
36 | public function getWidth() |
||
40 | |||
41 | /** |
||
42 | * @param int $width |
||
43 | * |
||
44 | * @return $this |
||
45 | */ |
||
46 | public function setWidth($width) |
||
51 | |||
52 | /** |
||
53 | * @param string[] $input |
||
54 | * |
||
55 | * @return string[] |
||
56 | */ |
||
57 | View Code Duplication | public function wrap(array $input) |
|
67 | |||
68 | /** |
||
69 | * @param string[] $input |
||
70 | * |
||
71 | * @return string[] |
||
72 | */ |
||
73 | View Code Duplication | public function trim(array $input) |
|
82 | |||
83 | /** |
||
84 | * @param string $line |
||
85 | * |
||
86 | * @return string[] |
||
87 | */ |
||
88 | private function chunk($line) |
||
116 | } |
||
117 |
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.