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

CharacterModel   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 247
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getNickname() 0 8 2
A __construct() 0 7 1
A getMedia() 0 34 4
A getName() 0 11 3
A getAbout() 0 17 2
A __call() 0 5 2
A getVa() 0 39 4
A getFavorite() 0 7 1
A getAllInfo() 0 16 1
A getId() 0 3 1
A getImage() 0 5 2
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * CharacterModel class.
10
 */
11
class CharacterModel extends MainModel
12
{
13
    /**
14
     * Id of the character.
15
     *
16
     * @var string|int
17
     */
18
	private $_id;
19
20
    /**
21
     * Default constructor.
22
     *
23
     * @param string|int $id
24
     * @param string $parserArea
25
     *
26
     * @return void
27
     */
28
	public function __construct($id, $parserArea = '#contentWrapper')
29
    {
30
    	$this->_id = $id;
31
        $this->_url = $this->_myAnimeListUrl.'/character/'.$id;
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
        return call_user_func_array([$this, $method], $arguments);
50
    }
51
52
    /**
53
     * Get character id.
54
     *
55
     * @return string
56
     */
57
    private function getId()
58
    {
59
        return $this->_id;
60
    }
61
62
    /**
63
     * Get character image.
64
     *
65
     * @return string
66
     */
67
    private function getImage()
68
    {
69
        $image = $this->_parser->find('#content table tr', 0);
70
        $image = $image->find('td', 0)->find('div', 0)->find('a', 0)->find('img', 0);
71
        return $image ? $image->src : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $image ? $image->src : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
72
    }
73
74
    /**
75
     * Get character image.
76
     *
77
     * @return string
78
     */
79
    private function getNickname()
80
    {
81
        $nickname = $this->_parser->find('h1', 0)->plaintext;
82
        $nickname = trim(preg_replace('/\s+/', ' ', $nickname));
83
        preg_match('/\"([^"])*/', $nickname, $nickname);
0 ignored issues
show
Bug introduced by
$nickname of type string is incompatible with the type array|null expected by parameter $matches of preg_match(). ( Ignorable by Annotation )

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

83
        preg_match('/\"([^"])*/', $nickname, /** @scrutinizer ignore-type */ $nickname);
Loading history...
84
        if ($nickname)
85
            return substr($nickname[0], 1, strlen($nickname[0]) - 2);
86
        return null;
87
    }
88
89
    /**
90
     * Get character name.
91
     *
92
     * @param bool $isKanji
93
     *
94
     * @return string
95
     */
96
    private function getName($isKanji=false)
97
    {
98
        $html = $this->_parser->find('#content table tr', 0);
99
        $html = $html->find('td', 0)->next_sibling()->find('div[class=normal_header]', 0);
100
101
        $name_kanji = $html->find('small', 0);
102
        $name_kanji = $name_kanji ? $name_kanji->plaintext : '';
103
104
        if ($isKanji)
105
            return preg_replace('/(\(|\))/', '', $name_kanji);
106
        return trim(str_replace($name_kanji, '', $html->plaintext));
107
    }
108
109
    /**
110
     * Get number of user who favorite the character.
111
     *
112
     * @return string
113
     */
114
    private function getFavorite()
115
    {
116
        $favorite = $this->_parser->find('#content table tr', 0)->find('td', 0)->plaintext;
117
        preg_match('/(Member Favorites: ).+/', $favorite, $parsed_favorite);
118
        $favorite = trim($parsed_favorite[0]);
119
        $parsed_favorite = explode(': ', $favorite);
120
        return str_replace(',', '', $parsed_favorite[1]);
121
    }
122
123
    /**
124
     * Get character about.
125
     *
126
     * @return string
127
     */
128
    private function getAbout()
129
    {
130
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
131
132
        preg_match('/(<div class="normal_header" style="height: 15px;">).*(<div class="normal_header">)/', $html, $about);
133
134
        $html = $html->find('div[class=normal_header]', 0);
135
        $about = str_replace($html->outertext, '', $about[0]);
136
        $about = str_replace('<div class="normal_header">', '', $about);
137
138
        preg_match('/(No biography written)/', $about, $temp_about);
139
        if (!$temp_about) {
140
            $about = str_replace(['<br>', '<br />', '  '], ["\n", "\n", ' '], $about);
141
            $about = strip_tags($about);
142
            return preg_replace('/\n[^\S\n]*/', "\n", $about);
143
        } else {
144
            return null;
145
        }
146
    }
147
148
    /**
149
     * Get character role in anime/manga.
150
     *
151
     * @param string $type Either anime or manga
152
     *
153
     * @return array
154
     */
155
    private function getMedia($type='anime')
156
    {
157
        $mediaography = [];
158
        $mediaography_index = 0;
159
160
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0);
161
        $mediaography_area = $type == 'anime' ? $html->find('table', 0) : $html->find('table', 1);
162
        $mediaography_area = $mediaography_area->find('tr');
0 ignored issues
show
Bug introduced by
The method find() 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

162
        /** @scrutinizer ignore-call */ 
163
        $mediaography_area = $mediaography_area->find('tr');

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...
163
        if ($mediaography_area) {
164
            foreach ($mediaography_area as $each_media) {
165
                $media_image = $each_media->find('td', 0)->find('img', 0)->src;
166
                $media_image = Helper::imageUrlCleaner($media_image);
167
                $mediaography[$mediaography_index]['image'] = $media_image;
168
169
                $each_media = $each_media->find('td', 1);
170
171
                // id
172
                $media_id = $each_media->find('a', 0)->href;
173
                $parsed_media_id = explode('/', $media_id);
174
                $media_id = $parsed_media_id[4];
175
                $mediaography[$mediaography_index]['id'] = $media_id;
176
177
                // title
178
                $media_title = $each_media->find('a', 0)->plaintext;
179
                $mediaography[$mediaography_index]['title'] = $media_title;
180
181
                // role
182
                $media_role = $each_media->find('div small', 0)->plaintext;
183
                $mediaography[$mediaography_index]['role'] = $media_role;
184
185
                $mediaography_index++;
186
            }
187
        }
188
        return $mediaography;
189
    }
190
191
    /**
192
     * Get character voice actor list.
193
     *
194
     * @return array
195
     */
196
    private function getVa()
197
    {
198
        $va = [];
199
        $va_index = 0;
200
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
201
        $va_area = $html->find('div[class=normal_header]', 1);
202
        $va_area = $va_area->next_sibling();
203
        if ($va_area->tag == 'table') {
204
            while (true) {
205
206
                // id
207
                $va_name_area = $va_area->find('td', 1);
208
                $va_id = $va_name_area->find('a', 0)->href;
209
                $parsed_va_id = explode('/', $va_id);
210
                $va_id = $parsed_va_id[4];
211
                $va[$va_index]['id'] = $va_id;
212
213
                // name
214
                $va_name = $va_name_area->find('a', 0)->plaintext;
215
                $va[$va_index]['name'] = $va_name;
216
217
                // role
218
                $va_role = $va_name_area->find('small', 0)->plaintext;
219
                $va[$va_index]['role'] = $va_role;
220
221
                // image
222
                $va_image = $va_area->find('img', 0)->src;
223
                $va_image = Helper::imageUrlCleaner($va_image);
224
                $va[$va_index]['image'] = $va_image;
225
226
                $va_area = $va_area->next_sibling();
227
                if ($va_area->tag != 'table') {
228
                    break;
229
                } else {
230
                    $va_index++;
231
                }
232
            }
233
        }
234
        return $va;
235
    }
236
237
    /**
238
     * Get character all information.
239
     *
240
     * @return array
241
     */
242
    private function getAllInfo()
243
    {
244
        $data = [
245
            'id'           => self::getId(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\CharacterModel::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

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

246
            'image'        => self::/** @scrutinizer ignore-call */ getImage(),
Loading history...
247
            'nickname'     => self::getNickname(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General...terModel::getNickname() 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

247
            'nickname'     => self::/** @scrutinizer ignore-call */ getNickname(),
Loading history...
248
            'name'         => self::getName(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\CharacterModel::getName() 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

248
            'name'         => self::/** @scrutinizer ignore-call */ getName(),
Loading history...
249
            'name_kanji'   => self::getName(true),
250
            'favorite'     => self::getFavorite(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General...terModel::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

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

251
            'about'        => self::/** @scrutinizer ignore-call */ getAbout(),
Loading history...
252
            'animeography' => self::getMedia('anime'),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General...racterModel::getMedia() 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

252
            'animeography' => self::/** @scrutinizer ignore-call */ getMedia('anime'),
Loading history...
253
            'mangaography' => self::getMedia('manga'),
254
            'va'           => self::getVa(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\CharacterModel::getVa() 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

254
            'va'           => self::/** @scrutinizer ignore-call */ getVa(),
Loading history...
255
        ];
256
257
        return $data;
258
    }
259
}