Passed
Pull Request — master (#1883)
by Janko
55:13 queued 27:46
created

StateIconAndTitle::getStateIconAndTitle()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 10.0021

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 34
nc 6
nop 1
dl 0
loc 52
ccs 35
cts 36
cp 0.9722
crap 10.0021
rs 7.6666
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\Module\Ship\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\Ship\ShipStateEnum;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Ship\Lib\ShipWrapperInterface;
12
13
class StateIconAndTitle
14
{
15 8
    public function __construct(
16
        private GameControllerInterface $game,
17
        private Parser $bbCodeParser
18 8
    ) {}
19
20
    /**
21
     * @return array<string>|null
22
     */
23 7
    public function getStateIconAndTitle(ShipWrapperInterface $wrapper): ?array
24
    {
25 7
        $ship = $wrapper->get();
26 7
        $state = $ship->getState();
27
28 7
        if ($state === ShipStateEnum::SHIP_STATE_REPAIR_ACTIVE) {
29 2
            $isBase = $ship->isBase();
30 2
            return ['rep2', sprintf('%s repariert die Station', $isBase ? 'Stationscrew' : 'Schiffscrew')];
31
        }
32
33 5
        if ($state === ShipStateEnum::SHIP_STATE_REPAIR_PASSIVE) {
34 2
            $isBase = $ship->isBase();
35 2
            $repairDuration = $wrapper->getRepairDuration();
36 2
            return ['rep2', sprintf('%s wird repariert (noch %d Runden)', $isBase ? 'Station' : 'Schiff', $repairDuration)];
37
        }
38
39 3
        $currentTurn = $this->game->getCurrentRound()->getTurn();
40 3
        $astroLab = $wrapper->getAstroLaboratorySystemData();
41
        if (
42 3
            $state === ShipStateEnum::SHIP_STATE_ASTRO_FINALIZING
43 3
            && $astroLab !== null
44
        ) {
45 1
            return ['map1', sprintf(
46 1
                'Schiff kartographiert (noch %d Runden)',
47 1
                $astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH - $currentTurn
48 1
            )];
49
        }
50
51 2
        $takeover = $ship->getTakeoverActive();
52
        if (
53 2
            $state === ShipStateEnum::SHIP_STATE_ACTIVE_TAKEOVER
54 2
            && $takeover !== null
55
        ) {
56 1
            $targetNamePlainText = $this->bbCodeParser->parse($takeover->getTargetShip()->getName())->getAsText();
57 1
            return ['take2', sprintf(
58 1
                'Schiff übernimmt die "%s" (noch %d Runden)',
59 1
                $targetNamePlainText,
60 1
                $wrapper->getTakeoverTicksLeft($takeover)
61 1
            )];
62
        }
63
64 1
        $takeover = $ship->getTakeoverPassive();
65 1
        if ($takeover !== null) {
66 1
            $sourceUserNamePlainText = $this->bbCodeParser->parse($takeover->getSourceShip()->getUser()->getName())->getAsText();
67 1
            return ['untake2', sprintf(
68 1
                'Schiff wird von Spieler "%s" übernommen (noch %d Runden)',
69 1
                $sourceUserNamePlainText,
70 1
                $wrapper->getTakeoverTicksLeft($takeover)
71 1
            )];
72
        }
73
74
        return null;
75
    }
76
}
77