UserModel   B
last analyzed

Complexity

Total Complexity 49

Size/Duplication

Total Lines 570
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
eloc 187
c 3
b 2
f 1
dl 0
loc 570
rs 8.48
wmc 49

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getStat() 0 18 2
A getHistoryTitle() 0 3 1
A getStatStatusCount() 0 3 1
A getMeanScore() 0 6 1
A getFavTitle() 0 3 1
A getHistory() 0 19 2
A getHistoryImage() 0 5 1
A getStatus() 0 14 3
A getFavMediaTitle() 0 5 1
A getFavYear() 0 6 1
A getFavList() 0 30 6
A getHistoryDate() 0 5 1
A getFavId() 0 6 1
A getSns() 0 12 3
A getImage() 0 6 2
A getFavType() 0 6 1
A getDays() 0 6 1
A getFavImage() 0 7 1
A __construct() 0 7 1
A getHistoryId() 0 6 1
A getFavMedia() 0 6 1
A getStatus2() 0 6 1
A getStatStatus() 0 30 3
A __call() 0 7 2
A getUsername() 0 3 1
A getAbout() 0 6 2
A getHistoryProgress() 0 13 3
A getFavorite() 0 12 1
A getAllInfo() 0 24 1
A getFriend() 0 21 2

How to fix   Complexity   

Complex Class

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
2
3
namespace MalScraper\Model\User;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * UserModel class.
10
 */
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)
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 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)
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 = [];
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);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type boolean; however, parameter $matches of preg_match() does only seem to accept array|null, 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

460
        preg_match('/\'([^\'])*/', $image, /** @scrutinizer ignore-type */ $image);
Loading history...
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)
489
    {
490
        return $each_fav->find('a', 1)->plaintext;
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)
546
    {
547
        $anime_title = $each_fav->find('a', 2)->plaintext;
548
549
        return trim($anime_title);
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),
570
            'blog_post'      => $this->getStatus2(3),
571
            'club'           => $this->getStatus2(4),
572
            'sns'            => $this->getSns(),
573
            'friend'         => $this->getFriend(),
574
            'about'          => $this->getAbout(),
575
            'anime_stat'     => $this->getStat('anime'),
576
            'manga_stat'     => $this->getStat('manga'),
577
            'favorite'       => $this->getFavorite(),
578
        ];
579
580
        return $data;
581
    }
582
}
583