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 |
||
22 | class Anime extends API { |
||
23 | |||
24 | // Display constants |
||
25 | const WATCHING = 'Watching'; |
||
26 | const PLAN_TO_WATCH = 'Plan to Watch'; |
||
27 | const DROPPED = 'Dropped'; |
||
28 | const ON_HOLD = 'On Hold'; |
||
29 | const COMPLETED = 'Completed'; |
||
30 | |||
31 | /** |
||
32 | * The base url for api requests |
||
33 | * @var string $base_url |
||
34 | */ |
||
35 | protected $base_url = "https://hummingbird.me/api/v1/"; |
||
36 | |||
37 | /** |
||
38 | * Map of API status constants to display constants |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $const_map = [ |
||
42 | AnimeWatchingStatus::WATCHING => self::WATCHING, |
||
43 | AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH, |
||
44 | AnimeWatchingStatus::ON_HOLD => self::ON_HOLD, |
||
45 | AnimeWatchingStatus::DROPPED => self::DROPPED, |
||
46 | AnimeWatchingStatus::COMPLETED => self::COMPLETED, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Update the selected anime |
||
51 | * |
||
52 | * @param array $data |
||
53 | * @return array|false |
||
54 | */ |
||
55 | public function update($data) |
||
77 | |||
78 | /** |
||
79 | * Get the full set of anime lists |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | public function get_all_lists() |
||
108 | |||
109 | /** |
||
110 | * Get a category out of the full list |
||
111 | * |
||
112 | * @param string $status |
||
113 | * @return array |
||
114 | */ |
||
115 | public function get_list($status) |
||
125 | |||
126 | /** |
||
127 | * Get the data for the specified library entry |
||
128 | * |
||
129 | * @param string $id |
||
130 | * @param string $status |
||
131 | * @return array |
||
132 | */ |
||
133 | public function get_library_anime($id, $status) |
||
144 | |||
145 | /** |
||
146 | * Get information about an anime from its id |
||
147 | * |
||
148 | * @param string $anime_id |
||
149 | * @return array |
||
150 | */ |
||
151 | public function get_anime($anime_id) |
||
163 | |||
164 | /** |
||
165 | * Search for anime by name |
||
166 | * |
||
167 | * @param string $name |
||
168 | * @return array |
||
169 | */ |
||
170 | public function search($name) |
||
190 | |||
191 | /** |
||
192 | * Retrieve data from the api |
||
193 | * |
||
194 | * @codeCoverageIgnore |
||
195 | * @param string $status |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function _get_list_from_api($status = "all") |
||
226 | |||
227 | /** |
||
228 | * Handle caching of transformed api data |
||
229 | * |
||
230 | * @codeCoverageIgnore |
||
231 | * @param string $status |
||
232 | * @param \GuzzleHttp\Message\Response |
||
233 | * @return array |
||
234 | */ |
||
235 | protected function _check_cache($status, $response) |
||
258 | |||
259 | /** |
||
260 | * Sort the list by title |
||
261 | * |
||
262 | * @codeCoverageIgnore |
||
263 | * @param array $array |
||
264 | * @return void |
||
265 | */ |
||
266 | View Code Duplication | protected function sort_by_name(&$array) |
|
277 | } |
||
278 | // End of AnimeModel.php |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.