Passed
Push — master ( 767dcd...21977d )
by Nico
22:25 queued 10:21
created

StateIconAndTitle::getStateIconAndTitle()   D

Complexity

Conditions 18
Paths 19

Size

Total Lines 79
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 21.5544

Importance

Changes 0
Metric Value
cc 18
eloc 51
nc 19
nop 1
dl 0
loc 79
ccs 42
cts 54
cp 0.7778
crap 21.5544
rs 4.8666
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\Module\Spacecraft\Lib\Ui;
6
7
use JBBCode\Parser;
8
use Stu\Component\Ship\AstronomicalMappingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\AstronomicalMappingEnum 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...
9
use Stu\Component\Spacecraft\SpacecraftStateEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
16
class StateIconAndTitle
17
{
18 8
    public function __construct(
19
        private GameControllerInterface $game,
20
        private Parser $bbCodeParser
21 8
    ) {}
22
23
    /**
24
     * @return array<string>|null
25
     */
26 11
    public function getStateIconAndTitle(SpacecraftWrapperInterface $wrapper): ?array
27
    {
28 11
        $spacecraft = $wrapper->get();
29 11
        $state = $spacecraft->getState();
30
31 11
        if ($state === SpacecraftStateEnum::RETROFIT) {
32
            return ['buttons/konstr1', 'Schiff wird umgerüstet'];
33
        }
34
35 11
        if ($state === SpacecraftStateEnum::REPAIR_ACTIVE) {
36 2
            $isStation = $spacecraft->isStation();
37 2
            return ['buttons/rep2', sprintf(
38 2
                '%s repariert %s',
39 2
                $isStation ? 'Stationscrew' : 'Schiffscrew',
40 2
                $isStation ? 'die Station' : 'das Schiff',
41 2
            )];
42
        }
43
44 9
        if ($state === SpacecraftStateEnum::REPAIR_PASSIVE) {
45 2
            $isStation = $spacecraft->isStation();
46 2
            $repairDuration = $wrapper->getRepairDuration();
47 2
            return ['buttons/rep2', sprintf('%s wird repariert (noch %d Runden)', $isStation ? 'Station' : 'Schiff', $repairDuration)];
48
        }
49
50 7
        $astroLab = $wrapper instanceof ShipWrapperInterface ? $wrapper->getAstroLaboratorySystemData() : null;
51
        if (
52 7
            $state === SpacecraftStateEnum::ASTRO_FINALIZING
53 7
            && $astroLab !== null
54
        ) {
55 1
            return ['buttons/map1', sprintf(
56 1
                'Schiff kartographiert (noch %d Runden)',
57 1
                $astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH - $this->game->getCurrentRound()->getTurn()
58 1
            )];
59
        }
60
61 6
        $takeover = $spacecraft->getTakeoverActive();
62
        if (
63 6
            $state === SpacecraftStateEnum::ACTIVE_TAKEOVER
64 6
            && $takeover !== null
65
        ) {
66 1
            $targetNamePlainText = $this->bbCodeParser->parse($takeover->getTargetSpacecraft()->getName())->getAsText();
67 1
            return ['buttons/take2', sprintf(
68 1
                'Schiff übernimmt die "%s" (noch %d Runden)',
69 1
                $targetNamePlainText,
70 1
                $wrapper->getTakeoverTicksLeft($takeover)
71 1
            )];
72
        }
73
74 5
        $takeover = $spacecraft->getTakeoverPassive();
75 5
        if ($takeover !== null) {
76 1
            $sourceUserNamePlainText = $this->bbCodeParser->parse($takeover->getSourceSpacecraft()->getUser()->getName())->getAsText();
77 1
            return ['buttons/untake2', sprintf(
78 1
                'Schiff wird von Spieler "%s" übernommen (noch %d Runden)',
79 1
                $sourceUserNamePlainText,
80 1
                $wrapper->getTakeoverTicksLeft($takeover)
81 1
            )];
82
        }
83
84
        if (
85 4
            $state === SpacecraftStateEnum::GATHER_RESOURCES
86 4
            && $spacecraft instanceof ShipInterface
87
        ) {
88
            $miningqueue = $spacecraft->getMiningQueue();
89
            $module = $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::BUSSARD_COLLECTOR)->getModule();
90
91
            if ($miningqueue !== null) {
92
                $locationmining = $miningqueue->getLocationMining();
93
                if ($module !== null) {
94
                    $gathercount = $module->getFactionId() == null ? 100 : 200;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $module->getFactionId() of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
95
                    return [sprintf('commodities/%s', $locationmining->getCommodity()->getId()), sprintf(
96
                        'Schiff sammelt Ressourcen (~%d %s/Tick)',
97
                        $gathercount,
98
                        $locationmining->getCommodity()->getName()
99
                    )];
100
                }
101
            }
102
        }
103
104 4
        return null;
105
    }
106
}
107