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 |
||
9 | class Pins extends SearchProvider |
||
10 | { |
||
11 | /** |
||
12 | * Likes pin with current ID |
||
13 | * |
||
14 | * @param integer $pinId |
||
15 | * @return bool |
||
16 | */ |
||
17 | public function like($pinId) |
||
21 | |||
22 | /** |
||
23 | * Removes your like from pin with current ID |
||
24 | * |
||
25 | * @param integer $pinId |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function unLike($pinId) |
||
32 | |||
33 | /** |
||
34 | * Calls pinterest API to like or unlike Pin by ID |
||
35 | * |
||
36 | * @param $pinId |
||
37 | * @param $url |
||
38 | * @return bool |
||
39 | */ |
||
40 | protected function likePinMethodCall($pinId, $url) |
||
49 | |||
50 | /** |
||
51 | * Writes comment for pin with current id |
||
52 | * |
||
53 | * @param integer $pinId |
||
54 | * @param string $text Comment |
||
55 | * @return array |
||
56 | */ |
||
57 | View Code Duplication | public function comment($pinId, $text) |
|
65 | |||
66 | /** |
||
67 | * Writes comment for pin with current id |
||
68 | * |
||
69 | * @param integer $pinId |
||
70 | * @param integer $commentId |
||
71 | * @return bool |
||
72 | */ |
||
73 | View Code Duplication | public function deleteComment($pinId, $commentId) |
|
82 | |||
83 | /** |
||
84 | * Create pin. Returns created pin ID |
||
85 | * |
||
86 | * @param string $imageUrl |
||
87 | * @param int $boardId |
||
88 | * @param string $description |
||
89 | * @return bool|int |
||
90 | */ |
||
91 | public function create($imageUrl, $boardId, $description = "") |
||
100 | |||
101 | /** |
||
102 | * Repin |
||
103 | * |
||
104 | * @param int $repinId |
||
105 | * @param int $boardId |
||
106 | * @param string $description |
||
107 | * @return bool|int |
||
108 | */ |
||
109 | public function repin($repinId, $boardId, $description = "") |
||
119 | |||
120 | /** |
||
121 | * Delete pin |
||
122 | * |
||
123 | * @param int $pinId |
||
124 | * @return bool |
||
125 | */ |
||
126 | View Code Duplication | public function delete($pinId) |
|
135 | |||
136 | /** |
||
137 | * Get information of pin by PinID |
||
138 | * |
||
139 | * @param $pinId |
||
140 | * @return array|bool |
||
141 | */ |
||
142 | View Code Duplication | public function info($pinId) |
|
150 | |||
151 | |||
152 | protected function getScope() |
||
156 | } |
||
157 |
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.