Passed
Push — dev ( 83c248...96a6f5 )
by Janko
10:53
created

AbstractWeaponPhase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

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