@@ -45,14 +45,14 @@ discard block |
||
45 | 45 | * @param int $roundBy |
46 | 46 | * @return float |
47 | 47 | */ |
48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
48 | + public function getAverage( array $ratingCounts, $roundBy = 1 ) |
|
49 | 49 | { |
50 | - $average = array_sum($ratingCounts); |
|
51 | - if ($average > 0) { |
|
52 | - $average = $this->getTotalSum($ratingCounts) / $average; |
|
50 | + $average = array_sum( $ratingCounts ); |
|
51 | + if( $average > 0 ) { |
|
52 | + $average = $this->getTotalSum( $ratingCounts ) / $average; |
|
53 | 53 | } |
54 | - $roundedAverage = round($average, intval($roundBy)); |
|
55 | - return floatval(apply_filters('site-reviews/rating/average', $roundedAverage, $ratingCounts, $average)); |
|
54 | + $roundedAverage = round( $average, intval( $roundBy ) ); |
|
55 | + return floatval( apply_filters( 'site-reviews/rating/average', $roundedAverage, $ratingCounts, $average ) ); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -65,50 +65,50 @@ discard block |
||
65 | 65 | * @param int $confidencePercentage |
66 | 66 | * @return int|float |
67 | 67 | */ |
68 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
68 | + public function getLowerBound( array $upDownCounts = [0, 0], $confidencePercentage = 95 ) |
|
69 | 69 | { |
70 | - $numRatings = array_sum($upDownCounts); |
|
71 | - if ($numRatings < 1) { |
|
70 | + $numRatings = array_sum( $upDownCounts ); |
|
71 | + if( $numRatings < 1 ) { |
|
72 | 72 | return 0; |
73 | 73 | } |
74 | 74 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
75 | 75 | $phat = 1 * $upDownCounts[1] / $numRatings; |
76 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
76 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt( ($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings )) / (1 + $z * $z / $numRatings); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | /** |
80 | 80 | * @return int|float |
81 | 81 | */ |
82 | - public function getOverallPercentage(array $ratingCounts) |
|
82 | + public function getOverallPercentage( array $ratingCounts ) |
|
83 | 83 | { |
84 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING', __CLASS__), 2); |
|
84 | + return round( $this->getAverage( $ratingCounts ) * 100 / glsr()->constant( 'MAX_RATING', __CLASS__ ), 2 ); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | 88 | * @return array |
89 | 89 | */ |
90 | - public function getPercentages(array $ratingCounts) |
|
90 | + public function getPercentages( array $ratingCounts ) |
|
91 | 91 | { |
92 | - $total = array_sum($ratingCounts); |
|
93 | - foreach ($ratingCounts as $index => $count) { |
|
94 | - if (empty($count)) { |
|
92 | + $total = array_sum( $ratingCounts ); |
|
93 | + foreach( $ratingCounts as $index => $count ) { |
|
94 | + if( empty($count) ) { |
|
95 | 95 | continue; |
96 | 96 | } |
97 | 97 | $ratingCounts[$index] = $count / $total * 100; |
98 | 98 | } |
99 | - return $this->getRoundedPercentages($ratingCounts); |
|
99 | + return $this->getRoundedPercentages( $ratingCounts ); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | /** |
103 | 103 | * @return float |
104 | 104 | */ |
105 | - public function getRanking(array $ratingCounts) |
|
105 | + public function getRanking( array $ratingCounts ) |
|
106 | 106 | { |
107 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
108 | - $this->getRankingUsingImdb($ratingCounts), |
|
107 | + return floatval( apply_filters( 'site-reviews/rating/ranking', |
|
108 | + $this->getRankingUsingImdb( $ratingCounts ), |
|
109 | 109 | $ratingCounts, |
110 | 110 | $this |
111 | - )); |
|
111 | + ) ); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
@@ -121,15 +121,15 @@ discard block |
||
121 | 121 | * @param int $confidencePercentage |
122 | 122 | * @return int|float |
123 | 123 | */ |
124 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
124 | + public function getRankingUsingImdb( array $ratingCounts, $confidencePercentage = 70 ) |
|
125 | 125 | { |
126 | - $avgRating = $this->getAverage($ratingCounts); |
|
126 | + $avgRating = $this->getAverage( $ratingCounts ); |
|
127 | 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 | 128 | // This could also be the average score of all items instead of a fixed value. |
129 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING', __CLASS__); // prior, 70% = 3.5 |
|
129 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant( 'MAX_RATING', __CLASS__ ); // prior, 70% = 3.5 |
|
130 | 130 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
131 | 131 | $bayesMinimal = 10; // confidence |
132 | - $numOfReviews = array_sum($ratingCounts); |
|
132 | + $numOfReviews = array_sum( $ratingCounts ); |
|
133 | 133 | return $avgRating > 0 |
134 | 134 | ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
135 | 135 | : 0; |
@@ -145,48 +145,48 @@ discard block |
||
145 | 145 | * @param int $confidencePercentage |
146 | 146 | * @return float |
147 | 147 | */ |
148 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
148 | + public function getRankingUsingZScores( array $ratingCounts, $confidencePercentage = 90 ) |
|
149 | 149 | { |
150 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING', __CLASS__); |
|
151 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
152 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
150 | + $ratingCountsSum = array_sum( $ratingCounts ) + glsr()->constant( 'MAX_RATING', __CLASS__ ); |
|
151 | + $weight = $this->getWeight( $ratingCounts, $ratingCountsSum ); |
|
152 | + $weightPow2 = $this->getWeight( $ratingCounts, $ratingCountsSum, true ); |
|
153 | 153 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
154 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
154 | + return $weight - $zScore * sqrt( ($weightPow2 - pow( $weight, 2 )) / ($ratingCountsSum + 1) ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
158 | 158 | * @param int $target |
159 | 159 | * @return array |
160 | 160 | */ |
161 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
161 | + protected function getRoundedPercentages( array $percentages, $totalPercent = 100 ) |
|
162 | 162 | { |
163 | - array_walk($percentages, function (&$percent, $index) { |
|
163 | + array_walk( $percentages, function( &$percent, $index ) { |
|
164 | 164 | $percent = [ |
165 | 165 | 'index' => $index, |
166 | - 'percent' => floor($percent), |
|
167 | - 'remainder' => fmod($percent, 1), |
|
166 | + 'percent' => floor( $percent ), |
|
167 | + 'remainder' => fmod( $percent, 1 ), |
|
168 | 168 | ]; |
169 | 169 | }); |
170 | - $indexes = glsr_array_column($percentages, 'index'); |
|
171 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
172 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
170 | + $indexes = glsr_array_column( $percentages, 'index' ); |
|
171 | + $remainders = glsr_array_column( $percentages, 'remainder' ); |
|
172 | + array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
173 | 173 | $i = 0; |
174 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
175 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
174 | + if( array_sum( glsr_array_column( $percentages, 'percent' ) ) > 0 ) { |
|
175 | + while( array_sum( glsr_array_column( $percentages, 'percent' ) ) < $totalPercent ) { |
|
176 | 176 | ++$percentages[$i]['percent']; |
177 | 177 | ++$i; |
178 | 178 | } |
179 | 179 | } |
180 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
181 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
180 | + array_multisort( $indexes, SORT_DESC, $percentages ); |
|
181 | + return array_combine( $indexes, glsr_array_column( $percentages, 'percent' ) ); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
185 | 185 | * @return int |
186 | 186 | */ |
187 | - protected function getTotalSum(array $ratingCounts) |
|
187 | + protected function getTotalSum( array $ratingCounts ) |
|
188 | 188 | { |
189 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
189 | + return array_reduce( array_keys( $ratingCounts ), function( $carry, $index ) use ($ratingCounts) { |
|
190 | 190 | return $carry + ($index * $ratingCounts[$index]); |
191 | 191 | }); |
192 | 192 | } |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | * @param bool $powerOf2 |
197 | 197 | * @return float |
198 | 198 | */ |
199 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
199 | + protected function getWeight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
200 | 200 | { |
201 | - return array_reduce(array_keys($ratingCounts), |
|
202 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
201 | + return array_reduce( array_keys( $ratingCounts ), |
|
202 | + function( $count, $rating ) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
203 | 203 | $ratingLevel = $powerOf2 |
204 | - ? pow($rating, 2) |
|
204 | + ? pow( $rating, 2 ) |
|
205 | 205 | : $rating; |
206 | 206 | return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
207 | 207 | } |