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 |
||
9 | View Code Duplication | class Phone |
|
|
|||
10 | { |
||
11 | /** |
||
12 | * @const int |
||
13 | */ |
||
14 | const CELLPHONE = 1; |
||
15 | |||
16 | /** |
||
17 | * @const int |
||
18 | */ |
||
19 | const TELEPHONE = 2; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $number; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $type; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param int $type |
||
35 | * @param int $number |
||
36 | */ |
||
37 | 5 | public function __construct(int $type = self::TELEPHONE, int $number = 0) |
|
42 | |||
43 | /** |
||
44 | * @return int |
||
45 | */ |
||
46 | 1 | public function getNumber(): int |
|
50 | |||
51 | /** |
||
52 | * @param int $number |
||
53 | */ |
||
54 | 1 | public function setNumber(int $number) |
|
58 | |||
59 | /** |
||
60 | * @return int |
||
61 | */ |
||
62 | 1 | public function getType(): int |
|
66 | |||
67 | /** |
||
68 | * @param int $type |
||
69 | */ |
||
70 | 1 | public function setType(int $type = self::CELLPHONE) |
|
74 | } |
||
75 |
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.