Total Complexity | 68 |
Total Lines | 491 |
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() |
||
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() |
||
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() |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Get anime/manga relation. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | private function getRelated() |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Get anime/manga character and its va. |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | private function getCharacter() |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Get anime/manga staff involved. |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | private function getStaff() |
||
356 | { |
||
357 | $staff = []; |
||
358 | $staff_index = 0; |
||
359 | $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1); |
||
360 | if ($staff_area) { |
||
361 | $staff_list = [ |
||
362 | $staff_area->find('div[class*=fl-l]', 0), |
||
363 | $staff_area->find('div[class*=fl-r]', 0) |
||
364 | ]; |
||
365 | foreach ($staff_list as $staff_side) { |
||
366 | if ($staff_side) { |
||
367 | foreach ($staff_side->find('table[width=100%]') as $each_staff) { |
||
368 | $st = $each_staff->find('tr td', 1); |
||
369 | |||
370 | $staff[$staff_index]['id'] = $this->getStaffId($st); |
||
371 | $staff[$staff_index]['name'] = $this->getStaffName($st); |
||
372 | $staff[$staff_index]['role'] = $this->getStaffRole($st); |
||
373 | $staff[$staff_index]['image'] = $this->getStaffImage($each_staff); |
||
374 | |||
375 | $staff_index++; |
||
376 | } |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | return $staff; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get staff id |
||
385 | * |
||
386 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | private function getStaffId($st) |
||
391 | { |
||
392 | $staff_id = $st->find('a', 0)->href; |
||
393 | $staff_id = explode('/', $staff_id); |
||
394 | return $staff_id[4]; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get staff name |
||
399 | * |
||
400 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
401 | * @param bool $va (Optional) |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | private function getStaffName($st, $va = false) |
||
406 | { |
||
407 | if ($va) { |
||
408 | return $st->find('a', 0)->plaintext; |
||
409 | } |
||
410 | return trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext)); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Get staff role |
||
415 | * |
||
416 | * @param \simplehtmldom_1_5\simple_html_dom $st |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | private function getStaffRole($st) |
||
421 | { |
||
422 | return trim($st->find('small', 0)->plaintext); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Get staff image |
||
427 | * |
||
428 | * @param \simplehtmldom_1_5\simple_html_dom $each_staff |
||
429 | * @param bool $va (Optional) |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | private function getStaffImage($each_staff, $va = false) |
||
434 | { |
||
435 | if ($va) { |
||
436 | $staff_image = $each_staff->find('table td', 1)->find('img', 0)->getAttribute('data-src'); |
||
437 | } else { |
||
438 | $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src'); |
||
439 | } |
||
440 | return Helper::imageUrlCleaner($staff_image); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Get anime/manga opening and ending song. |
||
445 | * |
||
446 | * @return array |
||
447 | */ |
||
448 | private function getSong() |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Get anime/manga all information. |
||
471 | * |
||
472 | * @return array |
||
473 | */ |
||
474 | private function getAllInfo() |
||
502 | } |
||
503 | } |