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 VenueSearchResult |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var Venue[] |
||
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 Venue[] $result |
||
36 | */ |
||
37 | private function __construct(array $result, int $page, int $pageSize, int $totalSize) |
||
44 | |||
45 | /** |
||
46 | * @return Venue[] |
||
47 | */ |
||
48 | public function getResult(): array |
||
52 | |||
53 | public function getPage(): int |
||
57 | |||
58 | public function getPageSize(): int |
||
62 | |||
63 | public function getTotalSize(): int |
||
67 | |||
68 | public function getLastPage(): int |
||
72 | |||
73 | /** |
||
74 | * @return VenueSearchResult |
||
75 | */ |
||
76 | public static function createEmpty(): self |
||
80 | |||
81 | /** |
||
82 | * @return VenueSearchResult |
||
83 | */ |
||
84 | public static function fromApi(array $response): self |
||
98 | } |
||
99 |
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.