| Total Complexity | 72 |
| Total Lines | 565 |
| 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') |
||
| 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 | } |
||
| 59 | |||
| 60 | return call_user_func_array([$this, $method], $arguments); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get anime/manga id. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | private function getId() |
||
| 69 | { |
||
| 70 | return $this->_id; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get anime/manga cover. |
||
| 75 | * |
||
| 76 | * @return string|bool |
||
| 77 | */ |
||
| 78 | private function getCover() |
||
| 79 | { |
||
| 80 | $anime_cover = $this->_parser->find('img.ac', 0); |
||
| 81 | |||
| 82 | return $anime_cover ? $anime_cover->src : ''; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get anime/manga title. |
||
| 87 | * |
||
| 88 | * @return string|bool |
||
| 89 | */ |
||
| 90 | private function getTitle() |
||
| 91 | { |
||
| 92 | $anime_cover = $this->_parser->find('img.ac', 0); |
||
| 93 | |||
| 94 | return $anime_cover ? $anime_cover->alt : ''; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get anime/manga alternative title. |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | private function getTitle2() |
||
| 103 | { |
||
| 104 | $title2 = []; |
||
| 105 | |||
| 106 | $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0); |
||
| 107 | |||
| 108 | preg_match('/(English:<\/span>)([^<]*)/', $anime_info->innertext, $english); |
||
| 109 | $title2['english'] = trim($english ? $english[2] : ''); |
||
| 110 | |||
| 111 | preg_match('/(Synonyms:<\/span>)([^<]*)/', $anime_info->innertext, $synonym); |
||
| 112 | $title2['synonym'] = trim($synonym ? $synonym[2] : ''); |
||
| 113 | |||
| 114 | preg_match('/(Japanese:<\/span>)([^<]*)/', $anime_info->innertext, $japanese); |
||
| 115 | $title2['japanese'] = trim($japanese ? $japanese[2] : ''); |
||
| 116 | |||
| 117 | return $title2; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get anime/manga synopsis. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | private function getSynopsis() |
||
| 126 | { |
||
| 127 | $synopsis = $this->_parser->find('span[itemprop=description]', 0); |
||
| 128 | if ($synopsis) { |
||
| 129 | $synopsis = $synopsis->plaintext; |
||
| 130 | |||
| 131 | return trim(preg_replace('/\n[^\S\n]*/', "\n", $synopsis)); |
||
| 132 | } else { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get anime/manga score. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | private function getScore() |
||
| 143 | { |
||
| 144 | $score = $this->_parser->find('div[class="fl-l score"]', 0)->plaintext; |
||
| 145 | $score = trim($score); |
||
| 146 | |||
| 147 | return $score != 'N/A' ? $score : null; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get number of user who give score. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | private function getVoter() |
||
| 156 | { |
||
| 157 | $voter = $this->_parser->find('div[class="fl-l score"]', 0)->getAttribute('data-user'); |
||
| 158 | |||
| 159 | return trim(str_replace(['users', 'user', ','], '', $voter)); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get anime/manga rank. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | private function getRank() |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get anime/manga popularity. |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | private function getPopularity() |
||
| 181 | { |
||
| 182 | $popularity = $this->_parser->find('span[class="numbers popularity"] strong', 0)->plaintext; |
||
| 183 | |||
| 184 | return str_replace('#', '', $popularity); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get number of user who watch/read the anime/manga. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | private function getMembers() |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get number of user who favorite the anime/manga. |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function getFavorite() |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get anime/manga detail info. |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | private function getOtherInfo() |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get clean other info. |
||
| 248 | * |
||
| 249 | * @param string $info_type |
||
| 250 | * @param \simplehtmldom_1_5\simple_html_dom $next_info |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | private function getCleanInfo($info_type, $next_info) |
||
| 255 | { |
||
| 256 | $info_value = $next_info->plaintext; |
||
| 257 | $clean_info_value = trim(str_replace($info_type, '', $info_value)); |
||
| 258 | $clean_info_value = preg_replace("/([\s])+/", ' ', $clean_info_value); |
||
| 259 | |||
| 260 | return str_replace([', add some', '?', 'Not yet aired', 'Unknown'], '', $clean_info_value); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get cleaner other info. |
||
| 265 | * |
||
| 266 | * @param string $clean_info_type |
||
| 267 | * @param string $clean_info_value |
||
| 268 | * |
||
| 269 | * @return string|array |
||
| 270 | */ |
||
| 271 | private function getCleanerInfo1($clean_info_type, $clean_info_value) |
||
| 272 | { |
||
| 273 | if ($clean_info_type == 'published' || $clean_info_type == 'aired') { |
||
| 274 | $start_air = $end_air = ''; |
||
| 275 | if ($clean_info_value != 'Not available') { |
||
| 276 | $parsed_airing = explode(' to ', $clean_info_value); |
||
| 277 | $start_air = ($parsed_airing[0] != '?') ? $parsed_airing[0] : ''; |
||
| 278 | if (count($parsed_airing) > 1) { |
||
| 279 | $end_air = ($parsed_airing[1] != '?') ? $parsed_airing[1] : ''; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | $clean_info_value = []; |
||
| 284 | $clean_info_value['start'] = $start_air; |
||
| 285 | $clean_info_value['end'] = $end_air; |
||
| 286 | } |
||
| 287 | |||
| 288 | return $clean_info_value; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get cleaner other info. |
||
| 293 | * |
||
| 294 | * @param \simplehtmldom_1_5\simple_html_dom $next_info |
||
| 295 | * @param string $clean_info_type |
||
| 296 | * @param string|array $clean_info_value |
||
| 297 | * |
||
| 298 | * @return string|array |
||
| 299 | */ |
||
| 300 | private function getCleanerInfo2($next_info, $clean_info_type, $clean_info_value) |
||
| 301 | { |
||
| 302 | if ($clean_info_type == 'producers' |
||
| 303 | || $clean_info_type == 'licensors' |
||
| 304 | || $clean_info_type == 'studios' |
||
| 305 | || $clean_info_type == 'genres' |
||
| 306 | || $clean_info_type == 'authors' |
||
| 307 | ) { |
||
| 308 | $info_temp = []; |
||
| 309 | $info_temp_index = 0; |
||
| 310 | if ($clean_info_value != 'None found') { |
||
| 311 | foreach ($next_info->find('a') as $each_info) { |
||
| 312 | $temp_id = explode('/', $each_info->href); |
||
| 313 | $info_temp[$info_temp_index]['id'] = $clean_info_type == 'authors' ? $temp_id[2] : $temp_id[3]; |
||
| 314 | $info_temp[$info_temp_index]['name'] = $each_info->plaintext; |
||
| 315 | $info_temp_index++; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | return $info_temp; |
||
| 320 | } |
||
| 321 | |||
| 322 | return $clean_info_value; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get anime/manga relation. |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | private function getRelated() |
||
| 331 | { |
||
| 332 | $related = []; |
||
| 333 | $related_area = $this->_parser->find('.anime_detail_related_anime', 0); |
||
| 334 | if ($related_area) { |
||
| 335 | foreach ($related_area->find('tr') as $rel) { |
||
| 336 | $rel_type = $rel->find('td', 0)->plaintext; |
||
| 337 | $rel_type = trim(strtolower(str_replace(':', '', $rel_type))); |
||
| 338 | |||
| 339 | $each_rel = []; |
||
| 340 | $each_rel_index = 0; |
||
| 341 | $rel_anime = $rel->find('td', 1); |
||
| 342 | foreach ($rel_anime->find('a') as $r) { |
||
| 343 | $each_rel[$each_rel_index] = $this->getRelatedDetail($r); |
||
| 344 | $each_rel_index++; |
||
| 345 | } |
||
| 346 | |||
| 347 | $related[$rel_type] = $each_rel; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | return $related; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get related detail. |
||
| 356 | * |
||
| 357 | * @param \simplehtmldom_1_5\simple_html_dom $r |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | private function getRelatedDetail($r) |
||
| 362 | { |
||
| 363 | $related = []; |
||
| 364 | $rel_anime_link = $r->href; |
||
| 365 | $separated_anime_link = explode('/', $rel_anime_link); |
||
| 366 | |||
| 367 | $related['id'] = $separated_anime_link[2]; |
||
| 368 | $related['title'] = $r->plaintext; |
||
| 369 | $related['type'] = $separated_anime_link[1]; |
||
| 370 | |||
| 371 | return $related; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get anime/manga character and its va. |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | private function getCharacter() |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get anime/manga staff involved. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | private function getStaff() |
||
| 425 | { |
||
| 426 | $staff = []; |
||
| 427 | $staff_index = 0; |
||
| 428 | $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1); |
||
| 429 | if ($staff_area) { |
||
| 430 | $staff_list = [ |
||
| 431 | $staff_area->find('div[class*=fl-l]', 0), |
||
| 432 | $staff_area->find('div[class*=fl-r]', 0), |
||
| 433 | ]; |
||
| 434 | foreach ($staff_list as $staff_side) { |
||
| 435 | if ($staff_side) { |
||
| 436 | foreach ($staff_side->find('table[width=100%]') as $each_staff) { |
||
| 437 | $st = $each_staff->find('tr td', 1); |
||
| 438 | |||
| 439 | $staff[$staff_index]['id'] = $this->getStaffId($st); |
||
| 440 | $staff[$staff_index]['name'] = $this->getStaffName($st); |
||
| 441 | $staff[$staff_index]['role'] = $this->getStaffRole($st); |
||
| 442 | $staff[$staff_index]['image'] = $this->getStaffImage($each_staff); |
||
| 443 | |||
| 444 | $staff_index++; |
||
| 445 | } |
||
| 446 | } |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | return $staff; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get staff id. |
||
| 455 | * |
||
| 456 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | private function getStaffId($st) |
||
| 461 | { |
||
| 462 | $staff_id = $st->find('a', 0)->href; |
||
| 463 | $staff_id = explode('/', $staff_id); |
||
| 464 | |||
| 465 | return $staff_id[4]; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get staff name. |
||
| 470 | * |
||
| 471 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
| 472 | * @param bool $va (Optional) |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | private function getStaffName($st, $va = false) |
||
| 477 | { |
||
| 478 | if ($va) { |
||
| 479 | return $st->find('a', 0)->plaintext; |
||
| 480 | } |
||
| 481 | |||
| 482 | return trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext)); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get staff role. |
||
| 487 | * |
||
| 488 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function getStaffRole($st) |
||
| 493 | { |
||
| 494 | return trim($st->find('small', 0)->plaintext); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get staff image. |
||
| 499 | * |
||
| 500 | * @param \simplehtmldom_1_5\simple_html_dom $each_staff |
||
| 501 | * @param bool $va (Optional) |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | private function getStaffImage($each_staff, $va = false) |
||
| 506 | { |
||
| 507 | if ($va) { |
||
| 508 | $staff_image = $each_staff->find('table td', 1)->find('img', 0)->getAttribute('data-src'); |
||
| 509 | } else { |
||
| 510 | $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src'); |
||
| 511 | } |
||
| 512 | |||
| 513 | return Helper::imageUrlCleaner($staff_image); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get anime/manga opening and ending song. |
||
| 518 | * |
||
| 519 | * @return array |
||
| 520 | */ |
||
| 521 | private function getSong() |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Get anime/manga all information. |
||
| 545 | * |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | private function getAllInfo() |
||
| 576 | } |
||
| 577 | } |
||
| 578 |