| Total Complexity | 49 |
| Total Lines | 568 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UserModel 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 UserModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class UserModel extends MainModel |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Username. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $_user; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Default constructor. |
||
| 22 | * |
||
| 23 | * @param string $user |
||
| 24 | * @param string $parserArea |
||
| 25 | * |
||
| 26 | * @return void |
||
| 27 | */ |
||
| 28 | public function __construct($user, $parserArea = '#content') |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Default call. |
||
| 39 | * |
||
| 40 | * @param string $method |
||
| 41 | * @param array $arguments |
||
| 42 | * |
||
| 43 | * @return array|string|int |
||
| 44 | */ |
||
| 45 | public function __call($method, $arguments) |
||
| 46 | { |
||
| 47 | if ($this->_error) { |
||
| 48 | return $this->_error; |
||
| 49 | } |
||
| 50 | |||
| 51 | return call_user_func_array([$this, $method], $arguments); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get username. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | private function getUsername() |
||
| 60 | { |
||
| 61 | return $this->_user; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get user image. |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | private function getImage() |
||
| 70 | { |
||
| 71 | $image = $this->_parser->find('.container-left .user-profile', 0); |
||
| 72 | $image = $image->find('.user-image img', 0); |
||
| 73 | |||
| 74 | return $image ? Helper::imageUrlCleaner($image->src) : ''; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get user status. |
||
| 79 | * |
||
| 80 | * @param string $status |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | private function getStatus($status) |
||
| 85 | { |
||
| 86 | $status_area = $this->_parser->find('.container-left .user-profile', 0); |
||
| 87 | $status_area = $status_area->find('.user-status', 0); |
||
| 88 | foreach ($status_area->find('li') as $each_status) { |
||
| 89 | $status_type = trim($each_status->find('span', 0)->plaintext); |
||
| 90 | $status_value = trim($each_status->find('span', 1)->plaintext); |
||
| 91 | |||
| 92 | if ($status == $status_type) { |
||
| 93 | return $status_value; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return ''; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get user status. |
||
| 102 | * |
||
| 103 | * @param int $liNo |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | private function getStatus2($liNo) |
||
| 108 | { |
||
| 109 | $status_area = $this->_parser->find('.container-left .user-profile', 0); |
||
| 110 | $status_area = $status_area->find('.user-status', 2); |
||
| 111 | |||
| 112 | return trim($status_area->find('li', $liNo)->find('span', 1)->plaintext); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get user sns. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | private function getSns() |
||
| 121 | { |
||
| 122 | $sns = []; |
||
| 123 | $sns_area = $this->_parser->find('.container-left .user-profile', 0); |
||
| 124 | $sns_area = $sns_area->find('.user-profile-sns', 0); |
||
| 125 | foreach ($sns_area->find('a') as $each_sns) { |
||
| 126 | if ($each_sns->class != 'di-ib mb8') { |
||
| 127 | $sns[] = $each_sns->href; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | return $sns; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get user friend. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | private function getFriend() |
||
| 140 | { |
||
| 141 | $friend = []; |
||
| 142 | $friend_area = $this->_parser->find('.container-left .user-profile', 0); |
||
| 143 | $friend_area = $friend_area->find('.user-friends', 0); |
||
| 144 | |||
| 145 | $friend_count = $friend_area->prev_sibling()->find('a', 0)->plaintext; |
||
| 146 | preg_match('/\(\d+\)/', $friend_count, $friend_count); |
||
| 147 | $friend['count'] = str_replace(['(', ')'], '', $friend_count[0]); |
||
| 148 | |||
| 149 | $friend['data'] = []; |
||
| 150 | foreach ($friend_area->find('a') as $f) { |
||
| 151 | $temp_friend = []; |
||
| 152 | |||
| 153 | $temp_friend['name'] = $f->plaintext; |
||
| 154 | $temp_friend['image'] = Helper::imageUrlCleaner($f->getAttribute('data-bg')); |
||
| 155 | |||
| 156 | $friend['data'][] = $temp_friend; |
||
| 157 | } |
||
| 158 | |||
| 159 | return $friend; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get user about. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | private function getAbout() |
||
| 168 | { |
||
| 169 | $about_area = $this->_parser->find('.container-right', 0); |
||
| 170 | $about = $about_area->find('table tr td div[class=word-break]', 0); |
||
| 171 | |||
| 172 | return $about ? trim($about->innertext) : ''; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get user anime stat. |
||
| 177 | * |
||
| 178 | * @param string $type |
||
| 179 | * |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | private function getStat($type) |
||
| 183 | { |
||
| 184 | $anime_stat = []; |
||
| 185 | $right_area = $this->_parser->find('.container-right', 0); |
||
| 186 | $stat_area = $right_area->find('.user-statistics', 0); |
||
| 187 | if ($type == 'anime') { |
||
| 188 | $a_stat_area = $stat_area->find('div[class="user-statistics-stats mt16"]', 0); |
||
| 189 | } else { |
||
| 190 | $a_stat_area = $stat_area->find('div[class="user-statistics-stats mt16"]', 1); |
||
| 191 | } |
||
| 192 | $a_stat_score = $a_stat_area->find('.stat-score', 0); |
||
| 193 | |||
| 194 | $anime_stat['days'] = $this->getDays($a_stat_score); |
||
| 195 | $anime_stat['mean_score'] = $this->getMeanScore($a_stat_score); |
||
| 196 | $anime_stat['status'] = $this->getStatStatus($a_stat_area, $type); |
||
| 197 | $anime_stat['history'] = $this->getHistory($right_area, $type); |
||
| 198 | |||
| 199 | return $anime_stat; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get days stat. |
||
| 204 | * |
||
| 205 | * @param \simplehtmldom_1_5\simple_html_dom $a_stat_score |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | private function getDays($a_stat_score) |
||
| 210 | { |
||
| 211 | $days = $a_stat_score->find('div', 0); |
||
| 212 | $temp_days = $days->find('span', 0)->plaintext; |
||
| 213 | |||
| 214 | return str_replace($temp_days, '', $days->plaintext); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get mean score stat. |
||
| 219 | * |
||
| 220 | * @param \simplehtmldom_1_5\simple_html_dom $a_stat_score |
||
| 221 | * |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | private function getMeanScore($a_stat_score) |
||
| 225 | { |
||
| 226 | $mean_score = $a_stat_score->find('div', 1); |
||
| 227 | $temp_score = $mean_score->find('span', 0)->plaintext; |
||
| 228 | |||
| 229 | return str_replace($temp_score, '', $mean_score->plaintext); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get status stat. |
||
| 234 | * |
||
| 235 | * @param \simplehtmldom_1_5\simple_html_dom $a_stat_area |
||
| 236 | * @param string $type |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | private function getStatStatus($a_stat_area, $type) |
||
| 241 | { |
||
| 242 | $temp_stat = []; |
||
| 243 | $a_stat_status = $a_stat_area->find('ul[class=stats-status]', 0); |
||
| 244 | if ($type == 'anime') { |
||
| 245 | $temp_stat['watching'] = $this->getStatStatusCount($a_stat_status, 0); |
||
| 246 | $temp_stat['completed'] = $this->getStatStatusCount($a_stat_status, 1); |
||
| 247 | $temp_stat['on_hold'] = $this->getStatStatusCount($a_stat_status, 2); |
||
| 248 | $temp_stat['dropped'] = $this->getStatStatusCount($a_stat_status, 3); |
||
| 249 | $temp_stat['plan_to_watch'] = $this->getStatStatusCount($a_stat_status, 4); |
||
| 250 | } else { |
||
| 251 | $temp_stat['reading'] = $this->getStatStatusCount($a_stat_status, 0); |
||
| 252 | $temp_stat['completed'] = $this->getStatStatusCount($a_stat_status, 1); |
||
| 253 | $temp_stat['on_hold'] = $this->getStatStatusCount($a_stat_status, 2); |
||
| 254 | $temp_stat['dropped'] = $this->getStatStatusCount($a_stat_status, 3); |
||
| 255 | $temp_stat['plan_to_read'] = $this->getStatStatusCount($a_stat_status, 4); |
||
| 256 | } |
||
| 257 | |||
| 258 | $a_stat_status = $a_stat_area->find('ul[class=stats-data]', 0); |
||
| 259 | $temp_stat['total'] = $this->getStatStatusCount($a_stat_status, 1); |
||
| 260 | if ($type == 'anime') { |
||
| 261 | $temp_stat['rewatched'] = $this->getStatStatusCount($a_stat_status, 3); |
||
| 262 | $temp_stat['episode'] = $this->getStatStatusCount($a_stat_status, 5); |
||
| 263 | } else { |
||
| 264 | $temp_stat['reread'] = $this->getStatStatusCount($a_stat_status, 3); |
||
| 265 | $temp_stat['chapter'] = $this->getStatStatusCount($a_stat_status, 5); |
||
| 266 | $temp_stat['volume'] = $this->getStatStatusCount($a_stat_status, 7); |
||
| 267 | } |
||
| 268 | |||
| 269 | return $temp_stat; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get status stat count. |
||
| 274 | * |
||
| 275 | * @param \simplehtmldom_1_5\simple_html_dom $a_stat_area |
||
| 276 | * @param int $spanNo |
||
| 277 | * |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | private function getStatStatusCount($a_stat_status, $spanNo) |
||
| 281 | { |
||
| 282 | return str_replace(',', '', trim($a_stat_status->find('span', $spanNo)->plaintext)); |
||
|
|
|||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get history. |
||
| 287 | * |
||
| 288 | * @param \simplehtmldom_1_5\simple_html_dom $right_area |
||
| 289 | * @param string $type |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | private function getHistory($right_area, $type) |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get history image. |
||
| 316 | * |
||
| 317 | * @param \simplehtmldom_1_5\simple_html_dom $each_history |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | private function getHistoryImage($each_history) |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get history id. |
||
| 330 | * |
||
| 331 | * @param \simplehtmldom_1_5\simple_html_dom $history_data_area |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | private function getHistoryId($history_data_area) |
||
| 336 | { |
||
| 337 | $id = $history_data_area->find('a', 0)->href; |
||
| 338 | $id = explode('/', $id); |
||
| 339 | |||
| 340 | return $id[4]; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get history title. |
||
| 345 | * |
||
| 346 | * @param \simplehtmldom_1_5\simple_html_dom $history_data_area |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private function getHistoryTitle($history_data_area) |
||
| 351 | { |
||
| 352 | return $history_data_area->find('a', 0)->plaintext; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get history date. |
||
| 357 | * |
||
| 358 | * @param \simplehtmldom_1_5\simple_html_dom $history_data_area |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | private function getHistoryDate($history_data_area) |
||
| 363 | { |
||
| 364 | $date = $history_data_area->find('span', 0)->plaintext; |
||
| 365 | |||
| 366 | return trim($date); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get history progress. |
||
| 371 | * |
||
| 372 | * @param \simplehtmldom_1_5\simple_html_dom $history_data_area |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | private function getHistoryProgress($history_data_area) |
||
| 377 | { |
||
| 378 | $progress = $history_data_area->find('.graph-content', 0)->next_sibling()->innertext; |
||
| 379 | $progress = trim(preg_replace("/([\s])+/", ' ', strip_tags($progress))); |
||
| 380 | $progress = explode('·', $progress); |
||
| 381 | $p1 = explode(' ', $progress[0]); |
||
| 382 | |||
| 383 | $temp_history['status'] = strtolower(count($p1) > 3 ? $progress[0] : $p1[0]); |
||
| 384 | $temp_history['progress'] = count($p1) > 3 ? '-' : $p1[1]; |
||
| 385 | $temp_history['score'] = trim(str_replace('Scored', '', $progress[1])); |
||
| 386 | |||
| 387 | return $temp_history; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get favorite. |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | private function getFavorite() |
||
| 396 | { |
||
| 397 | $right_area = $this->_parser->find('.container-right', 0); |
||
| 398 | $favorite_area = $right_area->find('.user-favorites-outer', 0); |
||
| 399 | |||
| 400 | $favorite['anime'] = $this->getFavList($favorite_area, 'anime'); |
||
| 401 | $favorite['manga'] = $this->getFavList($favorite_area, 'manga'); |
||
| 402 | $favorite['character'] = $this->getFavList($favorite_area, 'characters'); |
||
| 403 | $favorite['people'] = $this->getFavList($favorite_area, 'people'); |
||
| 404 | |||
| 405 | return $favorite; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get favorite list. |
||
| 410 | * |
||
| 411 | * @param \simplehtmldom_1_5\simple_html_dom $favorite_area |
||
| 412 | * @param string $type |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | private function getFavList($favorite_area, $type) |
||
| 417 | { |
||
| 418 | $favorite = []; |
||
| 419 | $favorite_area = $favorite_area->find('ul[class="favorites-list '.$type.'"]', 0); |
||
| 420 | if ($favorite_area) { |
||
| 421 | foreach ($favorite_area->find('li') as $each_fav) { |
||
| 422 | $temp_fav = []; |
||
| 423 | |||
| 424 | $temp_fav['image'] = $this->getFavImage($each_fav); |
||
| 425 | $temp_fav['id'] = $this->getFavId($each_fav); |
||
| 426 | |||
| 427 | if ($type == 'anime' || $type == 'manga') { |
||
| 428 | $temp_fav['title'] = $this->getFavTitle($each_fav); |
||
| 429 | $temp_fav['type'] = $this->getFavType($each_fav); |
||
| 430 | $temp_fav['year'] = $this->getFavYear($each_fav); |
||
| 431 | } else { |
||
| 432 | $temp_fav['name'] = $this->getFavTitle($each_fav); |
||
| 433 | |||
| 434 | if ($type == 'characters') { |
||
| 435 | $temp_fav['media_id'] = $this->getFavMedia($each_fav, 2); |
||
| 436 | $temp_fav['media_title'] = $this->getFavMediaTitle($each_fav); |
||
| 437 | $temp_fav['media_type'] = $this->getFavMedia($each_fav, 1); |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | $favorite[] = $temp_fav; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | return $favorite; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get favorite image. |
||
| 450 | * |
||
| 451 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | private function getFavImage($each_fav) |
||
| 456 | { |
||
| 457 | $image = $each_fav->find('a', 0)->style; |
||
| 458 | preg_match('/\'([^\'])*/', $image, $image); |
||
| 459 | $image = substr($image[0], 1); |
||
| 460 | |||
| 461 | return Helper::imageUrlCleaner($image); |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get favorite id. |
||
| 466 | * |
||
| 467 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | private function getFavId($each_fav) |
||
| 472 | { |
||
| 473 | $id = $each_fav->find('a', 0)->href; |
||
| 474 | $id = explode('/', $id); |
||
| 475 | |||
| 476 | return $id[4]; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get favorite title. |
||
| 481 | * |
||
| 482 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | private function getFavTitle($each_fav) |
||
| 487 | { |
||
| 488 | return $each_fav->find('a', 1)->plaintext; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get favorite type. |
||
| 493 | * |
||
| 494 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | private function getFavType($each_fav) |
||
| 499 | { |
||
| 500 | $temp_type = $each_fav->find('span', 0)->plaintext; |
||
| 501 | $temp_type = explode('·', $temp_type); |
||
| 502 | |||
| 503 | return trim($temp_type[0]); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get favorite year. |
||
| 508 | * |
||
| 509 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | private function getFavYear($each_fav) |
||
| 514 | { |
||
| 515 | $temp_type = $each_fav->find('span', 0)->plaintext; |
||
| 516 | $temp_type = explode('·', $temp_type); |
||
| 517 | |||
| 518 | return trim($temp_type[1]); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get favorite anime/manga id. |
||
| 523 | * |
||
| 524 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | private function getFavMedia($each_fav, $key) |
||
| 529 | { |
||
| 530 | $media_id = $each_fav->find('a', 2)->href; |
||
| 531 | $media_id = explode('/', $media_id); |
||
| 532 | |||
| 533 | return $media_id[$key]; |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get favorite anime/manga title. |
||
| 538 | * |
||
| 539 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | private function getFavMediaTitle($each_fav) |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get user information. |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | private function getAllInfo() |
||
| 556 | { |
||
| 557 | $data = [ |
||
| 558 | 'username' => $this->getUsername(), |
||
| 559 | 'image' => $this->getImage(), |
||
| 560 | 'last_online' => $this->getStatus('Last Online'), |
||
| 561 | 'gender' => $this->getStatus('Gender'), |
||
| 562 | 'birthday' => $this->getStatus('Birthday'), |
||
| 563 | 'location' => $this->getStatus('Location'), |
||
| 564 | 'joined_date' => $this->getStatus('Joined'), |
||
| 565 | 'forum_post' => $this->getStatus2(0), |
||
| 566 | 'review' => $this->getStatus2(1), |
||
| 567 | 'recommendation' => $this->getStatus2(2), |
||
| 568 | 'blog_post' => $this->getStatus2(3), |
||
| 569 | 'club' => $this->getStatus2(4), |
||
| 570 | 'sns' => $this->getSns(), |
||
| 579 | } |
||
| 580 | } |
||
| 581 |