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

PeopleModel::getBiodata()   C

Complexity

Conditions 16
Paths 79

Size

Total Lines 48
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 37
nc 79
nop 1
dl 0
loc 48
rs 5.5666
c 0
b 0
f 0

How to fix   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\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * PeopleModel class.
10
 */
11
class PeopleModel extends MainModel
12
{
13
    /**
14
     * Id of the people.
15
     *
16
     * @var string|int
17
     */
18
	private $_id;
19
20
    /**
21
     * Biodata area.
22
     *
23
     * @var string
24
     */
25
    private $_biodata;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string|int $id
31
     * @param string $parserArea
32
     *
33
     * @return void
34
     */
35
	public function __construct($id, $parserArea = '#contentWrapper')
36
    {
37
    	$this->_id = $id;
38
        $this->_url = $this->_myAnimeListUrl.'/people/'.$id;
39
    	$this->_parserArea = $parserArea;
40
41
        parent::errorCheck($this);
42
43
        if (!$this->_error)
44
            self::setBiodata();
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\PeopleModel::setBiodata() 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

44
            self::/** @scrutinizer ignore-call */ 
45
                  setBiodata();
Loading history...
45
    }
46
47
    /**
48
     * Default call.
49
     *
50
     * @param string $method
51
     * @param array  $arguments
52
     *
53
     * @return array|string|int
54
     */
55
    public function __call($method, $arguments)
56
    {
57
        if ($this->_error)
58
            return $this->_error;
59
        return call_user_func_array([$this, $method], $arguments);
60
    }
61
62
    /**
63
     * Get people id.
64
     *
65
     * @return string
66
     */
67
    private function getId()
68
    {
69
        return $this->_id;
70
    }
71
72
    /**
73
     * Get people name.
74
     *
75
     * @return string
76
     */
77
    private function getName()
78
    {
79
        return $this->_parser->find('h1', 0)->plaintext;
80
    }
81
82
    /**
83
     * Get people image.
84
     *
85
     * @return string
86
     */
87
    private function getImage()
88
    {
89
        $image = $this->_parser->find('#content table tr', 0)->find('td', 0)->find('img', 0);
90
        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...
91
    }
92
93
    /**
94
     * Set people biodata.
95
     *
96
     * @return void
97
     */
98
    private function setBiodata()
99
    {
100
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0);
101
        $biodata = $html->innertext;
102
        $useless_biodata = '';
103
        $useless_area = $html->find('div', 0);
104
        for ($i = 0; $i < 4; $i++) {
105
            $useless_biodata .= $useless_area->outertext;
106
            $useless_area = $useless_area->next_sibling();
107
        }
108
        $biodata = str_replace($useless_biodata, '', $biodata);
109
        $this->_biodata = preg_replace("/([\s])+/", ' ', $biodata);
110
    }
111
112
    /**
113
     * Get people biodata.
114
     *
115
     * @param string $type Biodata type
116
     *
117
     * @return string|array
118
     */
119
    private function getBiodata($type)
120
    {
121
        switch ($type) {
122
            case 'given_name':
123
                preg_match("/(Given name:<\/span>)[^<]*/", $this->_biodata, $biodata);
124
                break;
125
            case 'family_name':
126
                preg_match("/(Family name:<\/span>)[^<]*/", $this->_biodata, $biodata);
127
                break;
128
            case 'alternative_name':
129
                preg_match("/(Alternate names:<\/span>)[^<]*/", $this->_biodata, $biodata);
130
                break;
131
            case 'birthday':
132
                preg_match("/(Birthday:<\/span>)([^<])*/", $this->_biodata, $biodata);
133
                break;
134
            case 'website':
135
                preg_match("/(Website:<\/span> <a)([^<])*/", $this->_biodata, $biodata);
136
                break;
137
            case 'favorite':
138
                preg_match("/(Member Favorites:<\/span>)([^<])*/", $this->_biodata, $biodata);
139
                break;
140
            default:
141
                return null;
142
        }
143
144
        if ($biodata) {
145
            if ($type != 'website') {
146
                $biodata = strip_tags($biodata[0]);
147
                $biodata = explode(': ', $biodata);
148
                $biodata = trim($biodata[1]);
149
            }
150
151
            if ($type == 'given_name' || $type == 'family_name' || $type == 'birthday')
152
                return $biodata;
153
154
            if ($type == 'alternative_name')
155
                return explode(', ', $biodata);
156
157
            if ($type == 'favorite')
158
                return str_replace(',', '', $biodata);
159
160
            if ($type == 'website') {
161
                preg_match('/".+"/', $biodata[0], $biodata);
162
                if ($biodata[0] != '"http://"')
163
                    return str_replace('"', '', $biodata[0]);
164
            }
165
        }
166
        return null;
167
    }
168
169
    /**
170
     * Get people more information.
171
     *
172
     * @return string
173
     */
174
    private function getMore()
175
    {
176
        $more = $this->_parser->find('#content table tr', 0)->find('td', 0);
177
        $more = $more->find('div[class^=people-informantion-more]', 0)->plaintext;
178
        return preg_replace('/\n[^\S\n]*/', "\n", $more);
179
    }
180
181
    /**
182
     * Get people voice actor list.
183
     *
184
     * @return array
185
     */
186
    private function getVa()
187
    {
188
        $va = [];
189
        $va_index = 0;
190
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
191
        $va_area = $html->find('.normal_header', 0)->next_sibling();
192
        if ($va_area->tag == 'table') {
193
            if ($va_area->find('tr')) {
194
                foreach ($va_area->find('tr') as $each_va) {
195
                    // anime image
196
                    $anime_image = $each_va->find('td', 0)->find('img', 0)->getAttribute('data-src');
197
                    $va[$va_index]['anime']['image'] = Helper::imageUrlCleaner($anime_image);
198
199
                    $anime_area = $each_va->find('td', 1);
200
201
                    // anime id
202
                    $anime_id = $anime_area->find('a', 0)->href;
203
                    $parsed_anime_id = explode('/', $anime_id);
204
                    $anime_id = $parsed_anime_id[4];
205
                    $va[$va_index]['anime']['id'] = $anime_id;
206
207
                    // anime title
208
                    $anime_title = $anime_area->find('a', 0)->plaintext;
209
                    $va[$va_index]['anime']['title'] = $anime_title;
210
211
                    // character image
212
                    $character_image = $each_va->find('td', 3)->find('img', 0)->getAttribute('data-src');
213
                    $va[$va_index]['character']['image'] = Helper::imageUrlCleaner($character_image);
214
215
                    $character_area = $each_va->find('td', 2);
216
217
                    // character id
218
                    $character_id = $character_area->find('a', 0)->href;
219
                    $parsed_character_id = explode('/', $character_id);
220
                    $character_id = $parsed_character_id[4];
221
                    $va[$va_index]['character']['id'] = $character_id;
222
223
                    // character name
224
                    $character_name = $character_area->find('a', 0)->plaintext;
225
                    $va[$va_index]['character']['name'] = $character_name;
226
227
                    // character role
228
                    $character_role = $character_area->find('div', 0)->plaintext;
229
                    $va[$va_index]['character']['role'] = $character_role;
230
231
                    $va_index++;
232
                }
233
            }
234
        }
235
        return $va;
236
    }
237
238
    /**
239
     * Get people staff list.
240
     *
241
     * @return array
242
     */
243
    private function getStaff()
244
    {
245
        $staff = [];
246
        $staff_index = 0;
247
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
248
        $staff_area = $html->find('.normal_header', 1)->next_sibling();
249
        if ($staff_area->tag == 'table') {
250
            foreach ($staff_area->find('tr') as $each_staff) {
251
                $anime_image = $each_staff->find('td', 0)->find('img', 0)->getAttribute('data-src');
252
                $staff[$staff_index]['image'] = Helper::imageUrlCleaner($anime_image);
253
254
                $each_staff = $each_staff->find('td', 1);
255
256
                // anime id
257
                $anime_id = $each_staff->find('a', 0)->href;
258
                $parsed_anime_id = explode('/', $anime_id);
259
                $anime_id = $parsed_anime_id[4];
260
                $staff[$staff_index]['id'] = $anime_id;
261
262
                // anime title
263
                $anime_title = $each_staff->find('a', 0)->plaintext;
264
                $staff[$staff_index]['title'] = $anime_title;
265
266
                // role
267
                $role = $each_staff->find('small', 0)->plaintext;
268
                $staff[$staff_index]['role'] = $role;
269
270
                $staff_index++;
271
            }
272
        }
273
        return $staff;
274
    }
275
276
    /**
277
     * Get people published manga list.
278
     *
279
     * @return array
280
     */
281
    private function getManga()
282
    {
283
        $published_manga = [];
284
        $manga_index = 0;
285
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
286
        $manga_area = $html->find('.normal_header', 2)->next_sibling();
287
        if ($manga_area->tag == 'table') {
288
            foreach ($manga_area->find('tr') as $each_manga) {
289
                $manga_image = $each_manga->find('td', 0)->find('img', 0)->getAttribute('data-src');
290
                $published_manga[$manga_index]['image'] = Helper::imageUrlCleaner($manga_image);
291
292
                $each_manga = $each_manga->find('td', 1);
293
294
                // manga id
295
                $manga_id = $each_manga->find('a', 0)->href;
296
                $parsed_manga_id = explode('/', $manga_id);
297
                $manga_id = $parsed_manga_id[4];
298
                $published_manga[$manga_index]['id'] = $manga_id;
299
300
                // manga title
301
                $manga_title = $each_manga->find('a', 0)->plaintext;
302
                $published_manga[$manga_index]['title'] = $manga_title;
303
304
                // role
305
                $role = $each_manga->find('small', 0)->plaintext;
306
                $published_manga[$manga_index]['role'] = $role;
307
308
                $manga_index++;
309
            }
310
        }
311
        return $published_manga;
312
    }
313
314
    /**
315
     * Get people all information.
316
     *
317
     * @return array
318
     */
319
    private function getAllInfo()
320
    {
321
        $data = [
322
            'id'               => self::getId(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\PeopleModel::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

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

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

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

325
            'given_name'       => self::/** @scrutinizer ignore-call */ getBiodata('given_name'),
Loading history...
326
            'family_name'      => self::getBiodata('family_name'),
327
            'alternative_name' => self::getBiodata('alternative_name'),
328
            'birthday'         => self::getBiodata('birthday'),
329
            'website'          => self::getBiodata('website'),
330
            'favorite'         => self::getBiodata('favorite'),
331
            'more'             => self::getMore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\PeopleModel::getMore() 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

331
            'more'             => self::/** @scrutinizer ignore-call */ getMore(),
Loading history...
332
            'va'               => self::getVa(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\General\PeopleModel::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

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

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

334
            'published_manga'  => self::/** @scrutinizer ignore-call */ getManga(),
Loading history...
335
        ];
336
337
        return $data;
338
    }
339
}