| Conditions | 11 |
| Paths | 28 |
| Total Lines | 59 |
| Code Lines | 37 |
| 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 |
||
| 19 | public function work(ColonyInterface $colony, array &$production, InformationInterface $information): void |
||
| 20 | { |
||
| 21 | $sum = $colony->getStorageSum(); |
||
| 22 | |||
| 23 | //DECREASE |
||
| 24 | foreach ($production as $commodityId => $obj) { |
||
| 25 | $amount = $obj->getProduction(); |
||
| 26 | $commodity = $this->commodityCache->get($commodityId); |
||
| 27 | |||
| 28 | if ($amount < 0) { |
||
| 29 | $amount = abs($amount); |
||
| 30 | |||
| 31 | if ($commodity->isSaveable()) { |
||
| 32 | // STANDARD |
||
| 33 | $this->storageManager->lowerStorage( |
||
| 34 | $colony, |
||
| 35 | $this->commodityCache->get($commodityId), |
||
| 36 | $amount |
||
|
|
|||
| 37 | ); |
||
| 38 | $sum -= $amount; |
||
| 39 | } else { |
||
| 40 | // EFFECTS |
||
| 41 | $depositMining = $this->colonyDepositMiningRepository->getCurrentUserDepositMinings($colony)[$commodityId]; |
||
| 42 | |||
| 43 | $depositMining->setAmountLeft($depositMining->getAmountLeft() - $amount); |
||
| 44 | $this->colonyDepositMiningRepository->save($depositMining); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | foreach ($production as $commodityId => $obj) { |
||
| 50 | |||
| 51 | $commodity = $this->commodityCache->get($commodityId); |
||
| 52 | if ($obj->getProduction() <= 0 || !$commodity->isSaveable()) { |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | if ($sum >= $colony->getMaxStorage()) { |
||
| 56 | if ($colony->getUser()->isStorageNotification()) { |
||
| 57 | $information->addInformation('Das Lager der Kolonie ist voll'); |
||
| 58 | } |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | if ($sum + $obj->getProduction() > $colony->getMaxStorage()) { |
||
| 62 | $this->storageManager->upperStorage( |
||
| 63 | $colony, |
||
| 64 | $commodity, |
||
| 65 | $colony->getMaxStorage() - $sum |
||
| 66 | ); |
||
| 67 | if ($colony->getUser()->isStorageNotification()) { |
||
| 68 | $information->addInformation('Das Lager der Kolonie ist voll'); |
||
| 69 | } |
||
| 70 | break; |
||
| 71 | } |
||
| 72 | $this->storageManager->upperStorage( |
||
| 73 | $colony, |
||
| 74 | $commodity, |
||
| 75 | $obj->getProduction() |
||
| 76 | ); |
||
| 77 | $sum += $obj->getProduction(); |
||
| 78 | } |
||
| 81 |