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 |
||
27 | View Code Duplication | final class PostWithAuthorDto implements ConstructableFromArrayInterface |
|
|
|||
28 | { |
||
29 | use ConstructableFromArrayTrait; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $title; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $slug; |
||
40 | |||
41 | /** |
||
42 | * @var UserId |
||
43 | */ |
||
44 | private $authorId; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $authorMobile; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $authorFullName; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $authorEmail; |
||
60 | |||
61 | /** |
||
62 | * @var PostId |
||
63 | */ |
||
64 | private $id; |
||
65 | |||
66 | /** |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function __construct( |
||
86 | |||
87 | public function getId(): PostId |
||
91 | |||
92 | public function getTitle(): string |
||
96 | |||
97 | public function getSlug(): string |
||
101 | |||
102 | public function getAuthorId(): UserId |
||
106 | |||
107 | public function getAuthorMobile(): string |
||
111 | |||
112 | public function getAuthorFullName(): string |
||
116 | |||
117 | public function getAuthorEmail(): string |
||
121 | } |
||
122 |
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.