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 |
||
5 | class Achievement |
||
6 | { |
||
7 | /** |
||
8 | * @var array |
||
9 | */ |
||
10 | protected $data = []; |
||
11 | |||
12 | /** |
||
13 | * Achievement constructor. |
||
14 | * |
||
15 | * @param array $data |
||
16 | */ |
||
17 | public function __construct(array $data) |
||
21 | |||
22 | /** |
||
23 | * Achievement ID. |
||
24 | * |
||
25 | * @return mixed|null |
||
26 | */ |
||
27 | public function id() |
||
31 | |||
32 | /** |
||
33 | * Achievement points. |
||
34 | * |
||
35 | * @return int |
||
36 | */ |
||
37 | public function points(): int |
||
41 | |||
42 | /** |
||
43 | * Achievement criterias. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | View Code Duplication | public function criterias(): array |
|
55 | |||
56 | /** |
||
57 | * Determines if achievement was already completed. |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function completed(): bool |
||
65 | |||
66 | /** |
||
67 | * Achievement criteria IDs. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function criteriaIds(): array |
||
77 | } |
||
78 |
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.