Passed
Push — feature/super-model ( a850c4...c51526 )
by axel
03:11
created

AllGenreModel::getGenreCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\Lists;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * AllGenreModel class.
10
 */
11
class AllGenreModel extends MainModel
12
{
13
    /**
14
     * Either anime or manga.
15
     *
16
     * @var string
17
     */
18
	private $_type;
19
20
    /**
21
     * Default constructor.
22
     *
23
     * @param string $type
24
     * @param string $parserArea
25
     *
26
     * @return void
27
     */
28
	public function __construct($type, $parserArea = '.anime-manga-search .genre-link')
29
    {
30
    	$this->_type = $type;
31
        if ($type == 'anime') {
32
            $this->_url = $this->_myAnimeListUrl.'/anime.php';
33
        } else {
34
            $this->_url = $this->_myAnimeListUrl.'/manga.php';
35
        }
36
    	$this->_parserArea = $parserArea;
37
38
        parent::errorCheck($this);
39
    }
40
41
    /**
42
     * Default call.
43
     *
44
     * @param string $method
45
     * @param array  $arguments
46
     *
47
     * @return array|string|int
48
     */
49
    public function __call($method, $arguments)
50
    {
51
        if ($this->_error)
52
            return $this->_error;
53
        return call_user_func_array([$this, $method], $arguments);
54
    }
55
56
    /**
57
     * Get genre count
58
     *
59
     * @param \simplehtmldom_1_5\simple_html_dom $each_genre
60
     *
61
     * @return string
62
     */
63
    private function getGenreCount($each_genre)
64
    {
65
        $count = $each_genre->plaintext;
66
        preg_match('/\([0-9,]+\)/', $count, $count);
0 ignored issues
show
Bug introduced by
It seems like $count can also be of type string; 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

66
        preg_match('/\([0-9,]+\)/', $count, /** @scrutinizer ignore-type */ $count);
Loading history...
67
        $count = substr($count[0], 1, strlen($count[0]) - 2);
68
        return str_replace(',', '', $count);
69
    }
70
71
    /**
72
     * Get list of all genre.
73
     *
74
     * @return array
75
     */
76
    private function getAllInfo()
77
    {
78
        $data = [];
79
        foreach ($this->_parser->find('.genre-list a') as $each_genre) {
80
            $genre = [];
81
82
            $link = $each_genre->href;
83
            $link = explode('/', $link);
84
            $id = $link[3];
85
            $genre['id'] = $id;
86
87
            $name = str_replace('_', ' ', $link[4]);
88
            $genre['name'] = $name;
89
90
            $genre['count'] = $this->getGenreCount($each_genre);
91
92
            $data[] = $genre;
93
        }
94
        return $data;
95
    }
96
}