Passed
Branch feature/super-model (24c950)
by axel
02:55
created

InfoModel::getCharacter()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 37
nc 2
nop 0
dl 0
loc 54
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MalScraper\Model;
4
5
use MalScraper\Helper\Helper;
6
7
/**
8
 * InfoModel class.
9
 */
10
class InfoModel extends MainModel
11
{
12
    /**
13
     * Type of info. Either anime or manga.
14
     *
15
     * @var string
16
     */
17
	private $_type;
18
19
    /**
20
     * Id of the anime or manga.
21
     *
22
     * @var string|int
23
     */
24
	private $_id;
25
26
    /**
27
     * Default constructor.
28
     *
29
     * @param string $type
30
     * @param string|int $id
31
     * @param string $parserArea
32
     *
33
     * @return void
34
     */
35
	public function __construct($type, $id, $parserArea = '#content')
36
    {
37
    	$this->_type = $type;
38
    	$this->_id = $id;
39
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id;
40
    	$this->_parserArea = $parserArea;
41
42
        parent::errorCheck($this);
43
    }
44
45
    /**
46
     * Default call.
47
     *
48
     * @param string $method
49
     * @param array  $arguments
50
     *
51
     * @return array|string|int
52
     */
53
    public function __call($method, $arguments)
54
    {
55
        if ($this->_error)
56
            return $this->_error;
57
        return call_user_func_array([$this, $method], $arguments);
58
    }
59
60
    /**
61
     * Get anime/manga id.
62
     *
63
     * @return string
64
     */
65
    private function getId()
66
    {
67
    	return $this->_id;
68
    }
69
70
    /**
71
     * Get anime/manga cover.
72
     *
73
     * @return string
74
     */
75
    private function getCover()
76
    {
77
        $anime_cover = $this->_parser->find('img.ac', 0);
78
        return $anime_cover ? $anime_cover->src : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $anime_cover ? $anime_cover->src : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
79
    }
80
81
    /**
82
     * Get anime/manga title.
83
     *
84
     * @return string
85
     */
86
    private function getTitle()
87
    {
88
        $anime_cover = $this->_parser->find('img.ac', 0);
89
        return $anime_cover ? $anime_cover->alt : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $anime_cover ? $anime_cover->alt : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
90
    }
91
92
    /**
93
     * Get anime/manga alternative title.
94
     *
95
     * @return array
96
     */
97
    private function getTitle2()
98
    {
99
        $title2 = [];
100
101
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
102
103
        preg_match('/(English:<\/span>)([^<]*)/', $anime_info->innertext, $english);
104
        $title2['english'] = trim($english ? $english[2] : '');
105
106
        preg_match('/(Synonyms:<\/span>)([^<]*)/', $anime_info->innertext, $synonym);
107
        $title2['synonym'] = trim($synonym ? $synonym[2] : '');
108
109
        preg_match('/(Japanese:<\/span>)([^<]*)/', $anime_info->innertext, $japanese);
110
        $title2['japanese'] = trim($japanese ? $japanese[2] : '');
111
112
        return $title2;
113
    }
114
115
    /**
116
     * Get anime/manga synopsis.
117
     *
118
     * @return string
119
     */
120
    private function getSynopsis()
121
    {
122
        $synopsis = $this->_parser->find('span[itemprop=description]', 0);
123
        if ($synopsis) {
124
            $synopsis = $synopsis->plaintext;
125
            return trim(preg_replace('/\n[^\S\n]*/', "\n", $synopsis));
126
        } else {
127
            return null;
128
        }
129
    }
130
131
    /**
132
     * Get anime/manga score.
133
     *
134
     * @return string
135
     */
136
    private function getScore()
137
    {
138
        $score = $this->_parser->find('div[class="fl-l score"]', 0)->plaintext;
139
        $score = trim($score);
140
        return $score != 'N/A' ? $score : null;
141
    }
142
143
    /**
144
     * Get number of user who give score.
145
     *
146
     * @return string
147
     */
148
    private function getVoter()
149
    {
150
        $voter = $this->_parser->find('div[class="fl-l score"]', 0)->getAttribute('data-user');
151
        return trim(str_replace(['users', 'user', ','], '', $voter));
152
    }
153
154
    /**
155
     * Get anime/manga rank.
156
     *
157
     * @return string
158
     */
159
    private function getRank()
160
    {
161
        $rank = $this->_parser->find('span[class="numbers ranked"] strong', 0)->plaintext;
162
        $rank = $rank != 'N/A' ? $rank : '';
163
        return str_replace('#', '', $rank);
164
    }
165
166
    /**
167
     * Get anime/manga popularity.
168
     *
169
     * @return string
170
     */
171
    private function getPopularity()
172
    {
173
        $popularity = $this->_parser->find('span[class="numbers popularity"] strong', 0)->plaintext;
174
        return str_replace('#', '', $popularity);
175
    }
176
177
    /**
178
     * Get number of user who watch/read the anime/manga.
179
     *
180
     * @return string
181
     */
182
    private function getMembers()
183
    {
184
        $member = $this->_parser->find('span[class="numbers members"] strong', 0)->plaintext;
185
        return str_replace(',', '', $member);
186
    }
187
188
    /**
189
     * Get number of user who favorite the anime/manga.
190
     *
191
     * @return string
192
     */
193
    private function getFavorite()
194
    {
195
        $favorite = $this->_parser->find('div[data-id=info2]', 0)->next_sibling()->next_sibling()->next_sibling();
196
        $favorite_title = $favorite->find('span', 0)->plaintext;
197
        $favorite = $favorite->plaintext;
198
        $favorite = trim(str_replace($favorite_title, '', $favorite));
199
        $favorite = str_replace(',', '', $favorite);
200
        return preg_replace("/([\s])+/", ' ', $favorite);
201
    }
202
203
    /**
204
     * Get anime/manga detail info.
205
     *
206
     * @return array
207
     */
208
    private function getOtherInfo()
209
    {
210
        $info = [];
211
212
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
213
        $other_info = (count($anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
0 ignored issues
show
Bug introduced by
It seems like $anime_info->find('h2') can also be of type simplehtmldom_1_5\simple_html_dom_node; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        $other_info = (count(/** @scrutinizer ignore-type */ $anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
Loading history...
214
        $next_info = $other_info->next_sibling();
0 ignored issues
show
Bug introduced by
The method next_sibling() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
        /** @scrutinizer ignore-call */ 
215
        $next_info = $other_info->next_sibling();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
        while (true) {
216
            $info_type = $next_info->find('span', 0)->plaintext;
217
            $clean_info_type = strtolower(str_replace(': ', '', $info_type));
218
219
            $info_value = $next_info->plaintext;
220
            $clean_info_value = trim(str_replace($info_type, '', $info_value));
221
            $clean_info_value = preg_replace("/([\s])+/", ' ', $clean_info_value);
222
            $clean_info_value = str_replace([', add some', '?', 'Not yet aired', 'Unknown'], '', $clean_info_value);
223
224
            if ($clean_info_type == 'published' || $clean_info_type == 'aired') {
225
                $start_air = '';
226
                $end_air = '';
227
                if ($clean_info_value != 'Not available') {
228
                    $parsed_airing = explode(' to ', $clean_info_value);
229
230
                    $start_air = ($parsed_airing[0] != '?') ? date('Y-m-d', strtotime($parsed_airing[0])) : '';
231
                    if (count($parsed_airing) > 1) {
232
                        $end_air = ($parsed_airing[1] != '?') ? date('Y-m-d', strtotime($parsed_airing[1])) : '';
233
                    }
234
                }
235
236
                $clean_info_value = [];
237
                $clean_info_value['start'] = $start_air;
238
                $clean_info_value['end'] = $end_air;
239
            }
240
241
            if ($clean_info_type == 'producers'
242
                || $clean_info_type == 'licensors'
243
                || $clean_info_type == 'studios'
244
                || $clean_info_type == 'genres'
245
                || $clean_info_type == 'authors'
246
            ) {
247
                $info_temp = [];
248
                $info_temp_index = 0;
249
                if ($clean_info_value != 'None found') {
250
                    foreach ($next_info->find('a') as $each_info) {
251
                        $temp_id = explode('/', $each_info->href);
252
                        $info_temp[$info_temp_index]['id'] = $clean_info_type == 'authors' ? $temp_id[2] : $temp_id[3];
253
                        $info_temp[$info_temp_index]['name'] = $each_info->plaintext;
254
                        $info_temp_index++;
255
                    }
256
                }
257
                $clean_info_value = $info_temp;
258
            }
259
260
            $info[$clean_info_type] = $clean_info_value;
261
262
            $next_info = $next_info->next_sibling();
263
            if ($next_info->tag == 'h2' || $next_info->tag == 'br') {
264
                break;
265
            }
266
        }
267
        return $info;
268
    }
269
270
    /**
271
     * Get anime/manga relation.
272
     *
273
     * @return array
274
     */
275
    private function getRelated()
276
    {
277
        $related = [];
278
        $related_area = $this->_parser->find('.anime_detail_related_anime', 0);
279
        if ($related_area) {
280
            foreach ($related_area->find('tr') as $rel) {
281
                $rel_type = $rel->find('td', 0)->plaintext;
282
                $rel_type = trim(strtolower(str_replace(':', '', $rel_type)));
283
284
                $each_rel = [];
285
                $each_rel_index = 0;
286
                $rel_anime = $rel->find('td', 1);
287
                foreach ($rel_anime->find('a') as $r) {
288
                    $rel_anime_link = $r->href;
289
                    $separated_anime_link = explode('/', $rel_anime_link);
290
291
                    $each_rel[$each_rel_index]['id'] = $separated_anime_link[2];
292
                    $each_rel[$each_rel_index]['title'] = $r->plaintext;
293
                    $each_rel[$each_rel_index]['type'] = $separated_anime_link[1];
294
295
                    $each_rel_index++;
296
                }
297
298
                $related[$rel_type] = $each_rel;
299
            }
300
        }
301
        return $related;
302
    }
303
304
    /**
305
     * Get anime/manga character and its va.
306
     *
307
     * @return array
308
     */
309
    private function getCharacter()
310
    {
311
        $character = [];
312
        $char_index = 0;
313
        $character_area = $this->_parser->find('div[class^=detail-characters-list]', 0);
314
        if ($character_area) {
315
            $character_list = [
316
                $character_area->find('div[class*=fl-l]', 0),
317
                $character_area->find('div[class*=fl-r]', 0)
318
            ];
319
            foreach ($character_list as $character_side) {
320
                if ($character_side) {
321
                    foreach ($character_side->find('table[width=100%]') as $each_char) {
322
                        $char_image = $each_char->find('tr td', 0)->find('img', 0)->getAttribute('data-src');
323
                        $char_image = Helper::imageUrlCleaner($char_image);
324
325
                        $char = $each_char->find('tr td', 1);
326
327
                        $char_id = $char->find('a', 0)->href;
328
                        $char_id = explode('/', $char_id);
329
                        $char_id = $char_id[4];
330
331
                        $char_name = trim(preg_replace('/\s+/', ' ', $char->find('a', 0)->plaintext));
332
                        $char_role = trim($char->find('small', 0)->plaintext);
333
334
                        $character[$char_index]['id'] = $char_id;
335
                        $character[$char_index]['name'] = $char_name;
336
                        $character[$char_index]['role'] = $char_role;
337
                        $character[$char_index]['image'] = $char_image;
338
339
                        $va = $each_char->find('table td', 0);
340
                        if ($va) {
341
                            $va_id = $va->find('a', 0)->href;
342
                            $va_id = explode('/', $va_id);
343
                            $va_id = $va_id[4];
344
345
                            $va_name = $va->find('a', 0)->plaintext;
346
                            $va_role = $va->find('small', 0)->plaintext;
347
348
                            $va_image = $each_char->find('table td', 1)->find('img', 0)->getAttribute('data-src');
349
                            $va_image = Helper::imageUrlCleaner($va_image);
350
                        }
351
352
                        $character[$char_index]['va_id'] = isset($va_id) ? $va_id : '';
353
                        $character[$char_index]['va_name'] = isset($va_name) ? $va_name : '';
354
                        $character[$char_index]['va_role'] = isset($va_role) ? $va_role : '';
355
                        $character[$char_index]['va_image'] = isset($va_image) ? $va_image : '';
356
357
                        $char_index++;
358
                    }
359
                }
360
            }
361
        }
362
        return $character;
363
    }
364
365
    /**
366
     * Get anime/manga staff involved.
367
     *
368
     * @return array
369
     */
370
    private function getStaff()
371
    {
372
        $staff = [];
373
        $staff_index = 0;
374
        $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1);
375
        if ($staff_area) {
376
            $staff_list = [
377
                $staff_area->find('div[class*=fl-l]', 0),
378
                $staff_area->find('div[class*=fl-r]', 0)
379
            ];
380
            foreach ($staff_list as $staff_side) {
381
                if ($staff_side) {
382
                    foreach ($staff_side->find('table[width=100%]') as $each_staff) {
383
                        $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src');
384
                        $staff_image = Helper::imageUrlCleaner($staff_image);
385
386
                        $st = $each_staff->find('tr td', 1);
387
388
                        $staff_id = $st->find('a', 0)->href;
389
                        $staff_id = explode('/', $staff_id);
390
                        $staff_id = $staff_id[4];
391
392
                        $staff_name = trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext));
393
                        $staff_role = trim($st->find('small', 0)->plaintext);
394
395
                        $staff[$staff_index]['id'] = $staff_id;
396
                        $staff[$staff_index]['name'] = $staff_name;
397
                        $staff[$staff_index]['role'] = $staff_role;
398
                        $staff[$staff_index]['image'] = $staff_image;
399
400
                        $staff_index++;
401
                    }
402
                }
403
            }
404
        }
405
        return $staff;
406
    }
407
408
    /**
409
     * Get anime/manga opening and ending song.
410
     *
411
     * @return array
412
     */
413
    private function getSong()
414
    {
415
        $song = [];
416
        $song_area = $this->_parser->find('div[class*="theme-songs opnening"]', 0);
417
        if ($song_area) {
418
            foreach ($song_area->find('span.theme-song') as $each_song) {
419
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
420
                $song['opening'][] = $each_song;
421
            }
422
        }
423
424
        $song_area = $this->_parser->find('div[class*="theme-songs ending"]', 0);
425
        if ($song_area) {
426
            foreach ($song_area->find('span.theme-song') as $each_song) {
427
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
428
                $song['closing'][] = $each_song;
429
            }
430
        }
431
        return $song;
432
    }
433
434
    /**
435
     * Get anime/manga all information.
436
     *
437
     * @return array
438
     */
439
    private function getAllInfo()
440
    {
441
        $data = [
442
            'id'        => self::getId(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getId() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

442
            'id'        => self::/** @scrutinizer ignore-call */ getId(),
Loading history...
443
            'cover'     => self::getCover(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getCover() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

443
            'cover'     => self::/** @scrutinizer ignore-call */ getCover(),
Loading history...
444
            'title'     => self::getTitle(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getTitle() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

444
            'title'     => self::/** @scrutinizer ignore-call */ getTitle(),
Loading history...
445
            'title2'    => self::getTitle2(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getTitle2() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

445
            'title2'    => self::/** @scrutinizer ignore-call */ getTitle2(),
Loading history...
446
            'synopsis'  => self::getSynopsis(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getSynopsis() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

446
            'synopsis'  => self::/** @scrutinizer ignore-call */ getSynopsis(),
Loading history...
447
            'score'     => self::getScore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getScore() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

447
            'score'     => self::/** @scrutinizer ignore-call */ getScore(),
Loading history...
448
            'voter'     => self::getVoter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getVoter() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
            'voter'     => self::/** @scrutinizer ignore-call */ getVoter(),
Loading history...
449
            'rank'      => self::getRank(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getRank() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

449
            'rank'      => self::/** @scrutinizer ignore-call */ getRank(),
Loading history...
450
            'popularity'=> self::getPopularity(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getPopularity() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

450
            'popularity'=> self::/** @scrutinizer ignore-call */ getPopularity(),
Loading history...
451
            'members'   => self::getMembers(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getMembers() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

451
            'members'   => self::/** @scrutinizer ignore-call */ getMembers(),
Loading history...
452
            'favorite'  => self::getFavorite(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getFavorite() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

452
            'favorite'  => self::/** @scrutinizer ignore-call */ getFavorite(),
Loading history...
453
        ];
454
455
        $data = array_merge($data, self::getOtherInfo());
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getOtherInfo() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

455
        $data = array_merge($data, self::/** @scrutinizer ignore-call */ getOtherInfo());
Loading history...
456
457
        $data2 = [
458
            'related'   => self::getRelated(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getRelated() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

458
            'related'   => self::/** @scrutinizer ignore-call */ getRelated(),
Loading history...
459
            'character' => self::getCharacter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getCharacter() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

459
            'character' => self::/** @scrutinizer ignore-call */ getCharacter(),
Loading history...
460
            'staff'     => self::getStaff(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getStaff() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

460
            'staff'     => self::/** @scrutinizer ignore-call */ getStaff(),
Loading history...
461
            'song'      => self::getSong()
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\InfoModel::getSong() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

461
            'song'      => self::/** @scrutinizer ignore-call */ getSong()
Loading history...
462
        ];
463
464
        $data = array_merge($data, $data2);
465
466
    	return $data;
467
    }
468
}