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 Badge implements Model |
||
20 | { |
||
21 | const BADGE_TYPES = ['named', 'tag_based']; |
||
22 | const RANKS = ['gold', 'silver', 'bronze']; |
||
23 | |||
24 | protected $id; |
||
25 | protected $awardCount; |
||
26 | protected $badgeType; |
||
27 | protected $description; |
||
28 | protected $link; |
||
29 | protected $name; |
||
30 | protected $rank; |
||
31 | protected $user; |
||
32 | |||
33 | View Code Duplication | public static function fromJson(array $data) |
|
48 | |||
49 | public static function fromProperties( |
||
72 | |||
73 | public function setId($id) |
||
79 | |||
80 | public function getId() |
||
84 | |||
85 | public function setAwardCount($awardCount) |
||
91 | |||
92 | public function getAwardCount() |
||
96 | |||
97 | public function setBadgeType($badgeType) |
||
105 | |||
106 | public function getBadgeType() |
||
110 | |||
111 | public function setDescription($description) |
||
117 | |||
118 | public function getDescription() |
||
122 | |||
123 | public function setLink($link) |
||
129 | |||
130 | public function getLink() |
||
134 | |||
135 | public function setName($name) |
||
141 | |||
142 | public function getName() |
||
146 | |||
147 | public function setRank($rank) |
||
155 | |||
156 | public function getRank() |
||
160 | |||
161 | public function setUser(ShallowUser $user = null) |
||
167 | |||
168 | public function getUser() |
||
172 | } |
||
173 |
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.