Skip to content

Commit

Permalink
Update handling of null aggregate values (#43)
Browse files Browse the repository at this point in the history
Updated methods to handle null values for aggregate calculations by
changing the return type of the getActualAggregate function to '?float',
so that it can now allow null values. This change provides more
flexibility by not forcing a 0 value return when the aggregate
calculation is not possible and aids in better error management and
debugging.
  • Loading branch information
SmetDenis committed Mar 17, 2024
1 parent 9aa8a3c commit 957bd06
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
17 changes: 14 additions & 3 deletions src/Rules/Aggregate/AbstarctAggregateRuleCombo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ abstract class AbstarctAggregateRuleCombo extends AbstarctRuleCombo
self::MAX => ['10.123', ''],
];

abstract protected function getActualAggregate(array $colValues): float;
abstract protected function getActualAggregate(array $colValues): ?float;

protected function getActual(array|string $value): float
{
if (\is_string($value)) {
throw new \InvalidArgumentException('The value should be an array of numbers/strings');
}

return $this->getActualAggregate($value);
$result = $this->getActualAggregate($value);

return $result ?? 0;
}

protected function getExpected(): float
Expand All @@ -49,7 +51,16 @@ protected function validateComboAggregate(array $colValues, string $mode): ?stri
$verb = static::VERBS[$mode];
$name = static::NAME;

$actual = $this->getActual($colValues);
try {
$actual = $this->getActualAggregate($colValues); // Important to use the original method here!
} catch (\Throwable) {
$actual = null;
}

if ($actual === null) {
return null; // Looks like it's impossible to calculate the aggregate value in this case. Skip.
}

$expected = $this->getExpected();

if (!self::compare($expected, $actual, $mode)) {
Expand Down
8 changes: 2 additions & 6 deletions src/Rules/Aggregate/ComboAverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ final class ComboAverage extends AbstarctAggregateRuleCombo

protected const HELP_TOP = ['Regular the arithmetic mean. The sum of the numbers divided by the count.'];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
try {
return Average::mean(self::stringsToFloat($colValues));
} catch (\Exception) {
return 0;
}
return Average::mean(self::stringsToFloat($colValues));
}
}
2 changes: 1 addition & 1 deletion src/Rules/Aggregate/ComboCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ComboCount extends AbstarctAggregateRuleCombo
self::MAX => ['10', ''],
];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
return \count($colValues);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Aggregate/ComboCountEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ComboCountEmpty extends AbstarctAggregateRuleCombo
self::MAX => ['10', ''],
];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
return \count(\array_filter($colValues, static fn ($colValue) => $colValue === ''));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Aggregate/ComboCountNotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ComboCountNotEmpty extends AbstarctAggregateRuleCombo
self::MAX => ['10', ''],
];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
return \count(\array_filter($colValues, static fn ($colValue) => $colValue !== ''));
}
Expand Down
8 changes: 2 additions & 6 deletions src/Rules/Aggregate/ComboMedian.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ final class ComboMedian extends AbstarctAggregateRuleCombo

protected const HELP_TOP = ['Calculate the median average of a list of numbers.'];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
try {
return Average::median(self::stringsToFloat($colValues));
} catch (\Exception) {
return 0;
}
return Average::median(self::stringsToFloat($colValues));
}
}
2 changes: 1 addition & 1 deletion src/Rules/Aggregate/ComboSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ComboSum extends AbstarctAggregateRuleCombo

protected const HELP_TOP = ['Sum of the numbers in the column. Example: [1, 2, 3] => 6.'];

protected function getActualAggregate(array $colValues): float
protected function getActualAggregate(array $colValues): ?float
{
return \array_sum(self::stringsToFloat($colValues));
}
Expand Down

0 comments on commit 957bd06

Please sign in to comment.