Passed
Push — dev ( f1dc2a...210bba )
by Nico
08:46
created

ModuleSelector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 2
rs 9.9332
c 1
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
declare(strict_types=1);
4
5
namespace Stu\Lib\ModuleScreen;
6
7
use InvalidArgumentException;
8
use Stu\Component\Ship\ShipModuleTypeEnum;
9
use Stu\Module\Ship\Lib\ModuleValueCalculator;
10
use Stu\Module\Ship\Lib\ModuleValueCalculatorInterface;
11
use Stu\Module\ShipModule\ModuleTypeDescriptionMapper;
12
use Stu\Module\Tal\TalPageInterface;
13
use Stu\Orm\Entity\ColonyInterface;
14
use Stu\Orm\Entity\ShipBuildplanInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Entity\ShipRumpInterface;
17
use Stu\Orm\Entity\ShipRumpModuleLevelInterface;
18
use Stu\Orm\Repository\ModuleRepositoryInterface;
19
use Stu\Orm\Repository\ShipRumpModuleLevelRepositoryInterface;
20
use Stu\Orm\Repository\WeaponShieldRepositoryInterface;
21
22
class ModuleSelector implements ModuleSelectorInterface
23
{
24
    private const MACRO = 'html/modulescreen.xhtml/moduleselector';
25
    private const TEMPLATE = 'html/ajaxempty.xhtml';
26
27
    /** @var ModuleSelectorWrapperInterface[] */
28
    private $moduleSelectorWrappers;
29
    private int $moduleType;
30
    private ShipRumpInterface $rump;
31
    private int $userId;
32
    private ?ColonyInterface $colony;
33
    private ?ShipInterface $station;
34
    private ?ShipBuildplanInterface $buildplan;
35
    private ?WeaponShieldRepositoryInterface $weaponshield;
36
37
    private ModuleRepositoryInterface $moduleRepository;
38
39
    private ShipRumpModuleLevelRepositoryInterface $shipRumpModuleLevelRepository;
40
41
    private TalPageInterface $talPage;
42
43
    public function __construct(
44
        ModuleRepositoryInterface $moduleRepository,
45
        ?WeaponShieldRepositoryInterface $weaponshield,
46
        ShipRumpModuleLevelRepositoryInterface $shipRumpModuleLevelRepository,
47
        TalPageInterface $talPage,
48
        int $moduleType,
49
        ?ColonyInterface $colony,
50
        ?ShipInterface $station,
51
        ShipRumpInterface $rump,
52
        int $userId,
53
        ?ShipBuildplanInterface $buildplan = null
54
    ) {
55
        $this->weaponshield = $weaponshield;
56
        $this->moduleType = $moduleType;
57
        $this->rump = $rump;
58
        $this->userId = $userId;
59
        $this->colony = $colony;
60
        $this->station = $station;
61
        $this->buildplan = $buildplan;
62
        $this->moduleRepository = $moduleRepository;
63
        $this->shipRumpModuleLevelRepository = $shipRumpModuleLevelRepository;
64
        $this->talPage = $talPage;
65
    }
66
67
    public function allowMultiple(): bool
68
    {
69
        return false;
70
    }
71
72
    public function getMacro(): string
73
    {
74
        return self::MACRO;
75
    }
76
77
    public function render(): string
78
    {
79
        $this->talPage->setTemplate(self::TEMPLATE);
80
        $this->talPage->setVar('THIS', $this);
81
        return $this->talPage->parse();
82
    }
83
84
    public function getModuleType(): int
85
    {
86
        return $this->moduleType;
87
    }
88
89
    public function allowEmptySlot(): bool
90
    {
91
        return $this->getModuleLevels()->{'getModuleMandatory' . $this->getModuleType()}() == ShipModuleTypeEnum::MODULE_OPTIONAL;
92
    }
93
94
    public function getModuleDescription(): string
95
    {
96
        return ModuleTypeDescriptionMapper::getDescription($this->getModuleType());
97
    }
98
99
    public function getUserId(): int
100
    {
101
        return $this->userId;
102
    }
103
104
    public function getRump(): ShipRumpInterface
105
    {
106
        return $this->rump;
107
    }
108
109
    public function getFactionbyWeapon($module): ?WeaponShieldRepositoryInterface
110
    {
111
        return $this->weaponshield->getFactionByModule($module);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->weaponshie...actionByModule($module) could return the type Stu\Orm\Entity\WeaponShieldInterface which is incompatible with the type-hinted return Stu\Orm\Repository\Weapo...epositoryInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
The method getFactionByModule() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        return $this->weaponshield->/** @scrutinizer ignore-call */ getFactionByModule($module);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
    }
113
114
    private function getShipRumpRoleId(): int
115
    {
116
        $shipRumpRole = $this->getRump()->getShipRumpRole();
117
118
        if ($shipRumpRole === null) {
119
            throw new InvalidArgumentException('invalid rump without rump role');
120
        }
121
122
        return $shipRumpRole->getId();
123
    }
124
125
    public function getAvailableModules(): array
126
    {
127
        if ($this->moduleSelectorWrappers === null) {
128
            $this->moduleSelectorWrappers = [];
129
            $modules = [];
130
            if ($this->getModuleType() == ShipModuleTypeEnum::MODULE_TYPE_SPECIAL) {
131
                if ($this->getColony() !== null) {
132
                    $modules = $this->moduleRepository->getBySpecialTypeColonyAndRump(
133
                        $this->getColony()->getId(),
134
                        $this->getModuleType(),
135
                        $this->getRump()->getId(),
136
                        $this->getShipRumpRoleId()
137
                    );
138
                } elseif ($this->station !== null) {
139
                    $modules = $this->moduleRepository->getBySpecialTypeShipAndRump(
140
                        $this->station->getId(),
141
                        $this->getModuleType(),
142
                        $this->getRump()->getId(),
143
                        $this->getShipRumpRoleId()
144
                    );
145
                }
146
            } else {
147
                if ($this->getColony() !== null) {
148
                    $mod_level = $this->shipRumpModuleLevelRepository->getByShipRump(
149
                        $this->getRump()->getId()
150
                    );
151
152
                    $min_level = $mod_level->{'getModuleLevel' . $this->getModuleType() . 'Min'}();
153
                    $max_level = $mod_level->{'getModuleLevel' . $this->getModuleType() . 'Max'}();
154
155
                    $modules = $this->moduleRepository->getByTypeColonyAndLevel(
156
                        $this->getColony()->getId(),
157
                        $this->getModuleType(),
158
                        $this->getShipRumpRoleId(),
159
                        range($min_level, $max_level)
160
                    );
161
                }
162
            }
163
            foreach ($modules as $obj) {
164
                $this->moduleSelectorWrappers[$obj->getId()] = new ModuleSelectorWrapper($obj, $this->getBuildplan());
165
            }
166
        }
167
        return $this->moduleSelectorWrappers;
168
    }
169
170
    public function hasModuleSelected(): ModuleSelectWrapper
171
    {
172
        return new ModuleSelectWrapper($this->buildplan);
173
    }
174
175
    public function getColony(): ?ColonyInterface
176
    {
177
        return $this->colony;
178
    }
179
180
    public function getBuildplan(): ?ShipBuildplanInterface
181
    {
182
        return $this->buildplan;
183
    }
184
185
    public function getModuleLevelClass(ShipRumpInterface $rump, ModuleSelectorWrapperInterface $module): string
186
    {
187
        $moduleLevels = $this->getModuleLevels();
188
189
        if ($moduleLevels->{'getModuleLevel' . $module->getModule()->getType()}() > $module->getModule()->getLevel()) {
190
            return 'module_positive';
191
        }
192
        if ($moduleLevels->{'getModuleLevel' . $module->getModule()->getType()}() < $module->getModule()->getLevel()) {
193
            return 'module_negative';
194
        }
195
        return '';
196
    }
197
198
    public function getModuleValueCalculator(): ModuleValueCalculatorInterface
199
    {
200
        return new ModuleValueCalculator();
201
    }
202
203
    public function getModuleLevels(): ?ShipRumpModuleLevelInterface
204
    {
205
        return $this->shipRumpModuleLevelRepository->getByShipRump($this->rump->getId());
206
    }
207
}