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:
Víctor Falcón 2026-07-04 21:19:09 +02:00
parent f02cf7cd74
commit e8b6e62557
1 changed files with 6 additions and 3 deletions

View File

@ -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)