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 OnlineRecord |
|
|
|||
11 | { |
||
12 | use ImmutableTrait, SerializableTrait; |
||
13 | |||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $number; |
||
18 | |||
19 | /** |
||
20 | * @var Carbon |
||
21 | */ |
||
22 | private $date; |
||
23 | |||
24 | /** |
||
25 | * OnlineRecord constructor. |
||
26 | * @param int $number |
||
27 | * @param Carbon $date |
||
28 | * @throws ImmutableException |
||
29 | */ |
||
30 | public function __construct(int $number, Carbon $date) |
||
37 | |||
38 | /** |
||
39 | * @return int |
||
40 | */ |
||
41 | public function getNumber(): int |
||
45 | |||
46 | /** |
||
47 | * @return Carbon |
||
48 | */ |
||
49 | public function getDate(): Carbon |
||
53 | } |
||
54 |
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.