Conditions | 9 |
Paths | 24 |
Total Lines | 121 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
80 | public function overview(DateRequest $request): JsonResponse |
||
81 | { |
||
82 | // parameters for chart: |
||
83 | $dates = $request->getAll(); |
||
84 | /** @var Carbon $start */ |
||
85 | $start = $dates['start']; |
||
86 | /** @var Carbon $end */ |
||
87 | $end = $dates['end']; |
||
88 | |||
89 | |||
90 | $tempData = []; |
||
91 | $spentWith = $this->opsRepository->listExpenses($start, $end); |
||
92 | $earnedWith = $this->opsRepository->listIncome($start, $end); |
||
93 | $spentWithout = $this->noCatRepository->listExpenses($start, $end); |
||
94 | $earnedWithout = $this->noCatRepository->listIncome($start, $end); |
||
95 | $categories = []; |
||
96 | |||
97 | |||
98 | foreach ([$spentWith, $earnedWith, $spentWithout, $earnedWithout] as $set) { |
||
99 | foreach ($set as $currency) { |
||
100 | foreach ($currency['categories'] as $category) { |
||
101 | $categories[] = $category['name']; |
||
102 | $inKey = sprintf('%d-i', $currency['currency_id']); |
||
103 | $outKey = sprintf('%d-e', $currency['currency_id']); |
||
104 | // make data arrays if not yet present. |
||
105 | $tempData[$inKey] = $tempData[$inKey] ?? [ |
||
106 | 'currency_id' => $currency['currency_id'], |
||
107 | 'label' => (string) trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), |
||
108 | 'currency_code' => $currency['currency_code'], |
||
109 | 'currency_symbol' => $currency['currency_symbol'], |
||
110 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
111 | 'type' => 'bar', // line, area or bar |
||
112 | 'yAxisID' => 0, // 0, 1, 2 |
||
113 | 'entries' => [ |
||
114 | // per category: |
||
115 | // "category" => 5, |
||
116 | ], |
||
117 | ]; |
||
118 | $tempData[$outKey] = $tempData[$outKey] ?? [ |
||
119 | 'currency_id' => $currency['currency_id'], |
||
120 | 'label' => (string) trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]), |
||
121 | 'currency_code' => $currency['currency_code'], |
||
122 | 'currency_symbol' => $currency['currency_symbol'], |
||
123 | 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
124 | 'type' => 'bar', // line, area or bar |
||
125 | 'yAxisID' => 0, // 0, 1, 2 |
||
126 | 'entries' => [ |
||
127 | // per category: |
||
128 | // "category" => 5, |
||
129 | ], |
||
130 | ]; |
||
131 | |||
132 | foreach ($category['transaction_journals'] as $journal) { |
||
133 | // is it expense or income? |
||
134 | $letter = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i'; |
||
135 | $currentKey = sprintf('%d-%s', $currency['currency_id'], $letter); |
||
136 | $name = $category['name']; |
||
137 | $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0'; |
||
138 | $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // foreach ([] as $set) { |
||
145 | // foreach ($set as $currency) { |
||
146 | // $inKey = sprintf('%d-i', $currency['currency_id']); |
||
147 | // $outKey = sprintf('%d-e', $currency['currency_id']); |
||
148 | // $categories[] = (string)trans('firefly.no_category'); |
||
149 | // // make data arrays if not yet present. |
||
150 | // $tempData[$inKey] = $tempData[$inKey] ?? [ |
||
151 | // 'currency_id' => $currency['currency_id'], |
||
152 | // 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), |
||
153 | // 'currency_code' => $currency['currency_code'], |
||
154 | // 'currency_symbol' => $currency['currency_symbol'], |
||
155 | // 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
156 | // 'type' => 'bar', // line, area or bar |
||
157 | // 'yAxisID' => 0, // 0, 1, 2 |
||
158 | // 'entries' => [ |
||
159 | // // per category: |
||
160 | // // "category" => 5, |
||
161 | // ], |
||
162 | // ]; |
||
163 | // $tempData[$outKey] = $tempData[$outKey] ?? [ |
||
164 | // 'currency_id' => $currency['currency_id'], |
||
165 | // 'label' => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]), |
||
166 | // 'currency_code' => $currency['currency_code'], |
||
167 | // 'currency_symbol' => $currency['currency_symbol'], |
||
168 | // 'currency_decimal_places' => $currency['currency_decimal_places'], |
||
169 | // 'type' => 'bar', // line, area or bar |
||
170 | // 'yAxisID' => 0, // 0, 1, 2 |
||
171 | // 'entries' => [ |
||
172 | // // per category: |
||
173 | // // "category" => 5, |
||
174 | // ], |
||
175 | // ]; |
||
176 | // foreach ($currency['transaction_journals'] as $journal) { |
||
177 | // // is it expense or income? |
||
178 | // $letter = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i'; |
||
179 | // $currentKey = sprintf('%d-%s', $currency['currency_id'], $letter); |
||
180 | // $name = (string)trans('firefly.no_category'); |
||
181 | // $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0'; |
||
182 | // $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']); |
||
183 | // } |
||
184 | // } |
||
185 | // } |
||
186 | |||
187 | // re-sort every spent array and add 0 for missing entries. |
||
188 | foreach ($tempData as $index => $set) { |
||
189 | $oldSet = $set['entries']; |
||
190 | $newSet = []; |
||
191 | foreach ($categories as $category) { |
||
192 | $value = $oldSet[$category] ?? '0'; |
||
193 | $value = -1 === bccomp($value, '0') ? bcmul($value, '-1') : $value; |
||
194 | $newSet[$category] = $value; |
||
195 | } |
||
196 | $tempData[$index]['entries'] = $newSet; |
||
197 | } |
||
198 | $chartData = array_values($tempData); |
||
199 | |||
200 | return response()->json($chartData); |
||
201 | } |
||
203 |