ModuleRecycling::retrieveSomeModules()   C
last analyzed

Complexity

Conditions 13
Paths 145

Size

Total Lines 76
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 42
nc 145
nop 4
dl 0
loc 76
ccs 42
cts 42
cp 1
crap 13
rs 6.2416
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft\Module;
6
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Lib\Transfer\EntityWithStorageInterface;
9
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
10
use Stu\Module\Control\StuRandom;
11
use Stu\Orm\Entity\Module;
12
use Stu\Orm\Entity\Spacecraft;
13
14
class ModuleRecycling implements ModuleRecyclingInterface
15
{
16 5
    public function __construct(
17
        private StorageManagerInterface $storageManager,
18
        private StuRandom $stuRandom
19 5
    ) {}
20
21 4
    #[\Override]
22
    public function retrieveSomeModules(
23
        Spacecraft $spacecraft,
24
        EntityWithStorageInterface $entity,
25
        InformationInterface $information,
26
        int $recyclingChance = 50
27
    ): void {
28
29
        /** @var array<int, array{0: Module, 1: int, 2: int}> */
30 4
        $recycledModuleChances = [];
31
32 4
        $buildplan = $spacecraft->getBuildplan();
33 4
        if ($buildplan === null) {
34 1
            return;
35
        }
36
37 3
        $buildplanModules = $buildplan->getModules();
38
39
        // recycle installed systems based on health
40 3
        foreach ($spacecraft->getSystems() as $system) {
41
42 1
            $module = $system->getModule();
43
            if (
44 1
                $module !== null
45 1
                && !array_key_exists($module->getId(), $recycledModuleChances)
46
            ) {
47 1
                $buildplanModule = $buildplanModules->get($module->getId());
48 1
                $amount = $buildplanModule === null ? 1 : $buildplanModule->getModuleCount();
49 1
                $chance = (int)ceil($recyclingChance * $system->getStatus() / 100);
50 1
                $recycledModuleChances[$module->getId()] = [$module, $amount, $chance];
51
            }
52
        }
53
54
        // recycle buildplan modules
55 3
        foreach ($buildplanModules as $buildplanModule) {
56
57 3
            $module = $buildplanModule->getModule();
58 3
            if (!array_key_exists($module->getId(), $recycledModuleChances)) {
59 2
                $moduleCount = $buildplanModule->getModuleCount();
60
61 2
                $recycledModuleChances[$module->getId()] = [
62 2
                    $module,
63 2
                    $this->stuRandom->rand(1, $moduleCount, true, (int)ceil($moduleCount / 2)),
64 2
                    intdiv($recyclingChance, 2)
65 2
                ];
66
            }
67
        }
68
69 3
        $maxStorage = $entity->getMaxStorage();
70 3
        $recycledModules = [];
71 3
        foreach ($recycledModuleChances as [$module, $amount, $recyclingChance]) {
72
73 3
            if ($entity->getStorageSum() >= $maxStorage) {
74 1
                $information->addInformation('Kein Lagerraum frei um Module zu recyclen!');
75 1
                break;
76
            }
77
78 2
            if ($this->stuRandom->rand(1, 100) > $recyclingChance) {
79 1
                continue;
80
            }
81
82 2
            $this->storageManager->upperStorage(
83 2
                $entity,
84 2
                $module->getCommodity(),
85 2
                $amount
86 2
            );
87
88 2
            $recycledModules[] = ['module' => $module, 'amount' => $amount];
89
        }
90 3
        if (count($recycledModules) > 0) {
91 2
            $information->addInformation("\nFolgende Module konnten recycelt werden:");
92 2
            foreach ($recycledModules as $recycled) {
93 2
                $information->addInformationf('%s, Anzahl: %d', $recycled['module']->getName(), $recycled['amount']);
94
            }
95
        } else {
96 1
            $information->addInformation("\nEs konnten keine Module recycelt werden.");
97
        }
98
    }
99
}
100