From aa1193a9eb67ccb99607cbfaebe2e7ecabb0fdea Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 5 Feb 2016 12:28:05 +0100 Subject: [PATCH] Fixed all problems related to strict types. --- app/Helpers/Csv/Data.php | 4 +- app/Helpers/Report/BalanceReportHelper.php | 2 +- .../Controllers/Chart/BudgetController.php | 2 +- .../Controllers/Chart/ReportController.php | 78 +++++++++---------- app/Http/Controllers/TagController.php | 7 +- app/Http/Requests/JournalFormRequest.php | 3 +- .../Account/AccountRepository.php | 4 +- app/Repositories/Bill/BillRepository.php | 42 +++++----- .../Shared/ComponentRepository.php | 2 +- app/Support/Steam.php | 12 +-- app/Support/Twig/General.php | 8 +- 11 files changed, 87 insertions(+), 77 deletions(-) diff --git a/app/Helpers/Csv/Data.php b/app/Helpers/Csv/Data.php index 92d4089db1..19a5e0fbb9 100644 --- a/app/Helpers/Csv/Data.php +++ b/app/Helpers/Csv/Data.php @@ -58,7 +58,7 @@ class Data */ public function getCsvFileContent() { - return $this->csvFileContent; + return $this->csvFileContent ?? ''; } /** @@ -171,7 +171,7 @@ class Data */ public function getReader() { - if (strlen($this->csvFileContent) === 0) { + if (!is_null($this->csvFileContent) && strlen($this->csvFileContent) === 0) { $this->loadCsvFile(); } diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 67654d89fd..6c317bbfdb 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -108,7 +108,7 @@ class BalanceReportHelper implements BalanceReportHelperInterface return $model->account_id == $account->id && $model->budget_id == $budget->id; } ); - $spent = 0; + $spent = '0'; if (!is_null($entry->first())) { $spent = $entry->first()->spent; } diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 41fe3213bd..a35e173a89 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -132,7 +132,7 @@ class BudgetController extends Controller /* * Sum of expenses on this day: */ - $amount = round(bcadd($amount, $sum), 2); + $amount = round(bcadd(strval($amount), $sum), 2); $entries->push([clone $start, $amount]); $start->addDay(); } diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 4a2f44868e..a2951cdf5a 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -126,24 +126,19 @@ class ReportController extends Controller * * @return array */ - protected function singleYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end) + protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) { - bcscale(2); - $income = '0'; - $expense = '0'; - $count = 0; + $entries = new Collection; while ($start < $end) { - $date = $start->format('Y-m'); - $currentIncome = $earned[$date] ?? 0; - $currentExpense = isset($spent[$date]) ? ($spent[$date] * -1) : 0; - $income = bcadd($income, $currentIncome); - $expense = bcadd($expense, $currentExpense); - $count++; - $start->addMonth(); + $incomeSum = $this->pluckFromArray($start->year, $earned); + $expenseSum = $this->pluckFromArray($start->year, $spent) * -1; + + $entries->push([clone $start, $incomeSum, $expenseSum]); + $start->addYear(); } - $data = $this->generator->yearInOutSummarized($income, $expense, $count); + $data = $this->generator->multiYearInOut($entries); return $data; } @@ -179,28 +174,23 @@ class ReportController extends Controller } /** - * @param array $earned - * @param array $spent - * @param Carbon $start - * @param Carbon $end + * @param int $year + * @param array $set * - * @return array + * @return string */ - protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) + protected function pluckFromArray($year, array $set) { - $entries = new Collection; - while ($start < $end) { - - $incomeSum = $this->pluckFromArray($start->year, $earned); - $expenseSum = $this->pluckFromArray($start->year, $spent) * -1; - - $entries->push([clone $start, $incomeSum, $expenseSum]); - $start->addYear(); + bcscale(2); + $sum = '0'; + foreach ($set as $date => $amount) { + if (substr($date, 0, 4) == $year) { + $sum = bcadd($sum, $amount); + } } - $data = $this->generator->multiYearInOut($entries); + return $sum; - return $data; } /** @@ -232,22 +222,32 @@ class ReportController extends Controller } /** - * @param int $year - * @param array $set + * @param array $earned + * @param array $spent + * @param Carbon $start + * @param Carbon $end * - * @return string + * @return array */ - protected function pluckFromArray($year, array $set) + protected function singleYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end) { bcscale(2); - $sum = '0'; - foreach ($set as $date => $amount) { - if (substr($date, 0, 4) == $year) { - $sum = bcadd($sum, $amount); - } + $income = '0'; + $expense = '0'; + $count = 0; + while ($start < $end) { + $date = $start->format('Y-m'); + $currentIncome = $earned[$date] ?? '0'; + $currentExpense = isset($spent[$date]) ? bcmul($spent[$date], '-1') : '0'; + $income = bcadd($income, $currentIncome); + $expense = bcadd($expense, $currentExpense); + + $count++; + $start->addMonth(); } - return $sum; + $data = $this->generator->yearInOutSummarized($income, $expense, $count); + return $data; } } diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index bc5b36e522..a74877aa3a 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -242,10 +242,11 @@ class TagController extends Controller $longitude = null; $zoomLevel = null; } + $date = $request->get('date') ?? ''; $data = [ 'tag' => $request->get('tag'), - 'date' => strlen($request->get('date')) > 0 ? new Carbon($request->get('date')) : null, + 'date' => strlen($date) > 0 ? new Carbon($date) : null, 'description' => strlen($request->get('description')) > 0 ? $request->get('description') : '', 'latitude' => $latitude, 'longitude' => $longitude, @@ -287,10 +288,12 @@ class TagController extends Controller $longitude = null; $zoomLevel = null; } + $date = $request->get('date') ?? ''; + $data = [ 'tag' => $request->get('tag'), - 'date' => strlen($request->get('date')) > 0 ? new Carbon($request->get('date')) : null, + 'date' => strlen($date) > 0 ? new Carbon($date) : null, 'description' => strlen($request->get('description')) > 0 ? $request->get('description') : '', 'latitude' => $latitude, 'longitude' => $longitude, diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 758dd6f60a..2972f1572a 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -31,6 +31,7 @@ class JournalFormRequest extends Request */ public function getJournalData() { + $tags = $this->get('tags') ?? ''; return [ 'what' => $this->get('what'), 'description' => $this->get('description'), @@ -45,7 +46,7 @@ class JournalFormRequest extends Request 'date' => new Carbon($this->get('date')), 'budget_id' => intval($this->get('budget_id')), 'category' => $this->get('category'), - 'tags' => explode(',', $this->get('tags')), + 'tags' => explode(',', $tags), ]; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 1affad1770..f44b6ed7f6 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -253,10 +253,10 @@ class AccountRepository implements AccountRepositoryInterface function (Account $account) use ($start, $end) { $account->startBalance = Steam::balance($account, $start, true); $account->endBalance = Steam::balance($account, $end, true); - $account->piggyBalance = 0; + $account->piggyBalance = '0'; /** @var PiggyBank $piggyBank */ foreach ($account->piggyBanks as $piggyBank) { - $account->piggyBalance += $piggyBank->currentRelevantRep()->currentamount; + $account->piggyBalance = bcadd($piggyBank->currentRelevantRep()->currentamount, $account->piggyBalance); } // sum of piggy bank amounts on this account: // diff between endBalance and piggyBalance. diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 55e210e2af..10dd09ad76 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -162,16 +162,17 @@ class BillRepository implements BillRepositoryInterface $ranges = $this->getRanges($bill, $start, $end); foreach ($ranges as $range) { - $paid = $bill->transactionjournals() - ->before($range['end']) - ->after($range['start']) - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - } - ) - ->first([DB::Raw('SUM(`transactions`.`amount`) as `sum_amount`')]); - $amount = bcadd($amount, $paid->sum_amount); + $paid = $bill->transactionjournals() + ->before($range['end']) + ->after($range['start']) + ->leftJoin( + 'transactions', function (JoinClause $join) { + $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); + } + ) + ->first([DB::Raw('SUM(`transactions`.`amount`) as `sum_amount`')]); + $sumAmount = $paid->sum_amount ?? '0'; + $amount = bcadd($amount, $sumAmount); } } @@ -196,16 +197,17 @@ class BillRepository implements BillRepositoryInterface $ranges = $this->getRanges($bill, $start, $end); $paidBill = '0'; foreach ($ranges as $range) { - $paid = $bill->transactionjournals() - ->before($range['end']) - ->after($range['start']) - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0); - } - ) - ->first([DB::Raw('SUM(`transactions`.`amount`) as `sum_amount`')]); - $paidBill = bcadd($paid->sum_amount, $paidBill); + $paid = $bill->transactionjournals() + ->before($range['end']) + ->after($range['start']) + ->leftJoin( + 'transactions', function (JoinClause $join) { + $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0); + } + ) + ->first([DB::Raw('SUM(`transactions`.`amount`) as `sum_amount`')]); + $sumAmount = $paid->sum_amount ?? '0'; + $paidBill = bcadd($sumAmount, $paidBill); } if ($paidBill == 0) { $amount = bcadd($amount, $bill->expectedAmount); diff --git a/app/Repositories/Shared/ComponentRepository.php b/app/Repositories/Shared/ComponentRepository.php index ba285cc1a8..fff108104c 100644 --- a/app/Repositories/Shared/ComponentRepository.php +++ b/app/Repositories/Shared/ComponentRepository.php @@ -37,7 +37,7 @@ class ComponentRepository ->whereIn('accounts.id', $ids) ->after($start) ->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]); - $amount = $entry->journalAmount; + $amount = $entry->journalAmount ?? '0'; return $amount; } diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 7431f715a7..e896c65c8c 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -23,7 +23,7 @@ class Steam * @param \Carbon\Carbon $date * @param bool $ignoreVirtualBalance * - * @return float + * @return string */ public function balance(Account $account, Carbon $date, $ignoreVirtualBalance = false) { @@ -45,11 +45,11 @@ class Steam )->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount'); if (!$ignoreVirtualBalance) { - $balance = bcadd($balance, $account->virtual_balance); + $balance = bcadd(strval($balance), $account->virtual_balance); } - $cache->store(round($balance, 2)); + $cache->store($balance); - return round($balance, 2); + return $balance; } /** @@ -177,14 +177,14 @@ class Steam if (!(strpos($string, 'k') === false)) { // has a K in it, remove the K and multiply by 1024. - $bytes = bcmul(rtrim($string, 'k'), 1024); + $bytes = bcmul(rtrim($string, 'k'), '1024'); return intval($bytes); } if (!(strpos($string, 'm') === false)) { // has a M in it, remove the M and multiply by 1048576. - $bytes = bcmul(rtrim($string, 'm'), 1048576); + $bytes = bcmul(rtrim($string, 'm'),'1048576'); return intval($bytes); } diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php index d9e01f9e64..f04c087281 100644 --- a/app/Support/Twig/General.php +++ b/app/Support/Twig/General.php @@ -16,6 +16,7 @@ use Twig_SimpleFunction; * @codeCoverageIgnore * * Class TwigSupport + * @todo these functions should be parameterized. * * @package FireflyIII\Support */ @@ -164,13 +165,14 @@ class General extends Twig_Extension } /** + * * @return Twig_SimpleFilter */ protected function formatAmount() { return new Twig_SimpleFilter( 'formatAmount', function ($string) { - $value = is_null($string) ? '0' : $string; + $value = is_null($string) ? '0' : strval($string); return app('amount')->format($value); }, ['is_safe' => ['html']] @@ -184,7 +186,9 @@ class General extends Twig_Extension { return new Twig_SimpleFilter( 'formatAmountPlain', function ($string) { - return app('amount')->format($string, false); + $value = is_null($string) ? '0' : strval($string); + + return app('amount')->format($value, false); }, ['is_safe' => ['html']] ); }