Passed
Branch develop (3b935d)
by axel
03:07 queued 01:11
created

AllReviewModel::getAllInfo()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 2
nop 0
dl 0
loc 31
rs 9.568
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
 * AllReviewModel class.
10
 */
11
class AllReviewModel extends MainModel
12
{
13
    /**
14
     * Either anime, manga or bestvoted.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Page number.
22
     *
23
     * @var int
24
     */
25
    private $_page;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string     $type
31
     * @param string|int $page
32
     * @param string     $parserArea
33
     *
34
     * @return void
35
     */
36
    public function __construct($type, $page, $parserArea = '#content')
37
    {
38
        $this->_type = $type;
39
        $this->_page = $page;
0 ignored issues
show
Documentation Bug introduced by
It seems like $page can also be of type string. However, the property $_page is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
40
        if ($type != 'bestvoted') {
41
            $this->_url = $this->_myAnimeListUrl.'/reviews.php?t='.$type.'&p='.$page;
42
        } else {
43
            $this->_url = $this->_myAnimeListUrl.'/reviews.php?st='.$type.'&p='.$page;
44
        }
45
        $this->_parserArea = $parserArea;
46
47
        parent::errorCheck($this);
48
    }
49
50
    /**
51
     * Default call.
52
     *
53
     * @param string $method
54
     * @param array  $arguments
55
     *
56
     * @return array|string|int
57
     */
58
    public function __call($method, $arguments)
59
    {
60
        if ($this->_error) {
61
            return $this->_error;
62
        }
63
64
        return call_user_func_array([$this, $method], $arguments);
65
    }
66
67
    /**
68
     * Get type.
69
     *
70
     * @return string
71
     */
72
    private function getType()
73
    {
74
        return 'anime';
75
    }
76
77
    /**
78
     * Get page.
79
     *
80
     * @return string
81
     */
82
    private function getPage()
83
    {
84
        return $this->_page;
85
    }
86
87
    /**
88
     * Get review user.
89
     *
90
     * @param \simplehtmldom_1_5\simple_html_dom $very_bottom_area
91
     *
92
     * @return string
93
     */
94
    private function getReviewId($very_bottom_area)
95
    {
96
        $id = $very_bottom_area->find('a', 0)->href;
97
        $id = explode('?id=', $id);
98
99
        return $id[1];
100
    }
101
102
    /**
103
     * Get review source.
104
     *
105
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
106
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
107
     *
108
     * @return array
109
     */
110
    private function getReviewSource($top_area, $bottom_area)
111
    {
112
        $source_area = $top_area->find('.mb8', 1);
113
114
        return [
115
            'type' => $this->getSourceType($source_area),
116
            'id'   => $this->getSourceId($source_area),
117
            'title'=> $this->getSourceTitle($source_area),
118
            'image'=> $this->getSourceImage($bottom_area),
119
        ];
120
    }
121
122
    /**
123
     * Get source type.
124
     *
125
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
126
     *
127
     * @return string
128
     */
129
    private function getSourceType($source_area)
130
    {
131
        $type = $source_area->find('small', 0)->plaintext;
132
        $type = str_replace(['(', ')'], '', $type);
133
134
        return strtolower($type);
135
    }
136
137
    /**
138
     * Get source id.
139
     *
140
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
141
     *
142
     * @return string
143
     */
144
    private function getSourceId($source_area)
145
    {
146
        $id = $source_area->find('strong a', 0)->href;
147
        $id = explode('/', $id);
148
149
        return $id[4];
150
    }
151
152
    /**
153
     * Get source title.
154
     *
155
     * @param \simplehtmldom_1_5\simple_html_dom $source_area
156
     *
157
     * @return string
158
     */
159
    private function getSourceTitle($source_area)
160
    {
161
        $title = $source_area->find('strong', 0)->plaintext;
162
163
        return trim($title);
164
    }
165
166
    /**
167
     * Get source image.
168
     *
169
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
170
     *
171
     * @return string
172
     */
173
    private function getSourceImage($bottom_area)
174
    {
175
        $image = $bottom_area->find('.picSurround img', 0)->getAttribute('data-src');
176
177
        return Helper::imageUrlCleaner($image);
178
    }
179
180
    /**
181
     * Get review id.
182
     *
183
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
184
     *
185
     * @return string
186
     */
187
    private function getReviewUser($top_area)
188
    {
189
        $user = $top_area->find('table', 0);
190
191
        return $user->find('td', 1)->find('a', 0)->plaintext;
192
    }
193
194
    /**
195
     * Get review image.
196
     *
197
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
198
     *
199
     * @return string
200
     */
201
    private function getReviewImage($top_area)
202
    {
203
        $image = $top_area->find('table', 0);
204
        $image = $image->find('td', 0)->find('img', 0)->src;
205
206
        return Helper::imageUrlCleaner($image);
207
    }
208
209
    /**
210
     * Get review helful.
211
     *
212
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
213
     *
214
     * @return string
215
     */
216
    private function getReviewHelpful($top_area)
217
    {
218
        $helpful = $top_area->find('table', 0);
219
        $helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext;
220
221
        return trim($helpful);
222
    }
223
224
    /**
225
     * Get review date.
226
     *
227
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
228
     *
229
     * @return array
230
     */
231
    private function getReviewDate($top_area)
232
    {
233
        $date = $top_area->find('div div', 0);
234
235
        return [
236
            'date' => $date->plaintext,
237
            'time' => $date->title,
238
        ];
239
    }
240
241
    /**
242
     * Get review episode seen.
243
     *
244
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
245
     *
246
     * @return string
247
     */
248
    private function getReviewEpisode($top_area)
249
    {
250
        $episode = $top_area->find('div div', 1)->plaintext;
251
        $episode = str_replace(['episodes seen', 'chapters read'], '', $episode);
252
253
        return trim($episode);
254
    }
255
256
    /**
257
     * Get review score.
258
     *
259
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
260
     *
261
     * @return array
262
     */
263
    private function getReviewScore($bottom_area)
264
    {
265
        $score = [];
266
        $score_area = $bottom_area->find('table', 0);
267
        if ($score_area) {
268
            foreach ($score_area->find('tr') as $each_score) {
269
                $score_type = strtolower($each_score->find('td', 0)->plaintext);
270
                $score_value = $each_score->find('td', 1)->plaintext;
271
                $score[$score_type] = $score_value;
272
            }
273
        }
274
275
        return $score;
276
    }
277
278
    /**
279
     * Get review text.
280
     *
281
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
282
     *
283
     * @return string
284
     */
285
    private function getReviewText($bottom_area)
286
    {
287
        $useless_area = $bottom_area->find('div', 0);
288
        $useless_area_1 = $useless_area->plaintext;
289
        $useless_area_2 = $useless_area->next_sibling()->plaintext;
290
        $useless_area_3 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext;
291
        $useless_area_4 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null;
292
        $text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3, $useless_area_4], '', $bottom_area->plaintext);
293
        $text = str_replace('&lt;', '<', $text);
294
295
        return trim(preg_replace('/\h+/', ' ', $text));
296
    }
297
298
    /**
299
     * Get anime/mange review.
300
     *
301
     * @return array
302
     */
303
    private function getAllInfo()
304
    {
305
        $data = [];
306
        $review_area = $this->_parser->find('.borderDark');
307
        if (count($review_area) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $review_area 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

307
        if (count(/** @scrutinizer ignore-type */ $review_area) > 0) {
Loading history...
308
            foreach ($this->_parser->find('.borderDark') as $each_review) {
309
                $tmp = [];
310
311
                $top_area = $each_review->find('.spaceit', 0);
312
                $bottom_area = $top_area->next_sibling();
313
                $very_bottom_area = $bottom_area->next_sibling();
314
315
                $tmp['id'] = $this->getReviewId($very_bottom_area);
316
                $tmp['source'] = $this->getReviewSource($top_area, $bottom_area);
317
                $tmp['username'] = $this->getReviewUser($top_area);
318
                $tmp['image'] = $this->getReviewImage($top_area);
319
                $tmp['helpful'] = $this->getReviewHelpful($top_area);
320
                $tmp['date'] = $this->getReviewDate($top_area);
321
                if ($this->_type == 'anime') {
322
                    $tmp['episode'] = $this->getReviewEpisode($top_area);
323
                } else {
324
                    $tmp['chapter'] = $this->getReviewEpisode($top_area);
325
                }
326
                $tmp['score'] = $this->getReviewScore($bottom_area);
327
                $tmp['review'] = $this->getReviewText($bottom_area);
328
329
                $data[] = $tmp;
330
            }
331
        }
332
333
        return $data;
334
    }
335
}
336