Passed
Push — dev ( 3fd410...aa33df )
by Janko
13:32
created

AbstractWeaponPhase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 9
dl 0
loc 12
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
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Battle\Weapon;
6
7
use RuntimeException;
8
use Stu\Component\Building\BuildingManagerInterface;
9
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
10
use Stu\Lib\Information\InformationInterface;
11
use Stu\Lib\Map\FieldTypeEffectEnum;
12
use Stu\Module\Colony\Lib\Damage\ApplyBuildingDamageInterface;
13
use Stu\Module\Control\StuRandom;
14
use Stu\Module\History\Lib\EntryCreatorInterface;
15
use Stu\Module\Logging\LoggerUtilFactoryInterface;
16
use Stu\Module\Logging\LoggerUtilInterface;
17
use Stu\Module\Spacecraft\Lib\Battle\Provider\AttackerInterface;
18
use Stu\Module\Spacecraft\Lib\Damage\ApplyDamageInterface;
19
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestroyerInterface;
20
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestructionCauseEnum;
21
use Stu\Module\Spacecraft\Lib\Destruction\SpacecraftDestructionInterface;
22
use Stu\Module\Spacecraft\Lib\Message\MessageFactoryInterface;
23
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
24
use Stu\Orm\Entity\ModuleInterface;
25
use Stu\Orm\Entity\SpacecraftInterface;
26
use Stu\Orm\Entity\UserInterface;
27
use Stu\Orm\Repository\UserRepositoryInterface;
28
29
abstract class AbstractWeaponPhase
30
{
31
    protected LoggerUtilInterface $loggerUtil;
32
33 3
    public function __construct(
34
        private UserRepositoryInterface $userRepository,
35
        protected EntryCreatorInterface $entryCreator,
36
        protected ApplyDamageInterface $applyDamage,
37
        protected ApplyBuildingDamageInterface $applyBuildingDamage,
38
        protected BuildingManagerInterface $buildingManager,
39
        protected StuRandom $stuRandom,
40
        protected MessageFactoryInterface $messageFactory,
41
        private SpacecraftDestructionInterface $spacecraftDestruction,
42
        LoggerUtilFactoryInterface $loggerUtilFactory
43
    ) {
44 3
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
45
    }
46
47 1
    protected function checkForSpacecraftDestruction(
48
        SpacecraftDestroyerInterface $attacker,
49
        SpacecraftWrapperInterface $targetWrapper,
50
        SpacecraftDestructionCauseEnum $destructionCause,
51
        InformationInterface $message
52
    ): void {
53
54 1
        if (!$targetWrapper->get()->isDestroyed()) {
55
            return;
56
        }
57
58 1
        $this->spacecraftDestruction->destroy(
59 1
            $attacker,
60 1
            $targetWrapper,
61 1
            $destructionCause,
62 1
            $message
63 1
        );
64
    }
65
66 1
    protected function getHitChance(AttackerInterface $attacker): int
67
    {
68 1
        $hitChance = $attacker->getHitChance();
69
70 1
        return
71 1
            $attacker->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::HIT_CHANCE_INTERFERENCE)
72
            ? (int)ceil($hitChance / 100 * $this->stuRandom->rand(15, 60, true, 30))
73 1
            : $hitChance;
74
    }
75
76 1
    protected function getEvadeChance(SpacecraftWrapperInterface $wrapper): int
77
    {
78 1
        if (!$wrapper->get()->hasComputer()) {
79
            return 0;
80
        }
81
82 1
        $evadeChance = $wrapper->getComputerSystemDataMandatory()->getEvadeChance();
83
84 1
        if ($wrapper->get()->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::EVADE_CHANCE_INTERFERENCE)) {
85
            $malus = (int)abs($evadeChance / 100 * $this->stuRandom->rand(40, 85, true, 30));
86
87
            return $evadeChance - $malus;
88
        }
89
90 1
        return $evadeChance;
91
    }
92
93 1
    protected function getModule(SpacecraftInterface $ship, SpacecraftModuleTypeEnum $moduleType): ?ModuleInterface
94
    {
95 1
        $buildplan = $ship->getBuildplan();
96 1
        if ($buildplan === null) {
97 1
            return null;
98
        }
99
100
        $modules = $buildplan->getModulesByType($moduleType);
101
        if ($modules->isEmpty()) {
102
            return null;
103
        }
104
105
        return $modules->first();
106
    }
107
108 1
    protected function getUser(int $userId): UserInterface
109
    {
110 1
        $user = $this->userRepository->find($userId);
111 1
        if ($user === null) {
112
            throw new RuntimeException('this should not happen');
113
        }
114
115 1
        return $user;
116
    }
117
}
118