Passed
Push — develop ( 535216...2a3c68 )
by Paul
07:18
created

Rating::ranking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
class Rating
6
{
7
    public const CONFIDENCE_LEVEL_Z_SCORES = [
8
        50 => 0.67449,
9
        70 => 1.04,
10
        75 => 1.15035,
11
        80 => 1.282,
12
        85 => 1.44,
13
        90 => 1.64485,
14
        92 => 1.75,
15
        95 => 1.95996,
16
        96 => 2.05,
17
        97 => 2.17009,
18
        98 => 2.326,
19
        99 => 2.57583,
20
        '99.5' => 2.81,
21
        '99.8' => 3.08,
22
        '99.9' => 3.29053,
23
    ];
24
    public const MAX_RATING = 5;
25
    public const MIN_RATING = 0;
26
27 17
    public function average(array $ratingCounts, ?int $roundBy = null): float
28
    {
29 17
        $average = array_sum($ratingCounts);
30 17
        if ($average > 0) {
31 17
            $average = $this->totalSum($ratingCounts) / $average;
32
        }
33 17
        if (is_null($roundBy)) {
34 17
            $roundBy = glsr()->filterInt('rating/round-by', 1);
35
        }
36 17
        $roundedAverage = round($average, intval($roundBy));
37 17
        return glsr()->filterFloat('rating/average', $roundedAverage, $average, $ratingCounts);
38
    }
39
40 17
    public function emptyArray(): array
41
    {
42 17
        return array_fill_keys(range(0, glsr()->constant('MAX_RATING', __CLASS__)), 0);
43
    }
44
45 8
    public function format(float $rating): string
46
    {
47 8
        $roundBy = glsr()->filterInt('rating/round-by', 1);
48 8
        return (string) number_format_i18n($rating, $roundBy);
49
    }
50
51
    public function isValid(int $rating): bool
52
    {
53
        return array_key_exists($rating, $this->emptyArray());
54
    }
55
56
    /**
57
     * Get the lower bound for up/down ratings
58
     * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1].
59
     * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
60
     * @see https://news.ycombinator.com/item?id=10481507
61
     * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments
62
     * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html
63
     */
64
    public function lowerBound(array $upDownCounts = [0, 0], int $confidencePercentage = 95): float
65
    {
66
        $numRatings = array_sum($upDownCounts);
67
        if ($numRatings < 1) {
68
            return 0;
69
        }
70
        $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage];
71
        $phat = 1 * $upDownCounts[1] / $numRatings;
72
        return (float) ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings);
73
    }
74
75
    /**
76
     * @param array $noopedPlural The result of _n_noop()
77
     */
78
    public function optionsArray(array $noopedPlural, int $minRating = 1): array
79
    {
80
        $options = [];
81
        foreach (range(glsr()->constant('MAX_RATING', __CLASS__), $minRating) as $rating) {
82
            $options[$rating] = sprintf(translate_nooped_plural($noopedPlural, $rating, 'site-reviews'), $rating);
83
        }
84
        return $options;
85
    }
86
87
    public function overallPercentage(array $ratingCounts): float
88
    {
89
        return round($this->average($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2);
90
    }
91
92
    public function percentages(array $ratingCounts): array
93
    {
94
        if (empty($ratingCounts)) {
95
            $ratingCounts = $this->emptyArray();
96
        }
97
        $total = array_sum($ratingCounts);
98
        foreach ($ratingCounts as $index => $count) {
99
            if (empty($count)) {
100
                continue;
101
            }
102
            $ratingCounts[$index] = $count / $total * 100;
103
        }
104
        return $this->roundedPercentages($ratingCounts);
105
    }
106
107 17
    public function ranking(array $ratingCounts): float
108
    {
109 17
        return glsr()->filterFloat('rating/ranking',
110 17
            $this->rankingUsingImdb($ratingCounts),
111 17
            $ratingCounts,
112 17
            $this
113 17
        );
114
    }
115
116
    /**
117
     * Get the bayesian ranking for an array of reviews
118
     * This formula is the same one used by IMDB to rank their top 250 films.
119
     * @see https://www.xkcd.com/937/
120
     * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means
121
     * @see http://fulmicoton.com/posts/bayesian_rating/
122
     * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va
123
     */
124 17
    public function rankingUsingImdb(array $ratingCounts, int $confidencePercentage = 70): float
125
    {
126 17
        $avgRating = $this->average($ratingCounts);
127
        // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error.
128
        // This could also be the average score of all items instead of a fixed value.
129 17
        $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5
130
        // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior.
131 17
        $bayesMinimal = 10; // confidence
132 17
        $numOfReviews = array_sum($ratingCounts);
133 17
        return $avgRating > 0
134 17
            ? (float) (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews)
135 17
            : (float) 0;
136
    }
137
138
    /**
139
     * The quality of a 5 star rating depends not only on the average number of stars but also on
140
     * the number of reviews. This method calculates the bayesian ranking of a page by its number
141
     * of reviews and their rating.
142
     * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html
143
     * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268
144
     * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html
145
     */
146
    public function rankingUsingZScores(array $ratingCounts, int $confidencePercentage = 90): float
147
    {
148
        $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__);
149
        $weight = $this->weight($ratingCounts, $ratingCountsSum);
0 ignored issues
show
Bug introduced by
$ratingCountsSum of type double is incompatible with the type integer expected by parameter $ratingCountsSum of GeminiLabs\SiteReviews\Modules\Rating::weight(). ( Ignorable by Annotation )

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

149
        $weight = $this->weight($ratingCounts, /** @scrutinizer ignore-type */ $ratingCountsSum);
Loading history...
150
        $weightPow2 = $this->weight($ratingCounts, $ratingCountsSum, true);
151
        $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage];
152
        return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1));
153
    }
154
155
    /**
156
     * Returns array sorted by key DESC.
157
     */
158
    protected function roundedPercentages(array $percentages, int $totalPercent = 100): array
159
    {
160
        array_walk($percentages, function (&$percent, $index) {
161
            $percent = [
162
                'index' => $index,
163
                'percent' => floor($percent),
164
                'remainder' => fmod($percent, 1),
165
            ];
166
        });
167
        $indexes = wp_list_pluck($percentages, 'index');
168
        $remainders = wp_list_pluck($percentages, 'remainder');
169
        array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages);
170
        $i = 0;
171
        if (array_sum(wp_list_pluck($percentages, 'percent')) > 0) {
172
            while (array_sum(wp_list_pluck($percentages, 'percent')) < $totalPercent) {
173
                ++$percentages[$i]['percent'];
174
                ++$i;
175
            }
176
        }
177
        array_multisort($indexes, SORT_DESC, $percentages);
178
        return array_combine($indexes, wp_list_pluck($percentages, 'percent'));
179
    }
180
181 17
    protected function totalSum(array $ratingCounts): int
182
    {
183 17
        return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) {
184 17
            return $carry + ($index * $ratingCounts[$index]);
185 17
        }, 0);
186
    }
187
188
    protected function weight(array $ratingCounts, int $ratingCountsSum, bool $powerOf2 = false): float
189
    {
190
        return (float) array_reduce(array_keys($ratingCounts),
191
            function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) {
192
                $ratingLevel = $powerOf2
193
                    ? pow($rating, 2)
194
                    : $rating;
195
                return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum;
196
            },
197
            0
198
        );
199
    }
200
}
201