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 | class Manga extends API { |
||
28 | |||
29 | const READING = 'Reading'; |
||
30 | const PLAN_TO_READ = 'Plan to Read'; |
||
31 | const DROPPED = 'Dropped'; |
||
32 | const ON_HOLD = 'On Hold'; |
||
33 | const COMPLETED = 'Completed'; |
||
34 | |||
35 | /** |
||
36 | * Map API constants to display constants |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $const_map = [ |
||
40 | MangaReadingStatus::READING => self::READING, |
||
41 | MangaReadingStatus::PLAN_TO_READ => self::PLAN_TO_READ, |
||
42 | MangaReadingStatus::ON_HOLD => self::ON_HOLD, |
||
43 | MangaReadingStatus::DROPPED => self::DROPPED, |
||
44 | MangaReadingStatus::COMPLETED => self::COMPLETED |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * The base url for api requests |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $base_url = "https://hummingbird.me/"; |
||
52 | |||
53 | protected function _manga_api_call($type, $url, $json=null) |
||
84 | |||
85 | /** |
||
86 | * Add a manga to the list |
||
87 | * |
||
88 | * @param array $data |
||
89 | */ |
||
90 | public function add($data) |
||
101 | |||
102 | /** |
||
103 | * Update the selected manga |
||
104 | * |
||
105 | * @param array $data |
||
106 | * @return array |
||
107 | */ |
||
108 | public function update($data) |
||
118 | |||
119 | /** |
||
120 | * Delete a manga entry |
||
121 | * |
||
122 | * @param array $data |
||
123 | * @return array |
||
124 | */ |
||
125 | public function delete($data) |
||
131 | |||
132 | /** |
||
133 | * Search for manga by name |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @return array |
||
137 | */ |
||
138 | public function search($name) |
||
162 | |||
163 | /** |
||
164 | * Get the full set of anime lists |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function get_all_lists() |
||
179 | |||
180 | /** |
||
181 | * Get a category out of the full list |
||
182 | * |
||
183 | * @param string $status |
||
184 | * @return array |
||
185 | */ |
||
186 | public function get_list($status) |
||
193 | |||
194 | /** |
||
195 | * Retrieve the list from the hummingbird api |
||
196 | * |
||
197 | * @param string $status |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function _get_list_from_api($status = "All") |
||
217 | |||
218 | /** |
||
219 | * Check the status of the cache and return the appropriate response |
||
220 | * |
||
221 | * @param \GuzzleHttp\Message\Response $response |
||
222 | * @codeCoverageIgnore |
||
223 | * @return array |
||
224 | */ |
||
225 | private function _check_cache($response) |
||
259 | |||
260 | /** |
||
261 | * Map transformed anime data to be organized by reading status |
||
262 | * |
||
263 | * @param array $data |
||
264 | * @return array |
||
265 | */ |
||
266 | private function map_by_status($data) |
||
289 | |||
290 | /** |
||
291 | * Combine the two manga lists into one |
||
292 | * @param array $raw_data |
||
293 | * @return array |
||
294 | */ |
||
295 | private function zipper_lists($raw_data) |
||
299 | } |
||
300 | // End of MangaModel.php |
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.