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 |
||
19 | class ShallowUser implements Model |
||
20 | { |
||
21 | const USER_TYPES = ['does_not_exist', 'moderator', 'registered', 'unregistered']; |
||
22 | |||
23 | protected $id; |
||
24 | protected $acceptRate; |
||
25 | protected $badgeCounts; |
||
26 | protected $displayName; |
||
27 | protected $link; |
||
28 | protected $profileImage; |
||
29 | protected $reputation; |
||
30 | protected $userType; |
||
31 | |||
32 | View Code Duplication | public static function fromJson(array $data) |
|
51 | |||
52 | public static function fromProperties( |
||
73 | |||
74 | public function getId() |
||
78 | |||
79 | public function setId($id) |
||
85 | |||
86 | public function getAcceptRate() |
||
90 | |||
91 | public function setAcceptRate($acceptRate) |
||
97 | |||
98 | public function getBadgeCounts() |
||
102 | |||
103 | public function setBadgeCounts($badgeCounts) |
||
109 | |||
110 | public function getDisplayName() |
||
114 | |||
115 | public function setDisplayName($displayName) |
||
121 | |||
122 | public function getLink() |
||
126 | |||
127 | public function setLink($link) |
||
133 | |||
134 | public function getProfileImage() |
||
138 | |||
139 | public function setProfileImage($profileImage) |
||
145 | |||
146 | public function getReputation() |
||
150 | |||
151 | public function setReputation($reputation) |
||
157 | |||
158 | public function getUserType() |
||
162 | |||
163 | public function setUserType($userType) |
||
171 | } |
||
172 |
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.