1 | <?php |
||
2 | /** |
||
3 | * Support.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Generator\Report; |
||
24 | |||
25 | use FireflyIII\Models\Transaction; |
||
26 | use Illuminate\Support\Collection; |
||
27 | |||
28 | /** |
||
29 | * Class Support. |
||
30 | */ |
||
31 | class Support |
||
32 | { |
||
33 | /** |
||
34 | * @return Collection |
||
35 | */ |
||
36 | public function getTopExpenses(): Collection |
||
37 | { |
||
38 | return $this->getExpenses()->sortBy('transaction_amount'); |
||
0 ignored issues
–
show
|
|||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return Collection |
||
43 | */ |
||
44 | public function getTopIncome(): Collection |
||
45 | { |
||
46 | return $this->getIncome()->sortByDesc('transaction_amount'); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param Collection $collection |
||
51 | * @param int $sortFlag |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | protected function getAverages(Collection $collection, int $sortFlag): array |
||
56 | { |
||
57 | $result = []; |
||
58 | /** @var Transaction $transaction */ |
||
59 | foreach ($collection as $transaction) { |
||
60 | // opposing name and ID: |
||
61 | $opposingId = $transaction->opposing_account_id; |
||
62 | |||
63 | // is not set? |
||
64 | if (!isset($result[$opposingId])) { |
||
65 | $name = $transaction->opposing_account_name; |
||
66 | $result[$opposingId] = [ |
||
67 | 'name' => $name, |
||
68 | 'count' => 1, |
||
69 | 'id' => $opposingId, |
||
70 | 'average' => $transaction->transaction_amount, |
||
71 | 'sum' => $transaction->transaction_amount, |
||
72 | ]; |
||
73 | continue; |
||
74 | } |
||
75 | ++$result[$opposingId]['count']; |
||
76 | $result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount); |
||
77 | $result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], (string)$result[$opposingId]['count']); |
||
78 | } |
||
79 | |||
80 | // sort result by average: |
||
81 | $average = []; |
||
82 | foreach ($result as $key => $row) { |
||
83 | $average[$key] = (float)$row['average']; |
||
84 | } |
||
85 | |||
86 | array_multisort($average, $sortFlag, $result); |
||
87 | |||
88 | return $result; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five. |
||
93 | * |
||
94 | * @param array $spent |
||
95 | * @param array $earned |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function getObjectSummary(array $spent, array $earned): array |
||
100 | { |
||
101 | $return = [ |
||
102 | 'sum' => [ |
||
103 | 'spent' => '0', |
||
104 | 'earned' => '0', |
||
105 | ], |
||
106 | ]; |
||
107 | |||
108 | /** |
||
109 | * @var int |
||
110 | * @var string $entry |
||
111 | */ |
||
112 | foreach ($spent as $objectId => $entry) { |
||
113 | if (!isset($return[$objectId])) { |
||
114 | $return[$objectId] = ['spent' => '0', 'earned' => '0']; |
||
115 | } |
||
116 | |||
117 | $return[$objectId]['spent'] = $entry; |
||
118 | $return['sum']['spent'] = bcadd($return['sum']['spent'], $entry); |
||
119 | } |
||
120 | unset($entry); |
||
121 | |||
122 | /** |
||
123 | * @var int |
||
124 | * @var string $entry |
||
125 | */ |
||
126 | foreach ($earned as $objectId => $entry) { |
||
127 | if (!isset($return[$objectId])) { |
||
128 | $return[$objectId] = ['spent' => '0', 'earned' => '0']; |
||
129 | } |
||
130 | |||
131 | $return[$objectId]['earned'] = $entry; |
||
132 | $return['sum']['earned'] = bcadd($return['sum']['earned'], $entry); |
||
133 | } |
||
134 | |||
135 | return $return; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param Collection $collection |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function summarizeByAccount(Collection $collection): array |
||
144 | { |
||
145 | $result = []; |
||
146 | /** @var Transaction $transaction */ |
||
147 | foreach ($collection as $transaction) { |
||
148 | $accountId = $transaction->account_id; |
||
149 | $result[$accountId] = $result[$accountId] ?? '0'; |
||
150 | $result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]); |
||
151 | } |
||
152 | |||
153 | return $result; |
||
154 | } |
||
155 | } |
||
156 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.