Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

SpacecraftConfigurator::setTorpedo()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0073

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 6
nop 1
dl 0
loc 33
ccs 16
cts 17
cp 0.9412
crap 6.0073
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Creation;
4
5
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...
6
use RuntimeException;
7
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Module\Crew\Lib\CrewCreatorInterface;
11
use Stu\Component\Spacecraft\System\Control\AlertStateManagerInterface;
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 13
    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
    ) {}
38 13
39
    #[Override]
40 2
    public function setLocation(Location $location): SpacecraftConfiguratorInterface
41
    {
42
        $this->wrapper->get()->setLocation($location);
43 2
44
        return $this;
45 2
    }
46
47
    #[Override]
48 3
    public function loadEps(int $percentage): SpacecraftConfiguratorInterface
49
    {
50
        $epsSystem = $this->wrapper->getEpsSystemData();
51 3
52
        if ($epsSystem !== null) {
53 3
            $epsSystem
54 3
                ->setEps((int)floor($epsSystem->getTheoreticalMaxEps() / 100 * $percentage))
55 3
                ->update();
56 3
        }
57
58
        return $this;
59 3
    }
60
61
    #[Override]
62 3
    public function loadBattery(int $percentage): SpacecraftConfiguratorInterface
63
    {
64
        $epsSystem = $this->wrapper->getEpsSystemData();
65 3
66
        if ($epsSystem !== null) {
67 3
            $epsSystem
68 3
                ->setBattery((int)floor($epsSystem->getMaxBattery() / 100 * $percentage))
69 3
                ->update();
70 3
        }
71
72
        return $this;
73 3
    }
74
75
    #[Override]
76 3
    public function loadReactor(int $percentage): SpacecraftConfiguratorInterface
77
    {
78
        $reactor = $this->wrapper->getReactorWrapper();
79 3
        if ($reactor !== null) {
80 3
            $reactor->setLoad((int)floor($reactor->getCapacity() / 100 * $percentage));
81 3
        }
82
83
        return $this;
84 3
    }
85
86
    #[Override]
87 3
    public function loadWarpdrive(int $percentage): SpacecraftConfiguratorInterface
88
    {
89
        $warpdrive = $this->wrapper->getWarpDriveSystemData();
90 3
        if ($warpdrive !== null) {
91 3
            $warpdrive
92 3
                ->setWarpDrive((int)floor($warpdrive->getMaxWarpdrive() / 100 * $percentage))
93 3
                ->update();
94 3
        }
95
96
        return $this;
97 3
    }
98
99
    #[Override]
100 2
    public function maxOutSystems(): SpacecraftConfiguratorInterface
101
    {
102
        $this->loadEps(100)
103 2
            ->loadReactor(100)
104 2
            ->loadWarpdrive(100)
105 2
            ->loadBattery(100);
106 2
107
        $ship = $this->wrapper->get();
108 2
109
        $ship->getCondition()->setShield($ship->getMaxShield());
110 2
111
        return $this;
112 2
    }
113
114
    #[Override]
115 2
    public function createCrew(?int $amount = null): SpacecraftConfiguratorInterface
116
    {
117
        $ship = $this->wrapper->get();
118 2
119
        $buildplan = $ship->getBuildplan();
120 2
        if ($buildplan !== null) {
121 2
            $crewAmount = $amount !== null && $amount >= 0 ? $amount : $buildplan->getCrew();
122
            for ($j = 1; $j <= $crewAmount; $j++) {
123
                $crewAssignment = $this->crewCreator->create($ship->getUser()->getId());
124
                $crewAssignment->setSpacecraft($ship);
125 2
                $this->shipCrewRepository->save($crewAssignment);
126
127 2
                $ship->getCrewAssignments()->add($crewAssignment);
128 2
            }
129 2
            if ($crewAmount > 0) {
130 2
                $ship->getSpacecraftSystem(SpacecraftSystemTypeEnum::LIFE_SUPPORT)->setMode(SpacecraftSystemModeEnum::MODE_ALWAYS_ON);
131
            }
132 2
        }
133
134
        return $this;
135 2
    }
136
137 2
    #[Override]
138
    public function setAlertState(SpacecraftAlertStateEnum $alertState): SpacecraftConfiguratorInterface
139
    {
140 1
        $this->alertStateManager->setAlertState(
141
            $this->wrapper,
142
            $alertState
143 1
        );
144
145 1
        return $this;
146 1
    }
147
148
    #[Override]
149
    public function setTorpedo(?int $torpedoTypeId = null): SpacecraftConfiguratorInterface
150 1
    {
151
        $ship = $this->wrapper->get();
152 1
        if ($ship->getMaxTorpedos() === 0) {
153
            return $this;
154 1
        }
155
156
        $ship = $this->wrapper->get();
157 2
158
        if ($torpedoTypeId !== null) {
159
            $torpedoType = $this->torpedoTypeRepository->find($torpedoTypeId);
160 2
            if ($torpedoType === null) {
161 2
                throw new RuntimeException(sprintf('torpedoTypeId %d does not exist', $torpedoTypeId));
162 2
            }
163 2
        } else {
164
            $torpedoLevel = $ship->getRump()->getTorpedoLevel();
165 2
            if ($torpedoLevel === 0) {
166
                return $this;
167
            }
168 2
169
            $torpedoTypes = $this->torpedoTypeRepository->getByLevel($torpedoLevel);
170
            if ($torpedoTypes === []) {
171 2
                return $this;
172 2
            }
173 1
            shuffle($torpedoTypes);
174
175
            $torpedoType = current($torpedoTypes);
176 1
        }
177 1
178 1
        $this->torpedoManager->changeTorpedo($this->wrapper, $ship->getMaxTorpedos(), $torpedoType);
179
180
        return $this;
181
    }
182
183
    #[Override]
184
    public function setSpacecraftName(string $name): SpacecraftConfiguratorInterface
185
    {
186
        $ship = $this->wrapper->get();
187
        $ship->setName($name);
188
189
        return $this;
190
    }
191 1
192
    #[Override]
193 1
    public function finishConfiguration(): SpacecraftWrapperInterface
194
    {
195
        $this->spacecraftRepository->save($this->wrapper->get());
196 2
197
        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...
198
    }
199
}
200