Passed
Push — master ( 91454b...2408fc )
by Nico
22:07 queued 10:43
created

ModuleRecycling::retrieveSomeModules()   B

Complexity

Conditions 11
Paths 61

Size

Total Lines 69
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 34
c 1
b 0
f 0
nc 61
nop 4
dl 0
loc 69
ccs 36
cts 36
cp 1
crap 11
rs 7.3166

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\ModuleInterface;
13
use Stu\Orm\Entity\SpacecraftInterface;
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
        SpacecraftInterface $spacecraft,
25
        EntityWithStorageInterface $entity,
26
        InformationInterface $information,
27
        int $recyclingChance = 50
28
    ): void {
29
30
        /** @var array<int, array{0: ModuleInterface, 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 1
            if ($module !== null) {
45 1
                if (!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
51 1
                    $recycledModuleChances[$module->getId()] = [$module, $amount, $chance];
52
                }
53
            }
54
        }
55
56
        // recycle buildplan modules
57 3
        foreach ($buildplanModules as $buildplanModule) {
58
59 3
            $module = $buildplanModule->getModule();
60 3
            if (!array_key_exists($module->getId(), $recycledModuleChances)) {
61 2
                $moduleCount = $buildplanModule->getModuleCount();
62
63 2
                $recycledModuleChances[$module->getId()] = [
64 2
                    $module,
65 2
                    $this->stuRandom->rand(1, $moduleCount, true, (int)ceil($moduleCount / 2)),
66 2
                    intdiv($recyclingChance, 2)
67 2
                ];
68
            }
69
        }
70
71 3
        $maxStorage = $entity->getMaxStorage();
72
73 3
        foreach ($recycledModuleChances as [$module, $amount, $recyclingChance]) {
74
75 3
            if ($entity->getStorageSum() >= $maxStorage) {
76 1
                $information->addInformation('Kein Lagerraum frei um Module zu recyclen!');
77 1
                break;
78
            }
79
80 2
            if ($this->stuRandom->rand(1, 100) > $recyclingChance) {
81 1
                continue;
82
            }
83
84 2
            $this->storageManager->upperStorage(
85 2
                $entity,
86 2
                $module->getCommodity(),
87 2
                $amount
88 2
            );
89
90 2
            $information->addInformationf('Folgendes Modul konnte recycelt werden: %s, Anzahl: %d', $module->getName(), $amount);
91
        }
92
    }
93
}
94