Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

StateIconAndTitle::getStateIconAndTitle()   C

Complexity

Conditions 17
Paths 19

Size

Total Lines 75
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 21.7863

Importance

Changes 0
Metric Value
cc 17
eloc 49
nc 19
nop 1
dl 0
loc 75
ccs 38
cts 51
cp 0.7451
crap 21.7863
rs 5.2166
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::SHIP_STATE_RETROFIT) {
32
            return ['buttons/konstr1', 'Schiff wird umgerüstet'];
33
        }
34
35 11
        if ($state === SpacecraftStateEnum::SHIP_STATE_REPAIR_ACTIVE) {
36 2
            $isStation = $spacecraft->isStation();
37 2
            return ['buttons/rep2', sprintf('%s repariert die Station', $isStation ? 'Stationscrew' : 'Schiffscrew')];
38
        }
39
40 9
        if ($state === SpacecraftStateEnum::SHIP_STATE_REPAIR_PASSIVE) {
41 2
            $isStation = $spacecraft->isStation();
42 2
            $repairDuration = $wrapper->getRepairDuration();
43 2
            return ['buttons/rep2', sprintf('%s wird repariert (noch %d Runden)', $isStation ? 'Station' : 'Schiff', $repairDuration)];
44
        }
45
46 7
        $astroLab = $wrapper instanceof ShipWrapperInterface ? $wrapper->getAstroLaboratorySystemData() : null;
47
        if (
48 7
            $state === SpacecraftStateEnum::SHIP_STATE_ASTRO_FINALIZING
49 7
            && $astroLab !== null
50
        ) {
51 1
            return ['buttons/map1', sprintf(
52 1
                'Schiff kartographiert (noch %d Runden)',
53 1
                $astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH - $this->game->getCurrentRound()->getTurn()
54 1
            )];
55
        }
56
57 6
        $takeover = $spacecraft->getTakeoverActive();
58
        if (
59 6
            $state === SpacecraftStateEnum::SHIP_STATE_ACTIVE_TAKEOVER
60 6
            && $takeover !== null
61
        ) {
62 1
            $targetNamePlainText = $this->bbCodeParser->parse($takeover->getTargetSpacecraft()->getName())->getAsText();
63 1
            return ['buttons/take2', sprintf(
64 1
                'Schiff übernimmt die "%s" (noch %d Runden)',
65 1
                $targetNamePlainText,
66 1
                $wrapper->getTakeoverTicksLeft($takeover)
67 1
            )];
68
        }
69
70 5
        $takeover = $spacecraft->getTakeoverPassive();
71 5
        if ($takeover !== null) {
72 1
            $sourceUserNamePlainText = $this->bbCodeParser->parse($takeover->getSourceSpacecraft()->getUser()->getName())->getAsText();
73 1
            return ['buttons/untake2', sprintf(
74 1
                'Schiff wird von Spieler "%s" übernommen (noch %d Runden)',
75 1
                $sourceUserNamePlainText,
76 1
                $wrapper->getTakeoverTicksLeft($takeover)
77 1
            )];
78
        }
79
80
        if (
81 4
            $state === SpacecraftStateEnum::SHIP_STATE_GATHER_RESOURCES
82 4
            && $spacecraft instanceof ShipInterface
83
        ) {
84
            $miningqueue = $spacecraft->getMiningQueue();
85
            $module = $spacecraft->getShipSystem(SpacecraftSystemTypeEnum::SYSTEM_BUSSARD_COLLECTOR)->getModule();
86
            $gathercount = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $gathercount is dead and can be removed.
Loading history...
87
            if ($miningqueue !== null) {
88
                $locationmining = $miningqueue->getLocationMining();
89
                if ($module !== null) {
90
                    $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...
91
                    return [sprintf('commodities/%s', $locationmining->getCommodity()->getId()), sprintf(
92
                        'Schiff sammelt Ressourcen (~%d %s/Tick)',
93
                        $gathercount,
94
                        $locationmining->getCommodity()->getName()
95
                    )];
96
                }
97
            }
98
        }
99
100 4
        return null;
101
    }
102
}
103