Passed
Push — master ( 2d0ecd...a65ee9 )
by Nico
07:51
created

ShipMovementBlockingDeterminator::determine()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 10

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 34
c 1
b 1
f 0
nc 11
nop 1
dl 0
loc 62
ccs 38
cts 38
cp 1
crap 10
rs 7.6666

How to fix   Long Method    Complexity   

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 5
    public function determine(array $wrappers): array
15
    {
16 5
        $reasons = [];
17
18 5
        foreach ($wrappers as $wrapper) {
19 5
            $ship = $wrapper->get();
20
21
            // zu wenig Crew
22 5
            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 4
            $epsSystem = $wrapper->getEpsSystemData();
33
34 4
            $energyCostPerField = $ship->getRump()->getFlightEcost();
35 4
            $tractorBeamTarget = $ship->getTractoringShip();
36 4
            $shipEnergyStorage = $epsSystem->getEps();
37 4
            $warpdrivesystem = $wrapper->getWarpDriveSystemData();
38
39 4
            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 3
            if ($ship->getImpulseState() && $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
            if (
64 3
                $ship->getWarpState()
65 3
                && ($warpdrivesystem === null
66 3
                    || $warpdrivesystem->getWarpDrive() < 1)
67
            ) {
68 1
                $reasons[] = sprintf(
69 1
                    'Die %s hat nicht genug Warpantriebsenergie für den Flug',
70 1
                    $ship->getName()
71 1
                );
72
            }
73
        }
74
75 5
        return $reasons;
76
    }
77
}
78