Conditions | 7 |
Paths | 17 |
Total Lines | 105 |
Code Lines | 64 |
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 |
||
151 | protected function storeImportJournal(int $index, ImportJournal $importJournal): bool |
||
152 | { |
||
153 | Log::debug(sprintf('Going to store object #%d with description "%s"', $index, $importJournal->getDescription())); |
||
154 | $assetAccount = $importJournal->asset->getAccount(); |
||
155 | $amount = $importJournal->getAmount(); |
||
156 | $currencyId = $this->getCurrencyId($importJournal); |
||
157 | $foreignCurrencyId = $this->getForeignCurrencyId($importJournal, $currencyId); |
||
158 | $date = $importJournal->getDate($this->dateFormat)->format('Y-m-d'); |
||
159 | $opposingAccount = $this->getOpposingAccount($importJournal->opposing, $assetAccount->id, $amount); |
||
160 | $transactionType = $this->getTransactionType($amount, $opposingAccount); |
||
161 | $description = $importJournal->getDescription(); |
||
162 | |||
163 | // First step done! |
||
164 | $this->job->addStepsDone(1); |
||
165 | |||
166 | /** |
||
167 | * Check for double transfer. |
||
168 | */ |
||
169 | $parameters = [ |
||
170 | 'type' => $transactionType, |
||
171 | 'description' => $description, |
||
172 | 'amount' => $amount, |
||
173 | 'date' => $date, |
||
174 | 'asset' => $assetAccount->name, |
||
175 | 'opposing' => $opposingAccount->name, |
||
176 | ]; |
||
177 | if ($this->isDoubleTransfer($parameters) || $this->hashAlreadyImported($importJournal->hash)) { |
||
178 | $this->job->addStepsDone(3); |
||
179 | // throw error |
||
180 | $message = sprintf('Detected a possible duplicate, skip this one (hash: %s).', $importJournal->hash); |
||
181 | Log::error($message, $parameters); |
||
182 | throw new FireflyException($message); |
||
183 | } |
||
184 | unset($parameters); |
||
185 | |||
186 | // store journal and create transactions: |
||
187 | $parameters = [ |
||
188 | 'type' => $transactionType, |
||
189 | 'currency' => $currencyId, |
||
190 | 'foreign_currency' => $foreignCurrencyId, |
||
191 | 'asset' => $assetAccount, |
||
192 | 'opposing' => $opposingAccount, |
||
193 | 'description' => $description, |
||
194 | 'date' => $date, |
||
195 | 'hash' => $importJournal->hash, |
||
196 | 'amount' => $amount, |
||
197 | ]; |
||
198 | $journal = $this->storeJournal($parameters); |
||
199 | unset($parameters); |
||
200 | |||
201 | // Another step done! |
||
202 | $this->job->addStepsDone(1); |
||
203 | |||
204 | // store meta object things: |
||
205 | $this->storeCategory($journal, $importJournal->category->getCategory()); |
||
206 | $this->storeBudget($journal, $importJournal->budget->getBudget()); |
||
207 | |||
208 | // to save bill, also give it the amount: |
||
209 | $importJournal->bill->setAmount($amount); |
||
210 | |||
211 | $this->storeBill($journal, $importJournal->bill->getBill()); |
||
212 | $this->storeMeta($journal, $importJournal->metaDates); |
||
213 | $this->storeTags($importJournal->tags, $journal); |
||
214 | |||
215 | // set notes for journal: |
||
216 | $dbNote = new Note(); |
||
217 | $dbNote->noteable()->associate($journal); |
||
218 | $dbNote->text = trim($importJournal->notes); |
||
219 | $dbNote->save(); |
||
220 | |||
221 | // set journal completed: |
||
222 | $journal->completed = true; |
||
223 | $journal->save(); |
||
224 | |||
225 | // Another step done! |
||
226 | $this->job->addStepsDone(1); |
||
227 | |||
228 | // run rules if config calls for it: |
||
229 | if ($this->applyRules === true) { |
||
230 | Log::info('Will apply rules to this journal.'); |
||
231 | $this->applyRules($journal); |
||
232 | } |
||
233 | if (!($this->applyRules === true)) { |
||
234 | Log::info('Will NOT apply rules to this journal.'); |
||
235 | } |
||
236 | |||
237 | // match bills if config calls for it. |
||
238 | if ($this->matchBills === true) { |
||
239 | //$this->/applyRules($journal); |
||
|
|||
240 | Log::info('Cannot match bills (yet).'); |
||
241 | $this->matchBills($journal); |
||
242 | } |
||
243 | |||
244 | if (!($this->matchBills === true)) { |
||
245 | Log::info('Cannot match bills (yet), but do not have to.'); |
||
246 | } |
||
247 | |||
248 | // Another step done! |
||
249 | $this->job->addStepsDone(1); |
||
250 | $this->journals->push($journal); |
||
251 | |||
252 | Log::info(sprintf('Imported new journal #%d: "%s", amount %s %s.', $journal->id, $journal->description, $journal->transactionCurrency->code, $amount)); |
||
253 | |||
254 | return true; |
||
255 | } |
||
256 | |||
309 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.