From df918e852941d67ab5aae6f9e1389760900a00cf Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 5 Feb 2016 13:09:18 +0100 Subject: [PATCH] Fixed all problems related to strict types. --- .../Chart/Bill/ChartJsBillChartGenerator.php | 5 ++-- .../Budget/ChartJsBudgetChartGenerator.php | 5 ++-- .../ChartJsCategoryChartGenerator.php | 11 +++++---- .../Events/ConnectJournalToPiggyBank.php | 3 ++- app/Helpers/Report/BalanceReportHelper.php | 8 +++---- app/Http/Controllers/AccountController.php | 2 +- .../Controllers/Chart/BudgetController.php | 14 +++++------ .../Controllers/Chart/CategoryController.php | 4 ++-- app/Http/Controllers/JsonController.php | 2 +- app/Http/Controllers/PiggyBankController.php | 1 + app/Http/Controllers/TagController.php | 5 ++-- app/Http/Requests/JournalFormRequest.php | 6 ++--- .../Account/AccountRepository.php | 2 +- .../Journal/JournalRepository.php | 5 ++-- app/Support/Migration/TestData.php | 6 ++--- app/Support/Twig/General.php | 24 +++++++++---------- resources/views/piggy-banks/show.twig | 2 +- resources/views/reports/partials/balance.twig | 5 +++- 18 files changed, 58 insertions(+), 52 deletions(-) diff --git a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php index fb9ef0beea..3dbea5f630 100644 --- a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php +++ b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php @@ -39,7 +39,7 @@ class ChartJsBillChartGenerator implements BillChartGeneratorInterface 'label' => trans('firefly.unpaid'), ], [ - 'value' => round($paid * -1, 2), // paid is negative, must be positive. + 'value' => round(bcmul($paid, '-1'), 2), // paid is negative, must be positive. 'color' => 'rgba(0, 141, 76, 0.7)', 'highlight' => 'rgba(0, 141, 76, 0.9)', 'label' => trans('firefly.paid'), @@ -57,6 +57,7 @@ class ChartJsBillChartGenerator implements BillChartGeneratorInterface */ public function single(Bill $bill, Collection $entries) { + bcscale(2); $format = (string)trans('config.month'); $data = [ 'count' => 3, @@ -74,7 +75,7 @@ class ChartJsBillChartGenerator implements BillChartGeneratorInterface /* * journalAmount has been collected in BillRepository::getJournals */ - $actualAmount[] = round(($entry->journalAmount * -1), 2); + $actualAmount[] = round(bcmul($entry->journalAmount, '-1'), 2); } $data['datasets'][] = [ diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php index a04697511d..568d0c54f7 100644 --- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -68,6 +68,7 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface */ public function frontpage(Collection $entries) { + bcscale(2); $data = [ 'count' => 0, 'labels' => [], @@ -84,8 +85,8 @@ class ChartJsBudgetChartGenerator implements BudgetChartGeneratorInterface foreach ($filtered as $entry) { $data['labels'][] = $entry[0]; $left[] = round($entry[1], 2); - $spent[] = round($entry[2] * -1, 2); // spent is coming in negative, must be positive - $overspent[] = round($entry[3] * -1, 2); // same + $spent[] = round(bcmul($entry[2],'-1'), 2); // spent is coming in negative, must be positive + $overspent[] = round(bcmul($entry[3],'-1'), 2); // same } $data['datasets'][] = [ diff --git a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php index f8b8ec3231..b972718905 100644 --- a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php +++ b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php @@ -39,11 +39,11 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface foreach ($entries as $entry) { $data['labels'][] = $entry[1]; - $spent = round($entry[2], 2); - $earned = round($entry[3], 2); + $spent = $entry[2]; + $earned = $entry[3]; - $data['datasets'][0]['data'][] = $spent == 0 ? null : $spent * -1; - $data['datasets'][1]['data'][] = $earned == 0 ? null : $earned; + $data['datasets'][0]['data'][] = bccomp($spent, '0') === 0 ? null : bcmul($spent, '-1'); + $data['datasets'][1]['data'][] = bccomp($earned, '0') === 0 ? null : $earned; } return $data; @@ -89,6 +89,7 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface */ public function frontpage(Collection $entries) { + bcscale(2); $data = [ 'count' => 1, 'labels' => [], @@ -102,7 +103,7 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface foreach ($entries as $entry) { if ($entry->spent != 0) { $data['labels'][] = $entry->name; - $data['datasets'][0]['data'][] = round(($entry->spent * -1), 2); + $data['datasets'][0]['data'][] = round(bcmul($entry->spent, '-1'), 2); } } diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.php index 59429bbbec..b62a9c2b10 100644 --- a/app/Handlers/Events/ConnectJournalToPiggyBank.php +++ b/app/Handlers/Events/ConnectJournalToPiggyBank.php @@ -40,6 +40,7 @@ class ConnectJournalToPiggyBank /** @var TransactionJournal $journal */ $journal = $event->journal; $piggyBankId = $event->piggyBankId; + bcscale(2); /** @var PiggyBank $piggyBank */ $piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); @@ -57,7 +58,7 @@ class ConnectJournalToPiggyBank $amount = $journal->amount_positive; // if piggy account matches source account, the amount is positive if ($piggyBank->account_id == $journal->source_account->id) { - $amount = $amount * -1; + $amount = bcmul($amount,'-1'); } diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 6c317bbfdb..bf3f961df6 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -142,7 +142,7 @@ class BalanceReportHelper implements BalanceReportHelperInterface return $model->account_id == $account->id && is_null($model->budget_id); } ); - $spent = 0; + $spent = '0'; if (!is_null($entry->first())) { $spent = $entry->first()->spent; } @@ -151,7 +151,7 @@ class BalanceReportHelper implements BalanceReportHelperInterface return $tag->account_id == $account->id; } ); - $left = 0; + $left = '0'; if (!is_null($leftEntry->first())) { $left = $leftEntry->first()->sum; } @@ -185,7 +185,7 @@ class BalanceReportHelper implements BalanceReportHelperInterface return $model->account_id == $account->id && is_null($model->budget_id); } ); - $spent = 0; + $spent = '0'; if (!is_null($entry->first())) { $spent = $entry->first()->spent; } @@ -221,7 +221,7 @@ class BalanceReportHelper implements BalanceReportHelperInterface return $tag->account_id == $account->id; } ); - $left = 0; + $left = '0'; if (!is_null($leftEntry->first())) { $left = $leftEntry->first()->sum; } diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 7bda3cb6c3..6dfe30c0d8 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -285,7 +285,7 @@ class AccountController extends Controller return $array[$entryId]; } - return null; + return ''; } } diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index a35e173a89..84c2095cb4 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -185,26 +185,26 @@ class BudgetController extends Controller $currentStart = clone $start; $currentEnd = clone $end; $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); - $amount = 0; - $left = 0; + $amount = '0'; + $left = '0'; $spent = $expenses; - $overspent = 0; + $overspent = '0'; } else { $currentStart = clone $budget->startdate; $currentEnd = clone $budget->enddate; $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); $amount = $budget->amount; // smaller than 1 means spent MORE than budget allows. - $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? 0 : bcadd($budget->amount, $expenses); - $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? ($amount * -1) : $expenses; - $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : 0; + $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? '0' : bcadd($budget->amount, $expenses); + $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses; + $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : '0'; } $allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]); } $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end); - $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses, 0, 0]); + $allEntries->push([trans('firefly.noBudget'), '0', '0', $noBudgetExpenses, '0', '0']); $data = $this->generator->frontpage($allEntries); $cache->store($data); diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 2a503e1a50..c7e4b02178 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -406,8 +406,8 @@ class CategoryController extends Controller while ($start <= $end) { $str = $start->format('Y-m-d'); - $spent = $spentArray[$str] ?? 0; - $earned = $earnedArray[$str] ?? 0; + $spent = $spentArray[$str] ?? '0'; + $earned = $earnedArray[$str] ?? '0'; $date = Navigation::periodShow($start, '1D'); $entries->push([clone $start, $date, $spent, $earned]); $start->addDay(); diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index fab1453a10..85b6948c8e 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -66,7 +66,7 @@ class JsonController extends Controller if ($creditCardDue >= 0) { $amount = bcadd($amount, $creditCardDue); } - $amount = $amount * -1; + $amount = bcmul($amount,'-1'); $data = ['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php index 56ec30ceac..ca03321887 100644 --- a/app/Http/Controllers/PiggyBankController.php +++ b/app/Http/Controllers/PiggyBankController.php @@ -166,6 +166,7 @@ class PiggyBankController extends Controller { /** @var Collection $piggyBanks */ $piggyBanks = $piggyRepository->getPiggyBanks(); + /** @var Carbon $end */ $end = session('end', Carbon::now()->endOfMonth()); bcscale(2); diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index a74877aa3a..67869985d0 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -247,7 +247,7 @@ class TagController extends Controller $data = [ 'tag' => $request->get('tag'), 'date' => strlen($date) > 0 ? new Carbon($date) : null, - 'description' => strlen($request->get('description')) > 0 ? $request->get('description') : '', + 'description' => $request->get('description') ?? '', 'latitude' => $latitude, 'longitude' => $longitude, 'zoomLevel' => $zoomLevel, @@ -290,11 +290,10 @@ class TagController extends Controller } $date = $request->get('date') ?? ''; - $data = [ 'tag' => $request->get('tag'), 'date' => strlen($date) > 0 ? new Carbon($date) : null, - 'description' => strlen($request->get('description')) > 0 ? $request->get('description') : '', + 'description' => $request->get('description') ?? '', 'latitude' => $latitude, 'longitude' => $longitude, 'zoomLevel' => $zoomLevel, diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 2972f1572a..ee9fdf0788 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -38,14 +38,14 @@ class JournalFormRequest extends Request 'account_id' => intval($this->get('account_id')), 'account_from_id' => intval($this->get('account_from_id')), 'account_to_id' => intval($this->get('account_to_id')), - 'expense_account' => $this->get('expense_account'), - 'revenue_account' => $this->get('revenue_account'), + 'expense_account' => $this->get('expense_account') ?? '', + 'revenue_account' => $this->get('revenue_account') ?? '', 'amount' => round($this->get('amount'), 2), 'user' => Auth::user()->id, 'amount_currency_id_amount' => intval($this->get('amount_currency_id_amount')), 'date' => new Carbon($this->get('date')), 'budget_id' => intval($this->get('budget_id')), - 'category' => $this->get('category'), + 'category' => $this->get('category') ?? '', 'tags' => explode(',', $tags), ]; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index f44b6ed7f6..f3486d1a47 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -330,7 +330,7 @@ class AccountRepository implements AccountRepositoryInterface $balance = Steam::balance($account, $date, true); /** @var PiggyBank $p */ foreach ($account->piggybanks()->get() as $p) { - $balance -= $p->currentRelevantRep()->currentamount; + $balance = bcsub($p->currentRelevantRep()->currentamount, $balance); } return $balance; diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 8e94cd7d79..6184ff9d47 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -65,9 +65,10 @@ class JournalRepository implements JournalRepositoryInterface ->where('transaction_journals.order', '>=', $journal->order) ->where('transaction_journals.id', '!=', $journal->id) ->get(['transactions.*']); - $sum = 0; + bcscale(2); + $sum = '0'; foreach ($set as $entry) { - $sum += $entry->amount; + $sum = bcadd($entry->amount, $sum); } return $sum; diff --git a/app/Support/Migration/TestData.php b/app/Support/Migration/TestData.php index 341c192c89..69b8d12f1b 100644 --- a/app/Support/Migration/TestData.php +++ b/app/Support/Migration/TestData.php @@ -194,7 +194,7 @@ class TestData * @param $name * @param $amount */ - public static function createBudgetLimit(User $user, Carbon $current, $name, $amount) + public static function createBudgetLimit(User $user, Carbon $current, string $name, string $amount) { $start = clone $current; $end = clone $current; @@ -474,7 +474,7 @@ class TestData * * @return Account|null */ - public static function findAccount(User $user, $name) + public static function findAccount(User $user, string $name) { /** @var Account $account */ foreach ($user->accounts()->get() as $account) { @@ -495,7 +495,7 @@ class TestData * * @return Budget|null */ - public static function findBudget(User $user, $name) + public static function findBudget(User $user, string $name) { /** @var Budget $budget */ foreach (Budget::get() as $budget) { diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php index f04c087281..55a5dd1093 100644 --- a/app/Support/Twig/General.php +++ b/app/Support/Twig/General.php @@ -7,6 +7,7 @@ use Carbon\Carbon; use Config; use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionJournal; use Route; use Twig_Extension; use Twig_SimpleFilter; @@ -158,7 +159,7 @@ class General extends Twig_Extension protected function env() { return new Twig_SimpleFunction( - 'env', function ($name, $default) { + 'env', function (string $name, string $default) { return env($name, $default); } ); @@ -171,10 +172,9 @@ class General extends Twig_Extension protected function formatAmount() { return new Twig_SimpleFilter( - 'formatAmount', function ($string) { - $value = is_null($string) ? '0' : strval($string); + 'formatAmount', function (string $string) { - return app('amount')->format($value); + return app('amount')->format($string); }, ['is_safe' => ['html']] ); } @@ -185,10 +185,9 @@ class General extends Twig_Extension protected function formatAmountPlain() { return new Twig_SimpleFilter( - 'formatAmountPlain', function ($string) { - $value = is_null($string) ? '0' : strval($string); + 'formatAmountPlain', function (string $string) { - return app('amount')->format($value, false); + return app('amount')->format($string, false); }, ['is_safe' => ['html']] ); } @@ -199,8 +198,7 @@ class General extends Twig_Extension protected function formatFilesize() { return new Twig_SimpleFilter( - 'filesize', function ($size) { - $size = intval($size); + 'filesize', function (int $size) { // less than one GB, more than one MB if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) { @@ -223,7 +221,7 @@ class General extends Twig_Extension protected function formatJournal() { return new Twig_SimpleFilter( - 'formatJournal', function ($journal) { + 'formatJournal', function (TransactionJournal $journal) { return app('amount')->formatJournal($journal); }, ['is_safe' => ['html']] ); @@ -247,7 +245,7 @@ class General extends Twig_Extension protected function getAccountRole() { return new Twig_SimpleFilter( - 'getAccountRole', function ($name) { + 'getAccountRole', function (string $name) { return Config::get('firefly.accountRoles.' . $name); } ); @@ -283,7 +281,7 @@ class General extends Twig_Extension protected function mimeIcon() { return new Twig_SimpleFilter( - 'mimeIcon', function ($string) { + 'mimeIcon', function (string $string) { switch ($string) { default: return 'fa-file-o'; @@ -303,7 +301,7 @@ class General extends Twig_Extension protected function phpdate() { return new Twig_SimpleFunction( - 'phpdate', function ($str) { + 'phpdate', function (string $str) { return date($str); } ); diff --git a/resources/views/piggy-banks/show.twig b/resources/views/piggy-banks/show.twig index 2d7158f3eb..164d80241f 100644 --- a/resources/views/piggy-banks/show.twig +++ b/resources/views/piggy-banks/show.twig @@ -41,7 +41,7 @@ {{ 'target_amount'|_ }} - {{ piggyBank.targetAmount|formatAmount }} + {{ piggyBank.targetamount|formatAmount }} {{ 'saved_so_far'|_ }} diff --git a/resources/views/reports/partials/balance.twig b/resources/views/reports/partials/balance.twig index 74cf4804a9..b30e34ec0c 100644 --- a/resources/views/reports/partials/balance.twig +++ b/resources/views/reports/partials/balance.twig @@ -26,7 +26,10 @@ {{ balanceLine.getTitle }} - {{ balanceLine.getRepetition.amount|formatAmount }} + + [source] + + {% else %} {{ balanceLine.getTitle }} {% endif %}