| Total Complexity | 66 |
| Total Lines | 457 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like InfoModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InfoModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class InfoModel extends MainModel |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Type of info. Either anime or manga. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $_type; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Id of the anime or manga. |
||
| 22 | * |
||
| 23 | * @var string|int |
||
| 24 | */ |
||
| 25 | private $_id; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default constructor. |
||
| 29 | * |
||
| 30 | * @param string $type |
||
| 31 | * @param string|int $id |
||
| 32 | * @param string $parserArea |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function __construct($type, $id, $parserArea = '#content') |
||
| 37 | { |
||
| 38 | $this->_type = $type; |
||
| 39 | $this->_id = $id; |
||
| 40 | $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id; |
||
| 41 | $this->_parserArea = $parserArea; |
||
| 42 | |||
| 43 | parent::errorCheck($this); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Default call. |
||
| 48 | * |
||
| 49 | * @param string $method |
||
| 50 | * @param array $arguments |
||
| 51 | * |
||
| 52 | * @return array|string|int |
||
| 53 | */ |
||
| 54 | public function __call($method, $arguments) |
||
| 55 | { |
||
| 56 | if ($this->_error) |
||
| 57 | return $this->_error; |
||
| 58 | return call_user_func_array([$this, $method], $arguments); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get anime/manga id. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | private function getId() |
||
| 67 | { |
||
| 68 | return $this->_id; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get anime/manga cover. |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | private function getCover() |
||
| 77 | { |
||
| 78 | $anime_cover = $this->_parser->find('img.ac', 0); |
||
| 79 | return $anime_cover ? $anime_cover->src : ''; |
||
|
|
|||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get anime/manga title. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | private function getTitle() |
||
| 88 | { |
||
| 89 | $anime_cover = $this->_parser->find('img.ac', 0); |
||
| 90 | return $anime_cover ? $anime_cover->alt : ''; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get anime/manga alternative title. |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | private function getTitle2() |
||
| 99 | { |
||
| 100 | $title2 = []; |
||
| 101 | |||
| 102 | $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0); |
||
| 103 | |||
| 104 | preg_match('/(English:<\/span>)([^<]*)/', $anime_info->innertext, $english); |
||
| 105 | $title2['english'] = trim($english ? $english[2] : ''); |
||
| 106 | |||
| 107 | preg_match('/(Synonyms:<\/span>)([^<]*)/', $anime_info->innertext, $synonym); |
||
| 108 | $title2['synonym'] = trim($synonym ? $synonym[2] : ''); |
||
| 109 | |||
| 110 | preg_match('/(Japanese:<\/span>)([^<]*)/', $anime_info->innertext, $japanese); |
||
| 111 | $title2['japanese'] = trim($japanese ? $japanese[2] : ''); |
||
| 112 | |||
| 113 | return $title2; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get anime/manga synopsis. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | private function getSynopsis() |
||
| 122 | { |
||
| 123 | $synopsis = $this->_parser->find('span[itemprop=description]', 0); |
||
| 124 | if ($synopsis) { |
||
| 125 | $synopsis = $synopsis->plaintext; |
||
| 126 | return trim(preg_replace('/\n[^\S\n]*/', "\n", $synopsis)); |
||
| 127 | } else { |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get anime/manga score. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | private function getScore() |
||
| 138 | { |
||
| 139 | $score = $this->_parser->find('div[class="fl-l score"]', 0)->plaintext; |
||
| 140 | $score = trim($score); |
||
| 141 | return $score != 'N/A' ? $score : null; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get number of user who give score. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function getVoter() |
||
| 150 | { |
||
| 151 | $voter = $this->_parser->find('div[class="fl-l score"]', 0)->getAttribute('data-user'); |
||
| 152 | return trim(str_replace(['users', 'user', ','], '', $voter)); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get anime/manga rank. |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | private function getRank() |
||
| 161 | { |
||
| 162 | $rank = $this->_parser->find('span[class="numbers ranked"] strong', 0)->plaintext; |
||
| 163 | $rank = $rank != 'N/A' ? $rank : ''; |
||
| 164 | return str_replace('#', '', $rank); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get anime/manga popularity. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | private function getPopularity() |
||
| 173 | { |
||
| 174 | $popularity = $this->_parser->find('span[class="numbers popularity"] strong', 0)->plaintext; |
||
| 175 | return str_replace('#', '', $popularity); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get number of user who watch/read the anime/manga. |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | private function getMembers() |
||
| 184 | { |
||
| 185 | $member = $this->_parser->find('span[class="numbers members"] strong', 0)->plaintext; |
||
| 186 | return str_replace(',', '', $member); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get number of user who favorite the anime/manga. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | private function getFavorite() |
||
| 195 | { |
||
| 196 | $favorite = $this->_parser->find('div[data-id=info2]', 0)->next_sibling()->next_sibling()->next_sibling(); |
||
| 197 | $favorite_title = $favorite->find('span', 0)->plaintext; |
||
| 198 | $favorite = $favorite->plaintext; |
||
| 199 | $favorite = trim(str_replace($favorite_title, '', $favorite)); |
||
| 200 | $favorite = str_replace(',', '', $favorite); |
||
| 201 | return preg_replace("/([\s])+/", ' ', $favorite); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get anime/manga detail info. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | private function getOtherInfo() |
||
| 210 | { |
||
| 211 | $info = []; |
||
| 212 | |||
| 213 | $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0); |
||
| 214 | $other_info = (count($anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0); |
||
| 215 | $next_info = $other_info->next_sibling(); |
||
| 216 | while (true) { |
||
| 217 | $info_type = $next_info->find('span', 0)->plaintext; |
||
| 218 | $clean_info_type = strtolower(str_replace(': ', '', $info_type)); |
||
| 219 | |||
| 220 | $info_value = $next_info->plaintext; |
||
| 221 | $clean_info_value = trim(str_replace($info_type, '', $info_value)); |
||
| 222 | $clean_info_value = preg_replace("/([\s])+/", ' ', $clean_info_value); |
||
| 223 | $clean_info_value = str_replace([', add some', '?', 'Not yet aired', 'Unknown'], '', $clean_info_value); |
||
| 224 | |||
| 225 | if ($clean_info_type == 'published' || $clean_info_type == 'aired') { |
||
| 226 | $start_air = ''; |
||
| 227 | $end_air = ''; |
||
| 228 | if ($clean_info_value != 'Not available') { |
||
| 229 | $parsed_airing = explode(' to ', $clean_info_value); |
||
| 230 | |||
| 231 | $start_air = ($parsed_airing[0] != '?') ? date('Y-m-d', strtotime($parsed_airing[0])) : ''; |
||
| 232 | if (count($parsed_airing) > 1) { |
||
| 233 | $end_air = ($parsed_airing[1] != '?') ? date('Y-m-d', strtotime($parsed_airing[1])) : ''; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | $clean_info_value = []; |
||
| 238 | $clean_info_value['start'] = $start_air; |
||
| 239 | $clean_info_value['end'] = $end_air; |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($clean_info_type == 'producers' |
||
| 243 | || $clean_info_type == 'licensors' |
||
| 244 | || $clean_info_type == 'studios' |
||
| 245 | || $clean_info_type == 'genres' |
||
| 246 | || $clean_info_type == 'authors' |
||
| 247 | ) { |
||
| 248 | $info_temp = []; |
||
| 249 | $info_temp_index = 0; |
||
| 250 | if ($clean_info_value != 'None found') { |
||
| 251 | foreach ($next_info->find('a') as $each_info) { |
||
| 252 | $temp_id = explode('/', $each_info->href); |
||
| 253 | $info_temp[$info_temp_index]['id'] = $clean_info_type == 'authors' ? $temp_id[2] : $temp_id[3]; |
||
| 254 | $info_temp[$info_temp_index]['name'] = $each_info->plaintext; |
||
| 255 | $info_temp_index++; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | $clean_info_value = $info_temp; |
||
| 259 | } |
||
| 260 | |||
| 261 | $info[$clean_info_type] = $clean_info_value; |
||
| 262 | |||
| 263 | $next_info = $next_info->next_sibling(); |
||
| 264 | if ($next_info->tag == 'h2' || $next_info->tag == 'br') { |
||
| 265 | break; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | return $info; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get anime/manga relation. |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | private function getRelated() |
||
| 277 | { |
||
| 278 | $related = []; |
||
| 279 | $related_area = $this->_parser->find('.anime_detail_related_anime', 0); |
||
| 280 | if ($related_area) { |
||
| 281 | foreach ($related_area->find('tr') as $rel) { |
||
| 282 | $rel_type = $rel->find('td', 0)->plaintext; |
||
| 283 | $rel_type = trim(strtolower(str_replace(':', '', $rel_type))); |
||
| 284 | |||
| 285 | $each_rel = []; |
||
| 286 | $each_rel_index = 0; |
||
| 287 | $rel_anime = $rel->find('td', 1); |
||
| 288 | foreach ($rel_anime->find('a') as $r) { |
||
| 289 | $rel_anime_link = $r->href; |
||
| 290 | $separated_anime_link = explode('/', $rel_anime_link); |
||
| 291 | |||
| 292 | $each_rel[$each_rel_index]['id'] = $separated_anime_link[2]; |
||
| 293 | $each_rel[$each_rel_index]['title'] = $r->plaintext; |
||
| 294 | $each_rel[$each_rel_index]['type'] = $separated_anime_link[1]; |
||
| 295 | |||
| 296 | $each_rel_index++; |
||
| 297 | } |
||
| 298 | |||
| 299 | $related[$rel_type] = $each_rel; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | return $related; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get anime/manga character and its va. |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private function getCharacter() |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get anime/manga staff involved. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | private function getStaff() |
||
| 372 | { |
||
| 373 | $staff = []; |
||
| 374 | $staff_index = 0; |
||
| 375 | $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1); |
||
| 376 | if ($staff_area) { |
||
| 377 | $staff_list = [ |
||
| 378 | $staff_area->find('div[class*=fl-l]', 0), |
||
| 379 | $staff_area->find('div[class*=fl-r]', 0) |
||
| 380 | ]; |
||
| 381 | foreach ($staff_list as $staff_side) { |
||
| 382 | if ($staff_side) { |
||
| 383 | foreach ($staff_side->find('table[width=100%]') as $each_staff) { |
||
| 384 | $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src'); |
||
| 385 | $staff_image = Helper::imageUrlCleaner($staff_image); |
||
| 386 | |||
| 387 | $st = $each_staff->find('tr td', 1); |
||
| 388 | |||
| 389 | $staff_id = $st->find('a', 0)->href; |
||
| 390 | $staff_id = explode('/', $staff_id); |
||
| 391 | $staff_id = $staff_id[4]; |
||
| 392 | |||
| 393 | $staff_name = trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext)); |
||
| 394 | $staff_role = trim($st->find('small', 0)->plaintext); |
||
| 395 | |||
| 396 | $staff[$staff_index]['id'] = $staff_id; |
||
| 397 | $staff[$staff_index]['name'] = $staff_name; |
||
| 398 | $staff[$staff_index]['role'] = $staff_role; |
||
| 399 | $staff[$staff_index]['image'] = $staff_image; |
||
| 400 | |||
| 401 | $staff_index++; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | return $staff; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get anime/manga opening and ending song. |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | private function getSong() |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get anime/manga all information. |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | private function getAllInfo() |
||
| 468 | } |
||
| 469 | } |