Test Failed
Push — dev ( 0a21cd...88787c )
by Janko
08:43
created

SpacecraftConfigurator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Creation;
4
5
use InvalidArgumentException;
6
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
8
use Stu\Module\Crew\Lib\CrewCreatorInterface;
9
use Stu\Component\Spacecraft\System\Control\AlertStateManagerInterface;
10
use Stu\Module\Spacecraft\Lib\Auxiliary\SpacecraftStartupInterface;
11
use Stu\Module\Spacecraft\Lib\Crew\EntityWithCrewAssignmentsInterface;
12
use Stu\Module\Spacecraft\Lib\Torpedo\ShipTorpedoManagerInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\Location;
15
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
16
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
17
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
18
19
/**
20
 * @template T of SpacecraftWrapperInterface
21
 *
22
 * @implements SpacecraftConfiguratorInterface<T>
23
 */
24
class SpacecraftConfigurator implements SpacecraftConfiguratorInterface
25
{
26
    /**
27
     * @psalm-param T $wrapper
28
     */
29 12
    public function __construct(
30
        private readonly SpacecraftWrapperInterface $wrapper,
31
        private readonly TorpedoTypeRepositoryInterface $torpedoTypeRepository,
32
        private readonly ShipTorpedoManagerInterface $torpedoManager,
33
        private readonly CrewCreatorInterface $crewCreator,
34
        private readonly CrewAssignmentRepositoryInterface $shipCrewRepository,
35
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
36
        private readonly AlertStateManagerInterface $alertStateManager,
37
        private readonly SpacecraftStartupInterface $spacecraftStartup
38 12
    ) {}
39
40 1
    #[Override]
41
    public function setLocation(Location $location): SpacecraftConfiguratorInterface
42
    {
43 1
        $this->wrapper->get()->setLocation($location);
44
45 1
        return $this;
46
    }
47
48 2
    #[Override]
49
    public function loadEps(int $percentage): SpacecraftConfiguratorInterface
50
    {
51 2
        $epsSystem = $this->wrapper->getEpsSystemData();
52
53 2
        if ($epsSystem !== null) {
54 2
            $epsSystem
55 2
                ->setEps((int)floor($epsSystem->getTheoreticalMaxEps() / 100 * $percentage))
56 2
                ->update();
57
        }
58
59 2
        return $this;
60
    }
61
62 2
    #[Override]
63
    public function loadBattery(int $percentage): SpacecraftConfiguratorInterface
64
    {
65 2
        $epsSystem = $this->wrapper->getEpsSystemData();
66
67 2
        if ($epsSystem !== null) {
68 2
            $epsSystem
69 2
                ->setBattery((int)floor($epsSystem->getMaxBattery() / 100 * $percentage))
70 2
                ->update();
71
        }
72
73 2
        return $this;
74
    }
75
76 2
    #[Override]
77
    public function loadReactor(int $percentage): SpacecraftConfiguratorInterface
78
    {
79 2
        $reactor = $this->wrapper->getReactorWrapper();
80 2
        if ($reactor !== null) {
81 2
            $reactor->setLoad((int)floor($reactor->getCapacity() / 100 * $percentage));
82
        }
83
84 2
        return $this;
85
    }
86
87 2
    #[Override]
88
    public function loadWarpdrive(int $percentage): SpacecraftConfiguratorInterface
89
    {
90 2
        $warpdrive = $this->wrapper->getWarpDriveSystemData();
91 2
        if ($warpdrive !== null) {
92 2
            $warpdrive
93 2
                ->setWarpDrive((int)floor($warpdrive->getMaxWarpdrive() / 100 * $percentage))
94 2
                ->update();
95
        }
96
97 2
        return $this;
98
    }
99
100 1
    #[Override]
101
    public function maxOutSystems(): SpacecraftConfiguratorInterface
102
    {
103 1
        $this->loadEps(100)
104 1
            ->loadReactor(100)
105 1
            ->loadWarpdrive(100)
106 1
            ->loadBattery(100);
107
108 1
        $ship = $this->wrapper->get();
109
110 1
        $ship->getCondition()->setShield($ship->getMaxShield());
111
112 1
        return $this;
113
    }
114
115 1
    #[Override]
116
    public function createCrew(?int $amount = null): SpacecraftConfiguratorInterface
117
    {
118 1
        $spacecraft = $this->wrapper->get();
119
120 1
        $buildplan = $spacecraft->getBuildplan();
121 1
        if ($buildplan === null) {
122
            return $this;
123
        }
124
125 1
        $crewAmount = $amount !== null && $amount >= 0 ? $amount : $buildplan->getCrew();
126
127 1
        for ($j = 1; $j <= $crewAmount; $j++) {
128 1
            $crewAssignment = $this->crewCreator->create($spacecraft->getUser()->getId());
129 1
            $crewAssignment->setSpacecraft($spacecraft);
130 1
            $this->shipCrewRepository->save($crewAssignment);
131
132 1
            $spacecraft->getCrewAssignments()->add($crewAssignment);
133
        }
134
135 1
        $this->spacecraftStartup->startup($this->wrapper);
136
137 1
        return $this;
138
    }
139
140 1
    #[Override]
141
    public function transferCrew(EntityWithCrewAssignmentsInterface $provider): SpacecraftConfiguratorInterface
142
    {
143 1
        $ship = $this->wrapper->get();
144
145 1
        $buildplan = $ship->getBuildplan();
146 1
        if ($buildplan === null) {
147
            return $this;
148
        }
149
150 1
        $this->crewCreator->createCrewAssignments($ship, $provider, $buildplan->getCrew());
151
152 1
        $this->spacecraftStartup->startup($this->wrapper);
153
154 1
        return $this;
155
    }
156
157 1
    #[Override]
158
    public function setAlertState(SpacecraftAlertStateEnum $alertState): SpacecraftConfiguratorInterface
159
    {
160 1
        $this->alertStateManager->setAlertState(
161 1
            $this->wrapper,
162 1
            $alertState
163 1
        );
164
165 1
        return $this;
166
    }
167
168 1
    #[Override]
169
    public function setTorpedo(?int $torpedoTypeId = null): SpacecraftConfiguratorInterface
170
    {
171 1
        $spacecraft = $this->wrapper->get();
172 1
        if ($spacecraft->getMaxTorpedos() === 0) {
173
            return $this;
174
        }
175
176 1
        if ($torpedoTypeId !== null) {
177 1
            $torpedoType = $this->torpedoTypeRepository->find($torpedoTypeId);
178 1
            if ($torpedoType === null) {
179
                throw new InvalidArgumentException(sprintf('torpedoTypeId %d does not exist', $torpedoTypeId));
180
            }
181
        } else {
182
            $torpedoTypes = $this->torpedoTypeRepository->getByLevel($spacecraft->getRump()->getTorpedoLevel());
183
            if ($torpedoTypes === []) {
184
                return $this;
185
            }
186
187
            shuffle($torpedoTypes);
188
            $torpedoType = current($torpedoTypes);
189
        }
190
191 1
        $this->torpedoManager->changeTorpedo($this->wrapper, $spacecraft->getMaxTorpedos(), $torpedoType);
192
193 1
        return $this;
194
    }
195
196 1
    #[Override]
197
    public function setSpacecraftName(string $name): SpacecraftConfiguratorInterface
198
    {
199 1
        $this->wrapper->get()->setName($name);
200
201 1
        return $this;
202
    }
203
204 1
    #[Override]
205
    public function finishConfiguration(): SpacecraftWrapperInterface
206
    {
207 1
        $this->spacecraftRepository->save($this->wrapper->get());
208
209 1
        return $this->wrapper;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->wrapper returns the type Stu\Module\Spacecraft\Li...cecraftWrapperInterface which is incompatible with the return type mandated by Stu\Module\Spacecraft\Li...::finishConfiguration() of Stu\Module\Spacecraft\Lib\Creation\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
210
    }
211
}
212