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 |
||
12 | View Code Duplication | final class ArtistSearchResult |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var Artist[] |
||
16 | */ |
||
17 | private $result; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $page; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $pageSize; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $totalSize; |
||
33 | |||
34 | /** |
||
35 | * @param Artist[] $result |
||
36 | * @param int $page |
||
37 | * @param int $pageSize |
||
38 | * @param int $totalSize |
||
39 | */ |
||
40 | private function __construct(array $result, int $page, int $pageSize, int $totalSize) |
||
47 | |||
48 | /** |
||
49 | * @return Artist[] |
||
50 | */ |
||
51 | public function getResult(): array |
||
55 | |||
56 | /** |
||
57 | * @return int |
||
58 | */ |
||
59 | public function getPage(): int |
||
63 | |||
64 | /** |
||
65 | * @return int |
||
66 | */ |
||
67 | public function getPageSize(): int |
||
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getTotalSize(): int |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getLastPage(): int |
||
87 | |||
88 | /** |
||
89 | * @return ArtistSearchResult |
||
90 | */ |
||
91 | public static function createEmpty(): self |
||
95 | |||
96 | /** |
||
97 | * @param array $response |
||
98 | * |
||
99 | * @return ArtistSearchResult |
||
100 | */ |
||
101 | public static function fromApi(array $response): self |
||
115 | } |
||
116 |
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.