| Conditions | 11 |
| Paths | 9 |
| Total Lines | 62 |
| Code Lines | 36 |
| 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 |
||
| 15 | #[\Override] |
||
| 16 | public function manage(SpacecraftWrapperInterface $wrapper, array $values, ManagerProviderInterface $managerProvider): array |
||
| 17 | { |
||
| 18 | $values = $values['warpcore_transfer'] ?? null; |
||
| 19 | if ($values === null) { |
||
| 20 | throw new RuntimeException('value array not existent'); |
||
| 21 | } |
||
| 22 | |||
| 23 | $targetSpacecraft = $wrapper->get(); |
||
| 24 | |||
| 25 | if (!array_key_exists($targetSpacecraft->getId(), $values)) { |
||
| 26 | return []; |
||
| 27 | } |
||
| 28 | |||
| 29 | if ($values[$targetSpacecraft->getId()] === '' || $values[$targetSpacecraft->getId()] < 1) { |
||
| 30 | return []; |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!$managerProvider instanceof ManagerProviderSpacecraft) { |
||
| 34 | return []; |
||
| 35 | } |
||
| 36 | |||
| 37 | $requestedAmount = (int)$values[$targetSpacecraft->getId()]; |
||
| 38 | |||
| 39 | $targetReactor = $wrapper->getReactorWrapper(); |
||
| 40 | if ($targetReactor === null) { |
||
| 41 | return []; |
||
| 42 | } |
||
| 43 | |||
| 44 | if ( |
||
| 45 | $targetSpacecraft->getSystemState(SpacecraftSystemTypeEnum::WARPDRIVE) || |
||
| 46 | $targetSpacecraft->getSystemState(SpacecraftSystemTypeEnum::SHIELDS) |
||
| 47 | ) { |
||
| 48 | return [sprintf( |
||
| 49 | '%s: Warpantrieb und Schilde müssen deaktiviert sein', |
||
| 50 | $targetSpacecraft->getName() |
||
| 51 | )]; |
||
| 52 | } |
||
| 53 | |||
| 54 | $availableSourceLoad = $managerProvider->getReactorLoad(); |
||
| 55 | $targetCapacity = $targetReactor->getCapacity() - $targetReactor->getLoad(); |
||
| 56 | |||
| 57 | if ($availableSourceLoad <= 0) { |
||
| 58 | return []; |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($targetCapacity <= 0) { |
||
| 62 | return [sprintf( |
||
| 63 | '%s: Warpkern ist bereits voll geladen', |
||
| 64 | $targetSpacecraft->getName() |
||
| 65 | )]; |
||
| 66 | } |
||
| 67 | |||
| 68 | $actualTransfer = min($requestedAmount, $availableSourceLoad, $targetCapacity); |
||
| 69 | |||
| 70 | $targetReactor->changeLoad((int)$actualTransfer); |
||
| 71 | $managerProvider->lowerReactorLoad((int)$actualTransfer); |
||
| 72 | |||
| 73 | return [sprintf( |
||
| 74 | 'Warpkern-Ladung von %d an %s übertragen', |
||
| 75 | $actualTransfer, |
||
| 76 | $targetSpacecraft->getName() |
||
| 77 | )]; |
||
| 80 |