Passed
Push — feature/super-model ( 24c950...13c8a0 )
by axel
02:30
created

InfoModel::getRank()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * InfoModel class.
10
 */
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 : '';
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...
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 : '';
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...
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()
161
    {
162
        $rank = $this->_parser->find('span[class="numbers ranked"] strong', 0)->plaintext;
163
        $rank = $rank != 'N/A' ? $rank : '';
164
        return str_replace('#', '', $rank);
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()
184
    {
185
        $member = $this->_parser->find('span[class="numbers members"] strong', 0)->plaintext;
186
        return str_replace(',', '', $member);
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()
210
    {
211
        $info = [];
212
213
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
214
        $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

214
        $other_info = (count(/** @scrutinizer ignore-type */ $anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
Loading history...
215
        $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

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

443
            'id'        => self::/** @scrutinizer ignore-call */ getId(),
Loading history...
444
            'cover'     => self::getCover(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

444
            'cover'     => self::/** @scrutinizer ignore-call */ getCover(),
Loading history...
445
            'title'     => self::getTitle(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

445
            'title'     => self::/** @scrutinizer ignore-call */ getTitle(),
Loading history...
446
            'title2'    => self::getTitle2(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

446
            'title2'    => self::/** @scrutinizer ignore-call */ getTitle2(),
Loading history...
447
            'synopsis'  => self::getSynopsis(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

447
            'synopsis'  => self::/** @scrutinizer ignore-call */ getSynopsis(),
Loading history...
448
            'score'     => self::getScore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

448
            'score'     => self::/** @scrutinizer ignore-call */ getScore(),
Loading history...
449
            'voter'     => self::getVoter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

449
            'voter'     => self::/** @scrutinizer ignore-call */ getVoter(),
Loading history...
450
            'rank'      => self::getRank(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

450
            'rank'      => self::/** @scrutinizer ignore-call */ getRank(),
Loading history...
451
            'popularity'=> self::getPopularity(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General...oModel::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

451
            'popularity'=> self::/** @scrutinizer ignore-call */ getPopularity(),
Loading history...
452
            'members'   => self::getMembers(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

452
            'members'   => self::/** @scrutinizer ignore-call */ getMembers(),
Loading history...
453
            'favorite'  => self::getFavorite(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

453
            'favorite'  => self::/** @scrutinizer ignore-call */ getFavorite(),
Loading history...
454
        ];
455
456
        $data = array_merge($data, self::getOtherInfo());
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

456
        $data = array_merge($data, self::/** @scrutinizer ignore-call */ getOtherInfo());
Loading history...
457
458
        $data2 = [
459
            'related'   => self::getRelated(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

459
            'related'   => self::/** @scrutinizer ignore-call */ getRelated(),
Loading history...
460
            'character' => self::getCharacter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

460
            'character' => self::/** @scrutinizer ignore-call */ getCharacter(),
Loading history...
461
            'staff'     => self::getStaff(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

461
            'staff'     => self::/** @scrutinizer ignore-call */ getStaff(),
Loading history...
462
            'song'      => self::getSong()
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\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

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