From 3888b8cceb2c315b0a4261d964ce2e32974b33f1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 2 Jan 2016 16:57:31 +0100 Subject: [PATCH] Code cleanup according to PHPStorm. --- .../Chart/Bill/ChartJsBillChartGenerator.php | 11 +- .../Budget/ChartJsBudgetChartGenerator.php | 20 +- app/Http/Controllers/AccountController.php | 37 ++-- .../Controllers/Chart/ReportController.php | 180 ++++++++++++------ .../Account/AccountRepositoryInterface.php | 2 +- app/Support/Steam.php | 2 +- app/Validation/FireflyValidator.php | 4 +- 7 files changed, 157 insertions(+), 99 deletions(-) diff --git a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php index 52f3b540e7..879a1f8d84 100644 --- a/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php +++ b/app/Generator/Chart/Bill/ChartJsBillChartGenerator.php @@ -49,19 +49,12 @@ class ChartJsBillChartGenerator implements BillChartGenerator */ public function single(Bill $bill, Collection $entries) { - // language: - $format = trans('config.month'); - - $data = [ + $format = trans('config.month'); + $data = [ 'count' => 3, 'labels' => [], 'datasets' => [], ]; - - // dataset: max amount - // dataset: min amount - // dataset: actual amount - $minAmount = []; $maxAmount = []; $actualAmount = []; diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php index 6af66916ff..77678b93e9 100644 --- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php +++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php @@ -68,24 +68,24 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator */ public function frontpage(Collection $entries) { - $data = [ + $data = [ 'count' => 0, 'labels' => [], 'datasets' => [], ]; - // dataset: left - // dataset: spent - // dataset: overspent $left = []; $spent = []; $overspent = []; - foreach ($entries as $entry) { - if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) { - $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 + $filtered = $entries->filter( + function ($entry) { + return ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0); } + ); + 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 } $data['datasets'][] = [ diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 44f7e7e396..e018002bfc 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -154,29 +154,20 @@ class AccountController extends Controller $subTitleIcon = Config::get('firefly.subIconsByIdentifier.' . $what); $types = Config::get('firefly.accountTypesByIdentifier.' . $what); $accounts = $repository->getAccounts($types); - // last activity: - /** - * HERE WE ARE - */ - $start = clone Session::get('start', Carbon::now()->startOfMonth()); - $end = clone Session::get('end', Carbon::now()->endOfMonth()); + $start = clone Session::get('start', Carbon::now()->startOfMonth()); + $end = clone Session::get('end', Carbon::now()->endOfMonth()); $start->subDay(); - // start balances: - $ids = []; - foreach ($accounts as $account) { - $ids[] = $account->id; - } - + $ids = $accounts->pluck('id')->toArray(); $startBalances = Steam::balancesById($ids, $start); $endBalances = Steam::balancesById($ids, $end); $activities = Steam::getLastActivities($ids); $accounts->each( function (Account $account) use ($activities, $startBalances, $endBalances) { - $account->lastActivityDate = isset($activities[$account->id]) ? $activities[$account->id] : null; - $account->startBalance = isset($startBalances[$account->id]) ? $startBalances[$account->id] : null; - $account->endBalance = isset($endBalances[$account->id]) ? $endBalances[$account->id] : null; + $account->lastActivityDate = $this->isInArray($activities, $account->id); + $account->startBalance = $this->isInArray($startBalances, $account->id); + $account->endBalance = $this->isInArray($endBalances, $account->id); } ); @@ -283,4 +274,20 @@ class AccountController extends Controller } + + /** + * @param array $array + * @param $entryId + * + * @return null|mixed + */ + protected function isInArray(array $array, $entryId) + { + if (isset($array[$entryId])) { + return $array[$entryId]; + } + + return null; + } + } diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 1c2a68651a..545fe46d7a 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -61,38 +61,16 @@ class ReportController extends Controller $spentArray = $query->spentPerMonth($accounts, $start, $end); $earnedArray = $query->earnedPerMonth($accounts, $start, $end); - // per year? put all months together. if ($start->diffInMonths($end) > 12) { - $entries = new Collection; - while ($start < $end) { - - $incomeSum = $this->pluckFromArray($start->year, $earnedArray); - $expenseSum = $this->pluckFromArray($start->year, $spentArray) * -1; - - $entries->push([clone $start, $incomeSum, $expenseSum]); - $start->addYear(); - } - - $data = $this->generator->multiYearInOut($entries); - $cache->store($data); + // data = method X + $data = $this->multiYearInOut($earnedArray, $spentArray, $start, $end); } else { - // per month? simply use each month. - - $entries = new Collection; - while ($start < $end) { - // total income and total expenses: - $date = $start->format('Y-m'); - $incomeSum = isset($earnedArray[$date]) ? $earnedArray[$date] : 0; - $expenseSum = isset($spentArray[$date]) ? ($spentArray[$date] * -1) : 0; - - $entries->push([clone $start, $incomeSum, $expenseSum]); - $start->addMonth(); - } - - $data = $this->generator->yearInOut($entries); - $cache->store($data); + // data = method Y + $data = $this->singleYearInOut($earnedArray, $spentArray, $start, $end); } + $cache->store($data); + return Response::json($data); } @@ -125,47 +103,127 @@ class ReportController extends Controller // grouped by month $spentArray = $query->spentPerMonth($accounts, $start, $end); $earnedArray = $query->earnedPerMonth($accounts, $start, $end); - $income = '0'; - $expense = '0'; - $count = 0; - - bcscale(2); - if ($start->diffInMonths($end) > 12) { // per year - while ($start < $end) { - - $currentIncome = $this->pluckFromArray($start->year, $earnedArray); - $currentExpense = $this->pluckFromArray($start->year, $spentArray) * -1; - $income = bcadd($income, $currentIncome); - $expense = bcadd($expense, $currentExpense); - - $count++; - $start->addYear(); - } - - $data = $this->generator->multiYearInOutSummarized($income, $expense, $count); - $cache->store($data); + $data = $this->multiYearInOutSummarized($earnedArray, $spentArray, $start, $end); } else { // per month! - while ($start < $end) { - $date = $start->format('Y-m'); - $currentIncome = isset($earnedArray[$date]) ? $earnedArray[$date] : 0; - $currentExpense = isset($spentArray[$date]) ? ($spentArray[$date] * -1) : 0; - $income = bcadd($income, $currentIncome); - $expense = bcadd($expense, $currentExpense); - - $count++; - $start->addMonth(); - } - - $data = $this->generator->yearInOutSummarized($income, $expense, $count); - $cache->store($data); + $data = $this->singleYearInOutSummarized($earnedArray, $spentArray, $start, $end); } - + $cache->store($data); return Response::json($data); + } + /** + * @param array $earned + * @param array $spent + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + protected function singleYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end) + { + $income = '0'; + $expense = '0'; + $count = 0; + while ($start < $end) { + $date = $start->format('Y-m'); + $currentIncome = isset($earned[$date]) ? $earned[$date] : 0; + $currentExpense = isset($spent[$date]) ? ($spent[$date] * -1) : 0; + $income = bcadd($income, $currentIncome); + $expense = bcadd($expense, $currentExpense); + + $count++; + $start->addMonth(); + } + + $data = $this->generator->yearInOutSummarized($income, $expense, $count); + + return $data; + } + + /** + * @param array $earned + * @param array $spent + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + protected function multiYearInOutSummarized(array $earned, array $spent, Carbon $start, Carbon $end) + { + $income = '0'; + $expense = '0'; + $count = 0; + while ($start < $end) { + + $currentIncome = $this->pluckFromArray($start->year, $earned); + $currentExpense = $this->pluckFromArray($start->year, $spent) * -1; + $income = bcadd($income, $currentIncome); + $expense = bcadd($expense, $currentExpense); + + $count++; + $start->addYear(); + } + + $data = $this->generator->multiYearInOutSummarized($income, $expense, $count); + + return $data; + } + + /** + * @param array $earned + * @param array $spent + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + protected function multiYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) + { + $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(); + } + + $data = $this->generator->multiYearInOut($entries); + + return $data; + } + + /** + * @param array $earned + * @param array $spent + * @param Carbon $start + * @param Carbon $end + * + * @return array + */ + protected function singleYearInOut(array $earned, array $spent, Carbon $start, Carbon $end) + { + // per month? simply use each month. + + $entries = new Collection; + while ($start < $end) { + // total income and total expenses: + $date = $start->format('Y-m'); + $incomeSum = isset($earned[$date]) ? $earned[$date] : 0; + $expenseSum = isset($spent[$date]) ? ($spent[$date] * -1) : 0; + + $entries->push([clone $start, $incomeSum, $expenseSum]); + $start->addMonth(); + } + + $data = $this->generator->yearInOut($entries); + + return $data; } /** diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index d03cf070e6..b18ffb2807 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -44,7 +44,7 @@ interface AccountRepositoryInterface /** * @param array $types * - * @return mixed + * @return Collection */ public function getAccounts(array $types); diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 4a2a733355..216d5528e4 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -127,7 +127,7 @@ class Steam * @param array $ids * @param \Carbon\Carbon $date * - * @return float + * @return array */ public function balancesById(array $ids, Carbon $date) { diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 6d0d056296..1503518a1d 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -43,8 +43,8 @@ class FireflyValidator extends Validator * * @return bool */ - public function validateBelongsToUser($attribute, $value, $parameters - ) { + public function validateBelongsToUser($attribute, $value, $parameters) + { $count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count(); if ($count == 1) {