Passed
Push — master ( 5df554...4ae4ec )
by Nico
21:05 queued 13:30
created

ShipMovementBlockingDeterminator::determine()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 7.0671

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 31
c 1
b 1
f 0
nc 11
nop 1
dl 0
loc 58
ccs 32
cts 36
cp 0.8889
crap 7.0671
rs 8.4906

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Movement\Component;
6
7
use Stu\Component\Ship\System\Data\EpsSystemData;
8
9
/**
10
 * Looks for reasons a certain ship cannot move from its position
11
 */
12
final class ShipMovementBlockingDeterminator implements ShipMovementBlockingDeterminatorInterface
13
{
14 4
    public function determine(array $wrappers): array
15
    {
16 4
        $reasons = [];
17
18 4
        foreach ($wrappers as $wrapper) {
19 4
            $ship = $wrapper->get();
20
21
            // zu wenig Crew
22 4
            if (!$ship->hasEnoughCrew()) {
23 1
                $reasons[] = sprintf(
24 1
                    'Die %s hat ungenügend Crew',
25 1
                    $ship->getName()
26 1
                );
27
28 1
                continue;
29
            }
30
31
            /** @var EpsSystemData $epsSystem */
32 3
            $epsSystem = $wrapper->getEpsSystemData();
33
34 3
            $energyCostPerField = $ship->getRump()->getFlightEcost();
35 3
            $tractorBeamTarget = $ship->getTractoringShip();
36 3
            $shipEnergyStorage = $epsSystem->getEps();
37 3
            $warpdrivesystem = $wrapper->getWarpDriveSystemData();
38
39 3
            if ($tractorBeamTarget !== null) {
40 1
                $tractoredShipEnergyCostPerField = $tractorBeamTarget->getRump()->getFlightEcost() + $energyCostPerField;
41
42 1
                if ($shipEnergyStorage < $tractoredShipEnergyCostPerField) {
43 1
                    $reasons[] = sprintf(
44 1
                        'Die %s hat nicht genug Energie für den Traktor-Flug (%d benötigt)',
45 1
                        $ship->getName(),
46 1
                        $tractoredShipEnergyCostPerField
47 1
                    );
48
49 1
                    continue;
50
                }
51
            }
52
53
            // zu wenig E zum weiterfliegen
54 2
            if ($shipEnergyStorage < $energyCostPerField) {
55 1
                $reasons[] = sprintf(
56 1
                    'Die %s hat nicht genug Energie für den Flug (%d benötigt)',
57 1
                    $ship->getName(),
58 1
                    $energyCostPerField
59 1
                );
60
            }
61
62
            // zu wenig Warpantriebsenergie zum weiterfliegen
63 2
            if ($warpdrivesystem->getWarpDrive() < 1) {
64
                $reasons[] = sprintf(
65
                    'Die %s hat nicht genug Warpantriebsenergie für den Flug',
66
                    $ship->getName()
67
                );
68
            }
69
        }
70
71 4
        return $reasons;
72
    }
73
}
74