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