Total Complexity | 49 |
Total Lines | 570 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 1 |
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') |
||
29 | { |
||
30 | $this->_user = $user; |
||
31 | $this->_url = $this->_myAnimeListUrl.'/profile/'.$user; |
||
32 | $this->_parserArea = $parserArea; |
||
33 | |||
34 | parent::errorCheck($this); |
||
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) |
||
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 string |
||
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) |
||
294 | { |
||
295 | $history = []; |
||
296 | $a_history_area = $right_area->find('div[class="updates '.$type.'"]', 0); |
||
297 | foreach ($a_history_area->find('.statistics-updates') as $each_history) { |
||
298 | $temp_history = []; |
||
299 | $history_data_area = $each_history->find('.data', 0); |
||
300 | |||
301 | $temp_history['image'] = $this->getHistoryImage($each_history); |
||
302 | $temp_history['id'] = $this->getHistoryId($history_data_area); |
||
303 | $temp_history['title'] = $this->getHistoryTitle($history_data_area); |
||
304 | $temp_history['date'] = $this->getHistoryDate($history_data_area); |
||
305 | $progress = $this->getHistoryProgress($history_data_area); |
||
306 | $temp_history = array_merge($temp_history, $progress); |
||
307 | |||
308 | $history[] = $temp_history; |
||
309 | } |
||
310 | |||
311 | return $history; |
||
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) |
||
322 | { |
||
323 | $image = $each_history->find('img', 0)->src; |
||
324 | |||
325 | return Helper::imageUrlCleaner($image); |
||
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) |
||
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 = []; |
||
384 | $temp_history['status'] = strtolower(count($p1) > 3 ? $progress[0] : $p1[0]); |
||
385 | $temp_history['progress'] = count($p1) > 3 ? '-' : $p1[1]; |
||
386 | $temp_history['score'] = trim(str_replace('Scored', '', $progress[1])); |
||
387 | |||
388 | return $temp_history; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Get favorite. |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | private function getFavorite() |
||
397 | { |
||
398 | $right_area = $this->_parser->find('.container-right', 0); |
||
399 | $favorite_area = $right_area->find('.user-favorites-outer', 0); |
||
400 | |||
401 | $favorite = []; |
||
402 | $favorite['anime'] = $this->getFavList($favorite_area, 'anime'); |
||
403 | $favorite['manga'] = $this->getFavList($favorite_area, 'manga'); |
||
404 | $favorite['character'] = $this->getFavList($favorite_area, 'characters'); |
||
405 | $favorite['people'] = $this->getFavList($favorite_area, 'people'); |
||
406 | |||
407 | return $favorite; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Get favorite list. |
||
412 | * |
||
413 | * @param \simplehtmldom_1_5\simple_html_dom $favorite_area |
||
414 | * @param string $type |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | private function getFavList($favorite_area, $type) |
||
419 | { |
||
420 | $favorite = []; |
||
421 | $favorite_area = $favorite_area->find('ul[class="favorites-list '.$type.'"]', 0); |
||
422 | if ($favorite_area) { |
||
423 | foreach ($favorite_area->find('li') as $each_fav) { |
||
424 | $temp_fav = []; |
||
425 | |||
426 | $temp_fav['image'] = $this->getFavImage($each_fav); |
||
427 | $temp_fav['id'] = $this->getFavId($each_fav); |
||
428 | |||
429 | if ($type == 'anime' || $type == 'manga') { |
||
430 | $temp_fav['title'] = $this->getFavTitle($each_fav); |
||
431 | $temp_fav['type'] = $this->getFavType($each_fav); |
||
432 | $temp_fav['year'] = $this->getFavYear($each_fav); |
||
433 | } else { |
||
434 | $temp_fav['name'] = $this->getFavTitle($each_fav); |
||
435 | |||
436 | if ($type == 'characters') { |
||
437 | $temp_fav['media_id'] = $this->getFavMedia($each_fav, 2); |
||
438 | $temp_fav['media_title'] = $this->getFavMediaTitle($each_fav); |
||
439 | $temp_fav['media_type'] = $this->getFavMedia($each_fav, 1); |
||
440 | } |
||
441 | } |
||
442 | |||
443 | $favorite[] = $temp_fav; |
||
444 | } |
||
445 | } |
||
446 | |||
447 | return $favorite; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Get favorite image. |
||
452 | * |
||
453 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | private function getFavImage($each_fav) |
||
458 | { |
||
459 | $image = $each_fav->find('a', 0)->style; |
||
460 | preg_match('/\'([^\'])*/', $image, $image); |
||
|
|||
461 | $image = substr($image[0], 1); |
||
462 | |||
463 | return Helper::imageUrlCleaner($image); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Get favorite id. |
||
468 | * |
||
469 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | private function getFavId($each_fav) |
||
474 | { |
||
475 | $id = $each_fav->find('a', 0)->href; |
||
476 | $id = explode('/', $id); |
||
477 | |||
478 | return $id[4]; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Get favorite title. |
||
483 | * |
||
484 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
485 | * |
||
486 | * @return string |
||
487 | */ |
||
488 | private function getFavTitle($each_fav) |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Get favorite type. |
||
495 | * |
||
496 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | private function getFavType($each_fav) |
||
501 | { |
||
502 | $temp_type = $each_fav->find('span', 0)->plaintext; |
||
503 | $temp_type = explode('·', $temp_type); |
||
504 | |||
505 | return trim($temp_type[0]); |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Get favorite year. |
||
510 | * |
||
511 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
512 | * |
||
513 | * @return string |
||
514 | */ |
||
515 | private function getFavYear($each_fav) |
||
516 | { |
||
517 | $temp_type = $each_fav->find('span', 0)->plaintext; |
||
518 | $temp_type = explode('·', $temp_type); |
||
519 | |||
520 | return trim($temp_type[1]); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Get favorite anime/manga id. |
||
525 | * |
||
526 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
527 | * |
||
528 | * @return string |
||
529 | */ |
||
530 | private function getFavMedia($each_fav, $key) |
||
531 | { |
||
532 | $media_id = $each_fav->find('a', 2)->href; |
||
533 | $media_id = explode('/', $media_id); |
||
534 | |||
535 | return $media_id[$key]; |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * Get favorite anime/manga title. |
||
540 | * |
||
541 | * @param \simplehtmldom_1_5\simple_html_dom $each_fav |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | private function getFavMediaTitle($each_fav) |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Get user information. |
||
554 | * |
||
555 | * @return array |
||
556 | */ |
||
557 | private function getAllInfo() |
||
558 | { |
||
559 | $data = [ |
||
560 | 'username' => $this->getUsername(), |
||
561 | 'image' => $this->getImage(), |
||
562 | 'last_online' => $this->getStatus('Last Online'), |
||
563 | 'gender' => $this->getStatus('Gender'), |
||
564 | 'birthday' => $this->getStatus('Birthday'), |
||
565 | 'location' => $this->getStatus('Location'), |
||
566 | 'joined_date' => $this->getStatus('Joined'), |
||
567 | 'forum_post' => $this->getStatus2(0), |
||
568 | 'review' => $this->getStatus2(1), |
||
569 | 'recommendation' => $this->getStatus2(2), |
||
581 | } |
||
582 | } |
||
583 |