From 80718baa1669f5a3a711653908a5c895183d94c9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 11 Nov 2020 19:14:05 +0100 Subject: [PATCH] Catch null pointer --- app/Repositories/Bill/BillRepository.php | 77 +++++++++++++----------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 8ac12398a2..9d15e78752 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -92,7 +92,7 @@ class BillRepository implements BillRepositoryInterface public function findBill(?int $billId, ?string $billName): ?Bill { if (null !== $billId) { - $searchResult = $this->find((int) $billId); + $searchResult = $this->find((int)$billId); if (null !== $searchResult) { Log::debug(sprintf('Found bill based on #%d, will return it.', $billId)); @@ -100,7 +100,7 @@ class BillRepository implements BillRepositoryInterface } } if (null !== $billName) { - $searchResult = $this->findByName((string) $billName); + $searchResult = $this->findByName((string)$billName); if (null !== $searchResult) { Log::debug(sprintf('Found bill based on "%s", will return it.', $billName)); @@ -193,25 +193,26 @@ class BillRepository implements BillRepositoryInterface $fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.amount_min', 'bills.amount_max', 'bills.date', 'bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active',]; $ids = $accounts->pluck('id')->toArray(); + return $this->user->bills() - ->leftJoin( - 'transaction_journals', - static function (JoinClause $join) { - $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at'); - } - ) - ->leftJoin( - 'transactions', - static function (JoinClause $join) { - $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0); - } - ) - ->whereIn('transactions.account_id', $ids) - ->whereNull('transaction_journals.deleted_at') - ->orderBy('bills.active', 'DESC') - ->orderBy('bills.name', 'ASC') - ->groupBy($fields) - ->get($fields); + ->leftJoin( + 'transaction_journals', + static function (JoinClause $join) { + $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at'); + } + ) + ->leftJoin( + 'transactions', + static function (JoinClause $join) { + $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0); + } + ) + ->whereIn('transactions.account_id', $ids) + ->whereNull('transaction_journals.deleted_at') + ->orderBy('bills.active', 'DESC') + ->orderBy('bills.name', 'ASC') + ->groupBy($fields) + ->get($fields); } /** @@ -234,7 +235,7 @@ class BillRepository implements BillRepositoryInterface $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']); if ($set->count() > 0) { $journalIds = $set->pluck('id')->toArray(); - $amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); + $amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); $sum = bcadd($sum, $amount); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum)); } @@ -260,10 +261,10 @@ class BillRepository implements BillRepositoryInterface foreach ($bills as $bill) { /** @var Collection $set */ $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']); - $currencyId = (int) $bill->transaction_currency_id; + $currencyId = (int)$bill->transaction_currency_id; if ($set->count() > 0) { $journalIds = $set->pluck('id')->toArray(); - $amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); + $amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); $return[$currencyId] = $return[$currencyId] ?? '0'; $return[$currencyId] = bcadd($amount, $return[$currencyId]); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId)); @@ -296,7 +297,7 @@ class BillRepository implements BillRepositoryInterface if ($total > 0) { $average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2'); - $multi = bcmul($average, (string) $total); + $multi = bcmul($average, (string)$total); $sum = bcadd($sum, $multi); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum)); } @@ -323,13 +324,13 @@ class BillRepository implements BillRepositoryInterface $dates = $this->getPayDatesInRange($bill, $start, $end); $count = $bill->transactionJournals()->after($start)->before($end)->count(); $total = $dates->count() - $count; - $currencyId = (int) $bill->transaction_currency_id; + $currencyId = (int)$bill->transaction_currency_id; Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total)); if ($total > 0) { $average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2'); - $multi = bcmul($average, (string) $total); + $multi = bcmul($average, (string)$total); $return[$currencyId] = $return[$currencyId] ?? '0'; $return[$currencyId] = bcadd($return[$currencyId], $multi); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (for currency %d)', $multi, $return[$currencyId], $currencyId)); @@ -363,7 +364,7 @@ class BillRepository implements BillRepositoryInterface /** @var Note $note */ $note = $bill->notes()->first(); if (null !== $note) { - return (string) $note->text; + return (string)$note->text; } return ''; @@ -381,14 +382,14 @@ class BillRepository implements BillRepositoryInterface $repos->setUser($this->user); // get and sort on currency - $result = []; + $result = []; $journals = $bill->transactionJournals()->get(); /** @var TransactionJournal $journal */ foreach ($journals as $journal) { /** @var Transaction $transaction */ $transaction = $journal->transactions()->where('amount', '<', 0)->first(); - $currencyId = (int) $journal->transaction_currency_id; + $currencyId = (int)$journal->transaction_currency_id; $currency = $journal->transactionCurrency; $result[$currencyId] = $result[$currencyId] ?? [ 'sum' => '0', @@ -409,8 +410,9 @@ class BillRepository implements BillRepositoryInterface * @var array $arr */ foreach ($result as $currencyId => $arr) { - $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']); + $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']); } + return $result; } @@ -438,6 +440,7 @@ class BillRepository implements BillRepositoryInterface public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection { Log::debug('Now in getPaidDatesInRange()'); + return $bill->transactionJournals() ->before($end)->after($start)->get( [ @@ -559,8 +562,11 @@ class BillRepository implements BillRepositoryInterface /** @var TransactionJournal $journal */ foreach ($journals as $journal) { /** @var Transaction $transaction */ - $transaction = $journal->transactions()->where('amount', '<', 0)->first(); - $currencyId = (int) $journal->transaction_currency_id; + $transaction = $journal->transactions()->where('amount', '<', 0)->first(); + if (null === $transaction) { + continue; + } + $currencyId = (int)$journal->transaction_currency_id; $currency = $journal->transactionCurrency; $result[$currencyId] = $result[$currencyId] ?? [ 'sum' => '0', @@ -581,8 +587,9 @@ class BillRepository implements BillRepositoryInterface * @var array $arr */ foreach ($result as $currencyId => $arr) { - $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']); + $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']); } + return $result; } @@ -596,7 +603,7 @@ class BillRepository implements BillRepositoryInterface { /** @var Transaction $transaction */ foreach ($transactions as $transaction) { - $journal = $bill->user->transactionJournals()->find((int) $transaction['transaction_journal_id']); + $journal = $bill->user->transactionJournals()->find((int)$transaction['transaction_journal_id']); $journal->bill_id = $bill->id; $journal->save(); Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id)); @@ -750,7 +757,7 @@ class BillRepository implements BillRepositoryInterface $set = $this->user->bills()->orderBy('order', 'ASC')->get(); $current = 1; foreach ($set as $bill) { - if ((int) $bill->order !== $current) { + if ((int)$bill->order !== $current) { $bill->order = $current; $bill->save(); }