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 |
||
23 | class Anime extends API { |
||
24 | |||
25 | // Display constants |
||
26 | const WATCHING = 'Watching'; |
||
27 | const PLAN_TO_WATCH = 'Plan to Watch'; |
||
28 | const DROPPED = 'Dropped'; |
||
29 | const ON_HOLD = 'On Hold'; |
||
30 | const COMPLETED = 'Completed'; |
||
31 | |||
32 | /** |
||
33 | * The base url for api requests |
||
34 | * @var string $base_url |
||
35 | */ |
||
36 | protected $base_url = "https://hummingbird.me/api/v1/"; |
||
37 | |||
38 | /** |
||
39 | * Map of API status constants to display constants |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $const_map = [ |
||
43 | AnimeWatchingStatus::WATCHING => self::WATCHING, |
||
44 | AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH, |
||
45 | AnimeWatchingStatus::ON_HOLD => self::ON_HOLD, |
||
46 | AnimeWatchingStatus::DROPPED => self::DROPPED, |
||
47 | AnimeWatchingStatus::COMPLETED => self::COMPLETED, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Update the selected anime |
||
52 | * |
||
53 | * @param array $data |
||
54 | * @return array|false |
||
55 | */ |
||
56 | View Code Duplication | public function update($data) |
|
76 | |||
77 | /** |
||
78 | * Remove an anime from a list |
||
79 | * |
||
80 | * @param array $data |
||
81 | * @return array |
||
82 | */ |
||
83 | View Code Duplication | public function delete($data) |
|
103 | |||
104 | /** |
||
105 | * Get the full set of anime lists |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function get_all_lists() |
||
134 | |||
135 | /** |
||
136 | * Get a category out of the full list |
||
137 | * |
||
138 | * @param string $status |
||
139 | * @return array |
||
140 | */ |
||
141 | public function get_list($status) |
||
151 | |||
152 | /** |
||
153 | * Get information about an anime from its id |
||
154 | * |
||
155 | * @param string $anime_id |
||
156 | * @return array |
||
157 | */ |
||
158 | public function get_anime($anime_id) |
||
170 | |||
171 | /** |
||
172 | * Search for anime by name |
||
173 | * |
||
174 | * @param string $name |
||
175 | * @return array |
||
176 | */ |
||
177 | public function search($name) |
||
199 | |||
200 | /** |
||
201 | * Retrieve data from the api |
||
202 | * |
||
203 | * @codeCoverageIgnore |
||
204 | * @param string $status |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function _get_list_from_api($status = "all") |
||
235 | |||
236 | /** |
||
237 | * Handle caching of transformed api data |
||
238 | * |
||
239 | * @codeCoverageIgnore |
||
240 | * @param string $status |
||
241 | * @param \GuzzleHttp\Message\Response |
||
242 | * @return array |
||
243 | */ |
||
244 | protected function _check_cache($status, $response) |
||
267 | } |
||
268 | // End of AnimeModel.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.