Skip to content
Snippets Groups Projects
Commit 0032a1c8 authored by Tim Rohaly's avatar Tim Rohaly
Browse files

Issue #3508988 by tr: Average function not protected against the case of 0 votes

parent ef653008
No related branches found
No related tags found
2 merge requests!50Issue #3510555 return int in getCreatedTime(),!47Resolve #3508988 "Average function not"
Pipeline #434276 passed with warnings
......@@ -22,11 +22,17 @@ class Average extends VoteResultFunctionBase {
* {@inheritdoc}
*/
public function calculateResult(array $votes): float {
/** @var \Drupal\votingapi\VoteInterface[] $votes */
$count = count($votes);
if ($count === 0) {
return 0;
}
$total = 0;
foreach ($votes as $vote) {
$total += $vote->getValue();
}
return ($total / count($votes));
return ($total / $count);
}
}
......@@ -22,6 +22,7 @@ class Count extends VoteResultFunctionBase {
* {@inheritdoc}
*/
public function calculateResult(array $votes): float {
/** @var \Drupal\votingapi\VoteInterface[] $votes */
return count($votes);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment