Guard sumTransactions against unsupported category types
Both reviewers flagged that the income/expense ternary silently treated any non-Income type as the expense side. Replace it with a match that throws on Savings/Investment/Transfer, so a future miscall fails loudly instead of returning a wrong total. Behavior is unchanged for the only current callers (Income and Expense).
This commit is contained in:
parent
f02cf7cd74
commit
e8b6e62557
|
|
@ -16,6 +16,7 @@ use Carbon\Carbon;
|
|||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class CashflowAnalyticsController extends Controller
|
||||
{
|
||||
|
|
@ -218,9 +219,11 @@ class CashflowAnalyticsController extends Controller
|
|||
|
||||
private function sumTransactions(Collection $transactions, string $userCurrency, CategoryType $type): int
|
||||
{
|
||||
$onSide = $type === CategoryType::Income
|
||||
? fn (Transaction $transaction): bool => $transaction->isIncomeSide()
|
||||
: fn (Transaction $transaction): bool => $transaction->isExpenseSide();
|
||||
$onSide = match ($type) {
|
||||
CategoryType::Income => fn (Transaction $transaction): bool => $transaction->isIncomeSide(),
|
||||
CategoryType::Expense => fn (Transaction $transaction): bool => $transaction->isExpenseSide(),
|
||||
default => throw new InvalidArgumentException("sumTransactions only supports Income and Expense, got {$type->value}."),
|
||||
};
|
||||
|
||||
return $transactions
|
||||
->filter($onSide)
|
||||
|
|
|
|||
Loading…
Reference in New Issue