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

AnimeMangaReviewModel::getReviewId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\Additional;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * AnimeMangaReviewModel class.
10
 */
11
class AnimeMangaReviewModel extends MainModel
12
{
13
    /**
14
     * Either anime or manga.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Id of the anime/manga.
22
     *
23
     * @var string|int
24
     */
25
    private $_id;
26
27
    /**
28
     * Page number.
29
     *
30
     * @var int
31
     */
32
    private $_page;
33
34
    /**
35
     * Default constructor.
36
     *
37
     * @param string     $type
38
     * @param string|int $id
39
     * @param string|int $page
40
     * @param string     $parserArea
41
     *
42
     * @return void
43
     */
44
    public function __construct($type, $id, $page, $parserArea = '.js-scrollfix-bottom-rel')
45
    {
46
        $this->_type = $type;
47
        $this->_id = $id;
48
        $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...
49
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id.'/a/reviews?p='.$page;
50
        $this->_parserArea = $parserArea;
51
52
        parent::errorCheck($this);
53
    }
54
55
    /**
56
     * Default call.
57
     *
58
     * @param string $method
59
     * @param array  $arguments
60
     *
61
     * @return array|string|int
62
     */
63
    public function __call($method, $arguments)
64
    {
65
        if ($this->_error) {
66
            return $this->_error;
67
        }
68
69
        return call_user_func_array([$this, $method], $arguments);
70
    }
71
72
    /**
73
     * Get type.
74
     *
75
     * @return string
76
     */
77
    private function getType()
78
    {
79
        return $this->_type;
80
    }
81
82
    /**
83
     * Get anime/manga id.
84
     *
85
     * @return string
86
     */
87
    private function getId()
88
    {
89
        return $this->_id;
90
    }
91
92
    /**
93
     * Get page.
94
     *
95
     * @return string
96
     */
97
    private function getPage()
98
    {
99
        return $this->_page;
100
    }
101
102
    /**
103
     * Get review user.
104
     *
105
     * @param \simplehtmldom_1_5\simple_html_dom $very_bottom_area
106
     *
107
     * @return string
108
     */
109
    private function getReviewId($very_bottom_area)
110
    {
111
        $id = $very_bottom_area->find('a', 0)->href;
112
        $id = explode('?id=', $id);
113
114
        return $id[1];
115
    }
116
117
    /**
118
     * Get review id.
119
     *
120
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
121
     *
122
     * @return string
123
     */
124
    private function getReviewUser($top_area)
125
    {
126
        $user = $top_area->find('table', 0);
127
128
        return $user->find('td', 1)->find('a', 0)->plaintext;
129
    }
130
131
    /**
132
     * Get review image.
133
     *
134
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
135
     *
136
     * @return string
137
     */
138
    private function getReviewImage($top_area)
139
    {
140
        $image = $top_area->find('table', 0);
141
        $image = $image->find('td', 0)->find('img', 0)->src;
142
143
        return Helper::imageUrlCleaner($image);
144
    }
145
146
    /**
147
     * Get review helful.
148
     *
149
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
150
     *
151
     * @return string
152
     */
153
    private function getReviewHelpful($top_area)
154
    {
155
        $helpful = $top_area->find('table', 0);
156
        $helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext;
157
158
        return trim($helpful);
159
    }
160
161
    /**
162
     * Get review date.
163
     *
164
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
165
     *
166
     * @return array
167
     */
168
    private function getReviewDate($top_area)
169
    {
170
        $date = $top_area->find('div div', 0);
171
172
        return [
173
            'date' => $date->plaintext,
174
            'time' => $date->title,
175
        ];
176
    }
177
178
    /**
179
     * Get review episode seen.
180
     *
181
     * @param \simplehtmldom_1_5\simple_html_dom $top_area
182
     *
183
     * @return string
184
     */
185
    private function getReviewEpisode($top_area)
186
    {
187
        $episode = $top_area->find('div div', 1)->plaintext;
188
        $episode = str_replace(['episodes seen', 'chapters read'], '', $episode);
189
190
        return trim($episode);
191
    }
192
193
    /**
194
     * Get review score.
195
     *
196
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
197
     *
198
     * @return array
199
     */
200
    private function getReviewScore($bottom_area)
201
    {
202
        $score = [];
203
        $score_area = $bottom_area->find('table', 0);
204
        if ($score_area) {
205
            foreach ($score_area->find('tr') as $each_score) {
206
                $score_type = strtolower($each_score->find('td', 0)->plaintext);
207
                $score_value = $each_score->find('td', 1)->plaintext;
208
                $score[$score_type] = $score_value;
209
            }
210
        }
211
212
        return $score;
213
    }
214
215
    /**
216
     * Get review text.
217
     *
218
     * @param \simplehtmldom_1_5\simple_html_dom $bottom_area
219
     *
220
     * @return string
221
     */
222
    private function getReviewText($bottom_area)
223
    {
224
        $useless_area_1 = $bottom_area->find('div', 0)->plaintext;
225
        $useless_area_2 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext;
226
        $useless_area_3 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null;
227
        $text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3], '', $bottom_area->plaintext);
228
        $text = str_replace('&lt;', '<', $text);
229
230
        return trim(preg_replace('/\h+/', ' ', $text));
231
    }
232
233
    /**
234
     * Get anime/mange review.
235
     *
236
     * @return array
237
     */
238
    private function getAllInfo()
239
    {
240
        $data = [];
241
        $review_area = $this->_parser->find('.borderDark');
242
        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

242
        if (count(/** @scrutinizer ignore-type */ $review_area) > 0) {
Loading history...
243
            foreach ($this->_parser->find('.borderDark') as $each_review) {
244
                $tmp = [];
245
246
                $top_area = $each_review->find('.spaceit', 0);
247
                $bottom_area = $top_area->next_sibling();
248
                $very_bottom_area = $bottom_area->next_sibling();
249
250
                $tmp['id'] = $this->getReviewId($very_bottom_area);
251
                $tmp['username'] = $this->getReviewUser($top_area);
252
                $tmp['image'] = $this->getReviewImage($top_area);
253
                $tmp['helpful'] = $this->getReviewHelpful($top_area);
254
                $tmp['date'] = $this->getReviewDate($top_area);
255
                if ($this->_type == 'anime') {
256
                    $tmp['episode'] = $this->getReviewEpisode($top_area);
257
                } else {
258
                    $tmp['chapter'] = $this->getReviewEpisode($top_area);
259
                }
260
                $tmp['score'] = $this->getReviewScore($bottom_area);
261
                $tmp['review'] = $this->getReviewText($bottom_area);
262
263
                $data[] = $tmp;
264
            }
265
        }
266
267
        return $data;
268
    }
269
}
270