Test Failed
Pull Request — master (#2248)
by Janko
15:28 queued 07:43
created

SpacecraftConfigurator::transferCrew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
    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
    ) {}
39
40
    #[Override]
41
    public function setLocation(Location $location): SpacecraftConfiguratorInterface
42
    {
43
        $this->wrapper->get()->setLocation($location);
44
45
        return $this;
46
    }
47
48
    #[Override]
49
    public function loadEps(int $percentage): SpacecraftConfiguratorInterface
50
    {
51
        $epsSystem = $this->wrapper->getEpsSystemData();
52
53
        if ($epsSystem !== null) {
54
            $epsSystem
55
                ->setEps((int)floor($epsSystem->getTheoreticalMaxEps() / 100 * $percentage))
56
                ->update();
57
        }
58
59
        return $this;
60
    }
61
62
    #[Override]
63
    public function loadBattery(int $percentage): SpacecraftConfiguratorInterface
64
    {
65
        $epsSystem = $this->wrapper->getEpsSystemData();
66
67
        if ($epsSystem !== null) {
68
            $epsSystem
69
                ->setBattery((int)floor($epsSystem->getMaxBattery() / 100 * $percentage))
70
                ->update();
71
        }
72
73
        return $this;
74
    }
75
76
    #[Override]
77
    public function loadReactor(int $percentage): SpacecraftConfiguratorInterface
78
    {
79
        $reactor = $this->wrapper->getReactorWrapper();
80
        if ($reactor !== null) {
81
            $reactor->setLoad((int)floor($reactor->getCapacity() / 100 * $percentage));
82
        }
83
84
        return $this;
85
    }
86
87
    #[Override]
88
    public function loadWarpdrive(int $percentage): SpacecraftConfiguratorInterface
89
    {
90
        $warpdrive = $this->wrapper->getWarpDriveSystemData();
91
        if ($warpdrive !== null) {
92
            $warpdrive
93
                ->setWarpDrive((int)floor($warpdrive->getMaxWarpdrive() / 100 * $percentage))
94
                ->update();
95
        }
96
97
        return $this;
98
    }
99
100
    #[Override]
101
    public function maxOutSystems(): SpacecraftConfiguratorInterface
102
    {
103
        $this->loadEps(100)
104
            ->loadReactor(100)
105
            ->loadWarpdrive(100)
106
            ->loadBattery(100);
107
108
        $ship = $this->wrapper->get();
109
110
        $ship->getCondition()->setShield($ship->getMaxShield());
111
112
        return $this;
113
    }
114
115
    #[Override]
116
    public function createCrew(?int $amount = null): SpacecraftConfiguratorInterface
117
    {
118
        $spacecraft = $this->wrapper->get();
119
120
        $buildplan = $spacecraft->getBuildplan();
121
        if ($buildplan === null) {
122
            return $this;
123
        }
124
125
        $crewAmount = $amount !== null && $amount >= 0 ? $amount : $buildplan->getCrew();
126
127
        for ($j = 1; $j <= $crewAmount; $j++) {
128
            $crewAssignment = $this->crewCreator->create($spacecraft->getUser()->getId());
129
            $crewAssignment->setSpacecraft($spacecraft);
130
            $this->shipCrewRepository->save($crewAssignment);
131
132
            $spacecraft->getCrewAssignments()->add($crewAssignment);
133
        }
134
135
        $this->spacecraftStartup->startup($this->wrapper);
136
137
        return $this;
138
    }
139
140
    #[Override]
141
    public function transferCrew(EntityWithCrewAssignmentsInterface $provider): SpacecraftConfiguratorInterface
142
    {
143
        $ship = $this->wrapper->get();
144
145
        $buildplan = $ship->getBuildplan();
146
        if ($buildplan === null) {
147
            return $this;
148
        }
149
150
        $this->crewCreator->createCrewAssignments($ship, $provider, $buildplan->getCrew());
151
152
        $this->spacecraftStartup->startup($this->wrapper);
153
154
        return $this;
155
    }
156
157
    #[Override]
158
    public function setAlertState(SpacecraftAlertStateEnum $alertState): SpacecraftConfiguratorInterface
159
    {
160
        $this->alertStateManager->setAlertState(
161
            $this->wrapper,
162
            $alertState
163
        );
164
165
        return $this;
166
    }
167
168
    #[Override]
169
    public function setTorpedo(?int $torpedoTypeId = null): SpacecraftConfiguratorInterface
170
    {
171
        $spacecraft = $this->wrapper->get();
172
        if ($spacecraft->getMaxTorpedos() === 0) {
173
            return $this;
174
        }
175
176
        if ($torpedoTypeId !== null) {
177
            $torpedoType = $this->torpedoTypeRepository->find($torpedoTypeId);
178
            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
        $this->torpedoManager->changeTorpedo($this->wrapper, $spacecraft->getMaxTorpedos(), $torpedoType);
192
193
        return $this;
194
    }
195
196
    #[Override]
197
    public function setSpacecraftName(string $name): SpacecraftConfiguratorInterface
198
    {
199
        $this->wrapper->get()->setName($name);
200
201
        return $this;
202
    }
203
204
    #[Override]
205
    public function finishConfiguration(): SpacecraftWrapperInterface
206
    {
207
        $this->spacecraftRepository->save($this->wrapper->get());
208
209
        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