Conditions | 5 |
Paths | 12 |
Total Lines | 67 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | public function generate(Category $category, Carbon $start, Carbon $end): array |
||
43 | { |
||
44 | /** @var CategoryRepositoryInterface $repository */ |
||
45 | $repository = app(CategoryRepositoryInterface::class); |
||
46 | |||
47 | /** @var AccountRepositoryInterface $accountRepository */ |
||
48 | $accountRepository = app(AccountRepositoryInterface::class); |
||
49 | |||
50 | $types = [AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; |
||
51 | $accounts = $accountRepository->getAccountsByType($types); |
||
52 | $step = $this->calculateStep($start, $end); |
||
53 | $chartData = []; |
||
54 | $spent = []; |
||
55 | $earned = []; |
||
56 | |||
57 | /** @var Carbon $current */ |
||
58 | $current = clone $start; |
||
59 | |||
60 | while ($current <= $end) { |
||
61 | $key = $current->format('Y-m-d'); |
||
62 | $currentEnd = app('navigation')->endOfPeriod($current, $step); |
||
63 | $spent[$key] = $repository->spentInPeriod($category, $accounts, $current, $currentEnd); |
||
|
|||
64 | $earned[$key] = $repository->earnedInPeriod($category, $accounts, $current, $currentEnd); |
||
65 | $current = app('navigation')->addPeriod($current, $step, 0); |
||
66 | } |
||
67 | $currencies = $this->extractCurrencies($spent) + $this->extractCurrencies($earned); |
||
68 | |||
69 | // generate chart data (for each currency) |
||
70 | /** @var array $currency */ |
||
71 | foreach ($currencies as $currency) { |
||
72 | $code = $currency['currency_code']; |
||
73 | $name = $currency['currency_name']; |
||
74 | $chartData[sprintf('spent-in-%s', $code)] = [ |
||
75 | 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $name]), |
||
76 | 'entries' => [], |
||
77 | 'type' => 'bar', |
||
78 | 'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red |
||
79 | ]; |
||
80 | |||
81 | $chartData[sprintf('earned-in-%s', $code)] = [ |
||
82 | 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $name]), |
||
83 | 'entries' => [], |
||
84 | 'type' => 'bar', |
||
85 | 'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | /** @var Carbon $current */ |
||
90 | $current = clone $start; |
||
91 | |||
92 | while ($current <= $end) { |
||
93 | $key = $current->format('Y-m-d'); |
||
94 | $label = app('navigation')->periodShow($current, $step); |
||
95 | |||
96 | /** @var array $currency */ |
||
97 | foreach ($currencies as $currency) { |
||
98 | $code = $currency['currency_code']; |
||
99 | $spentInfoKey = sprintf('spent-in-%s', $code); |
||
100 | $earnedInfoKey = sprintf('earned-in-%s', $code); |
||
101 | $spentAmount = $spent[$key][$code]['spent'] ?? '0'; |
||
102 | $earnedAmount = $earned[$key][$code]['earned'] ?? '0'; |
||
103 | $chartData[$spentInfoKey]['entries'][$label] = round($spentAmount, $currency['currency_decimal_places']); |
||
104 | $chartData[$earnedInfoKey]['entries'][$label] = round($earnedAmount, $currency['currency_decimal_places']); |
||
105 | } |
||
106 | $current = app('navigation')->addPeriod($current, $step, 0); |
||
107 | } |
||
108 | return $chartData; |
||
109 | } |
||
166 | } |