| Conditions | 15 |
| Paths | 72 |
| Total Lines | 67 |
| 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 |
||
| 35 | public function transferCommodity( |
||
| 36 | int $commodityId, |
||
| 37 | string|int $wantedAmount, |
||
| 38 | ShipWrapperInterface|ColonyInterface $subject, |
||
| 39 | ShipInterface|ColonyInterface $source, |
||
| 40 | ShipInterface|ColonyInterface $target, |
||
| 41 | GameControllerInterface $game |
||
| 42 | ): void { |
||
| 43 | |||
| 44 | $sourceStorage = $source->getStorage()[$commodityId] ?? null; |
||
| 45 | if ($sourceStorage === null) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | $commodity = $sourceStorage->getCommodity(); |
||
| 50 | if (!$commodity->isBeamable($source->getUser(), $target->getUser())) { |
||
| 51 | $game->addInformationf(_('%s ist nicht beambar'), $commodity->getName()); |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $isDockTransfer = $source instanceof ShipInterface && $target instanceof ShipInterface |
||
| 56 | && ($source->getDockedTo() === $target || $target->getDockedTo() === $source); |
||
| 57 | |||
| 58 | $availableEps = $this->getAvailableEps($subject); |
||
| 59 | if (!$isDockTransfer && $availableEps < 1) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($wantedAmount === "max") { |
||
| 64 | $amount = $sourceStorage->getAmount(); |
||
| 65 | } else if (!is_numeric($wantedAmount)) { |
||
| 66 | return; |
||
| 67 | } else { |
||
| 68 | $amount = (int)$wantedAmount; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($amount < 1) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($target->getStorageSum() >= $target->getMaxStorage()) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $amount = min($amount, $sourceStorage->getAmount()); |
||
| 80 | $transferAmount = $commodity->getTransferCount() * $this->getBeamFactor($subject); |
||
| 81 | |||
| 82 | if (!$isDockTransfer && ceil($amount / $transferAmount) > $availableEps) { |
||
| 83 | $amount = $availableEps * $transferAmount; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($target->getStorageSum() + $amount > $target->getMaxStorage()) { |
||
| 87 | $amount = $target->getMaxStorage() - $target->getStorageSum(); |
||
| 88 | } |
||
| 89 | |||
| 90 | $epsUsage = (int)ceil($amount / $transferAmount); |
||
| 91 | |||
| 92 | $game->addInformationf( |
||
| 93 | _('%d %s (Energieverbrauch: %d)'), |
||
| 94 | $amount, |
||
| 95 | $commodity->getName(), |
||
| 96 | $epsUsage |
||
| 97 | ); |
||
| 98 | |||
| 99 | $this->lowerSourceStorage($amount, $commodity, $source); |
||
| 100 | $this->upperTargetStorage($amount, $commodity, $target); |
||
| 101 | $this->consumeEps($epsUsage, $subject); |
||
| 102 | } |
||
| 166 |