ReviewsExtractor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 73
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractReviews() 0 9 2
A extractReplyReview() 0 15 4
A extractReview() 0 25 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (c) Ne-Lexa
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 *
11
 * @see https://github.com/Ne-Lexa/google-play-scraper
12
 */
13
14
namespace Nelexa\GPlay\Scraper\Extractor;
15
16
use Nelexa\GPlay\Model\AppId;
17
use Nelexa\GPlay\Model\GoogleImage;
18
use Nelexa\GPlay\Model\ReplyReview;
19
use Nelexa\GPlay\Model\Review;
20
use Nelexa\GPlay\Util\DateStringFormatter;
21
22
/**
23
 * @internal
24
 */
25
class ReviewsExtractor
26
{
27
    /**
28
     * @param AppId $requestApp
29
     * @param array $data
30
     *
31
     * @return array
32
     */
33 11
    public static function extractReviews(AppId $requestApp, array $data): array
34
    {
35 11
        $reviews = [];
36
37 11
        foreach ($data as $reviewData) {
38 11
            $reviews[] = self::extractReview($requestApp, $reviewData);
39
        }
40
41 11
        return $reviews;
42
    }
43
44
    /**
45
     * @param AppId $requestApp
46
     * @param       $reviewData
47
     *
48
     * @return Review
49
     */
50 11
    public static function extractReview(AppId $requestApp, array $reviewData): Review
0 ignored issues
show
Unused Code introduced by
The parameter $requestApp is not used and could be removed. ( Ignorable by Annotation )

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

50
    public static function extractReview(/** @scrutinizer ignore-unused */ AppId $requestApp, array $reviewData): Review

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52 11
        $reviewId = $reviewData[0];
53
//        $reviewUrl = $requestApp->getUrl() . '&reviewId=' . urlencode($reviewId);
54 11
        $userName = $reviewData[1][0];
55 11
        $avatar = (new GoogleImage($reviewData[1][1][3][2]))->setSize(64);
56 11
        $date = DateStringFormatter::unixTimeToDateTime($reviewData[5][0]);
57 11
        $score = $reviewData[2] ?? 0;
58 11
        $text = (string) ($reviewData[4] ?? '');
59 11
        $likeCount = $reviewData[6];
60 11
        $appVersion = $reviewData[10] ?? null;
61
62 11
        $reply = self::extractReplyReview($reviewData);
63
64 11
        return new Review(
65
            $reviewId,
66
//            $reviewUrl,
67
            $userName,
68
            $text,
69
            $avatar,
70
            $date,
71
            $score,
72
            $likeCount,
73
            $reply,
74
            $appVersion
75
        );
76
    }
77
78
    /**
79
     * @param array $reviewData
80
     *
81
     * @return ReplyReview|null
82
     */
83 11
    private static function extractReplyReview(array $reviewData): ?ReplyReview
84
    {
85 11
        if (isset($reviewData[7][1])) {
86 6
            $replyText = $reviewData[7][1];
87 6
            $replyDate = DateStringFormatter::unixTimeToDateTime($reviewData[7][2][0]);
88
89 6
            if ($replyText && $reviewData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reviewData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
90 6
                return new ReplyReview(
91
                    $replyDate,
92
                    $replyText
93
                );
94
            }
95
        }
96
97 11
        return null;
98
    }
99
}
100