Conditions | 17 |
Paths | 324 |
Total Lines | 98 |
Code Lines | 48 |
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 |
||
93 | protected function getAccount(string $transactionType, string $direction, array $data): Account |
||
94 | { |
||
95 | // some debug logging: |
||
96 | Log::debug(sprintf('Now in getAccount(%s)', $direction), $data); |
||
97 | |||
98 | // final result: |
||
99 | $result = null; |
||
100 | |||
101 | // expected type of source account, in order of preference |
||
102 | /** @var array $array */ |
||
103 | $array = config('firefly.expected_source_types'); |
||
104 | $expectedTypes = $array[$direction]; |
||
105 | unset($array); |
||
106 | |||
107 | // and now try to find it, based on the type of transaction. |
||
108 | $message = 'Based on the fact that the transaction is a %s, the %s account should be in: %s'; |
||
109 | Log::debug(sprintf($message, $transactionType, $direction, implode(', ', $expectedTypes[$transactionType]))); |
||
110 | |||
111 | // first attempt, find by ID. |
||
112 | if (null !== $data['id']) { |
||
113 | $search = $this->accountRepository->findNull($data['id']); |
||
114 | if (null !== $search && in_array($search->accountType->type, $expectedTypes[$transactionType], true)) { |
||
115 | Log::debug( |
||
116 | sprintf('Found "account_id" object for %s: #%d, "%s" of type %s', $direction, $search->id, $search->name, $search->accountType->type) |
||
117 | ); |
||
118 | $result = $search; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | // second attempt, find by name. |
||
123 | if (null === $result && null !== $data['name']) { |
||
124 | Log::debug('Found nothing by account ID.'); |
||
125 | // find by preferred type. |
||
126 | $source = $this->accountRepository->findByName($data['name'], [$expectedTypes[$transactionType][0]]); |
||
127 | // or any expected type. |
||
128 | $source = $source ?? $this->accountRepository->findByName($data['name'], $expectedTypes[$transactionType]); |
||
129 | |||
130 | if (null !== $source) { |
||
131 | Log::debug(sprintf('Found "account_name" object for %s: #%d, %s', $direction, $source->id, $source->name)); |
||
132 | |||
133 | $result = $source; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | // third attempt, find by IBAN |
||
138 | if (null === $result && null !== $data['iban']) { |
||
139 | Log::debug('Found nothing by account name.'); |
||
140 | // find by preferred type. |
||
141 | $source = $this->accountRepository->findByIbanNull($data['iban'], [$expectedTypes[$transactionType][0]]); |
||
142 | // or any expected type. |
||
143 | $source = $source ?? $this->accountRepository->findByIbanNull($data['iban'], $expectedTypes[$transactionType]); |
||
144 | |||
145 | if (null !== $source) { |
||
146 | Log::debug(sprintf('Found "account_iban" object for %s: #%d, %s', $direction, $source->id, $source->name)); |
||
147 | |||
148 | $result = $source; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // return cash account. |
||
153 | if (null === $result && null === $data['name'] |
||
154 | && in_array(AccountType::CASH, $expectedTypes[$transactionType], true)) { |
||
155 | $result = $this->accountRepository->getCashAccount(); |
||
156 | } |
||
157 | |||
158 | // return new account. |
||
159 | if (null === $result) { |
||
160 | $data['name'] = $data['name'] ?? '(no name)'; |
||
161 | // final attempt, create it. |
||
162 | $preferredType = $expectedTypes[$transactionType][0]; |
||
163 | if (AccountType::ASSET === $preferredType) { |
||
164 | throw new FireflyException('TransactionFactory: Cannot create asset account with these values', $data); |
||
|
|||
165 | } |
||
166 | |||
167 | $result = $this->accountRepository->store( |
||
168 | [ |
||
169 | 'account_type_id' => null, |
||
170 | 'account_type' => $preferredType, |
||
171 | 'name' => $data['name'], |
||
172 | 'active' => true, |
||
173 | 'iban' => $data['iban'], |
||
174 | ] |
||
175 | ); |
||
176 | // store BIC |
||
177 | if (null !== $data['bic']) { |
||
178 | /** @var AccountMetaFactory $metaFactory */ |
||
179 | $metaFactory = app(AccountMetaFactory::class); |
||
180 | $metaFactory->create(['account_id' => $result->id, 'name' => 'BIC', 'data' => $data['bic']]); |
||
181 | } |
||
182 | // store account number |
||
183 | if (null !== $data['number']) { |
||
184 | /** @var AccountMetaFactory $metaFactory */ |
||
185 | $metaFactory = app(AccountMetaFactory::class); |
||
186 | $metaFactory->create(['account_id' => $result->id, 'name' => 'account_number', 'data' => $data['bic']]); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return $result; |
||
191 | } |
||
318 |