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 GithubCommitAuthor |
|
|
|||
11 | { |
||
12 | /** @var string */ |
||
13 | private $name; |
||
14 | /** @var string */ |
||
15 | private $email; |
||
16 | /** @var GithubUserId */ |
||
17 | private $githubUserId; |
||
18 | /** @var string */ |
||
19 | private $username; |
||
20 | /** @var string */ |
||
21 | private $avatarUrl; |
||
22 | |||
23 | /** |
||
24 | * Author constructor. |
||
25 | * |
||
26 | * @param $name |
||
27 | * @param $email |
||
28 | * @param $githubUserId |
||
29 | * @param $username |
||
30 | * @param $avatarUrl |
||
31 | */ |
||
32 | public function __construct( |
||
45 | |||
46 | /** @return string */ |
||
47 | public function getName() : string |
||
51 | |||
52 | /** @return string */ |
||
53 | public function getEmail() : string |
||
57 | |||
58 | /** @return GithubUserId */ |
||
59 | public function getGithubUserId() : GithubUserId |
||
63 | |||
64 | /** @return string */ |
||
65 | public function getUsername() : string |
||
69 | |||
70 | /** @return string */ |
||
71 | public function getAvatarUrl() : string |
||
75 | |||
76 | /** @return array */ |
||
77 | public function serialize() |
||
87 | |||
88 | /** |
||
89 | * @param array $data |
||
90 | * |
||
91 | * @throws \Exception |
||
92 | * |
||
93 | * @return static |
||
94 | */ |
||
95 | public static function deserialize(array $data) |
||
105 | |||
106 | /** |
||
107 | * @param $id |
||
108 | * |
||
109 | * @return GithubUserId|null |
||
110 | */ |
||
111 | private static function unSerializeId($id) |
||
119 | } |
||
120 |
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.