SpacecraftCountLayerRenderer::getDisplayCount()   C
last analyzed

Complexity

Conditions 17
Paths 13

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 27.7004

Importance

Changes 0
Metric Value
cc 17
eloc 28
nc 13
nop 1
dl 0
loc 47
ccs 18
cts 27
cp 0.6667
crap 27.7004
rs 5.2166
c 0
b 0
f 0

How to fix   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\Lib\Map\VisualPanel\Layer\Render;
6
7
use Stu\Component\Spacecraft\SpacecraftRumpRoleEnum;
8
use Stu\Lib\Map\VisualPanel\Layer\Data\CellDataInterface;
9
use Stu\Lib\Map\VisualPanel\Layer\Data\SpacecraftCountData;
10
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerEnum;
11
use Stu\Lib\Map\VisualPanel\PanelAttributesInterface;
12
use Stu\Orm\Entity\Spacecraft;
13
use Stu\Orm\Repository\StarSystemRepositoryInterface;
14
15
final class SpacecraftCountLayerRenderer implements LayerRendererInterface
16
{
17 50
    public function __construct(
18
        private bool $showCloakedEverywhere,
19
        private ?Spacecraft $currentSpacecraft,
20
        private StarSystemRepositoryInterface $starSystemRepository
21 50
    ) {}
22
23
    /** @param SpacecraftCountData $data */
24 50
    #[\Override]
25
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
26
    {
27 50
        $displayCount = $this->getDisplayCount($data);
28 50
        if ($displayCount === null) {
29 18
            return '';
30
        }
31
32 38
        return sprintf(
33 38
            '<div style="%s z-index: %d;" class="centered">%s</div>',
34 38
            $panel->getFontSize(),
35 38
            PanelLayerEnum::SPACECRAFT_COUNT->value,
36 38
            $displayCount
37 38
        );
38
    }
39
40 50
    private function getDisplayCount(SpacecraftCountData $data): ?string
41
    {
42 50
        if (!$data->isEnabled()) {
43 1
            return null;
44
        }
45
46 49
        $spacecraftCount = $data->getSpacecraftCount();
47
48 49
        if ($spacecraftCount > 0) {
49 8
            return $data->isDubious() ? '!'  : (string) $spacecraftCount;
50
        }
51
52 47
        if ($data->hasCloakedShips()) {
53 40
            if ($this->showCloakedEverywhere) {
54 2
                return $data->isDubious() ? '!'  : "?";
55
            }
56
57 38
            $currentSpacecraft = $this->currentSpacecraft;
58
59 38
            if ($currentSpacecraft !== null && $currentSpacecraft->getTachyonState()) {
60 36
                $rump = $currentSpacecraft->getRump();
61
62
                if (
63 36
                    $rump->getRoleId() === SpacecraftRumpRoleEnum::SENSOR
64 36
                    || $rump->getRoleId() === SpacecraftRumpRoleEnum::BASE
65
                ) {
66
                    $spacecraftX = $this->getRelevantXCoordinate($currentSpacecraft, $data);
67
                    $spacecraftY = $this->getRelevantYCoordinate($currentSpacecraft, $data);
68
                    $dataX = $this->getRelevantDataXCoordinate($data);
69
                    $dataY = $this->getRelevantDataYCoordinate($data);
70
71
                    $distanceX = abs($dataX - $spacecraftX);
72
                    $distanceY = abs($dataY - $spacecraftY);
73
                    $range = $this->getTachyonRange($currentSpacecraft);
74
75
                    if ($distanceX <= $range && $distanceY <= $range) {
76
                        return $data->isDubious() ? '!'  : "?";
77
                    }
78
                } elseif (
79 36
                    abs($data->getPosX() - $currentSpacecraft->getPosX()) <= $this->getTachyonRange($currentSpacecraft)
80 36
                    && abs($data->getPosY() - $currentSpacecraft->getPosY()) <= $this->getTachyonRange($currentSpacecraft)
81
                ) {
82 28
                    return $data->isDubious() ? '!'  : "?";
83
                }
84
            }
85
        }
86 17
        return null;
87
    }
88
89
90 36
    private function getTachyonRange(Spacecraft $spacecraft): int
91
    {
92 36
        return $spacecraft->isStation() ? 7 : 3;
93
    }
94
95
    private function getRelevantXCoordinate(Spacecraft $spacecraft, SpacecraftCountData $data): int
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
    private function getRelevantXCoordinate(Spacecraft $spacecraft, /** @scrutinizer ignore-unused */ SpacecraftCountData $data): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        $spacecraftSystemMap = $spacecraft->getStarsystemMap();
98
99
        if ($spacecraftSystemMap !== null) {
100
            return $spacecraftSystemMap->getSystem()->getCx() ?? $spacecraft->getPosX();
101
        }
102
103
        return $spacecraft->getPosX();
104
    }
105
106
    private function getRelevantYCoordinate(Spacecraft $spacecraft, SpacecraftCountData $data): int
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
    private function getRelevantYCoordinate(Spacecraft $spacecraft, /** @scrutinizer ignore-unused */ SpacecraftCountData $data): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        $spacecraftSystemMap = $spacecraft->getStarsystemMap();
109
110
        if ($spacecraftSystemMap !== null) {
111
            return $spacecraftSystemMap->getSystem()->getCy() ?? $spacecraft->getPosY();
112
        }
113
114
        return $spacecraft->getPosY();
115
    }
116
117
    private function getRelevantDataXCoordinate(SpacecraftCountData $data): int
118
    {
119
        $dataSystemId = $data->getSystemId();
120
        if ($dataSystemId !== null) {
121
            $dataSystem = $this->starSystemRepository->find($dataSystemId);
122
123
            if ($dataSystem !== null) {
124
                $cx = $dataSystem->getCx();
125
                if ($cx !== null) {
126
                    return $cx;
127
                }
128
            }
129
        }
130
131
        $posX = $data->getPosX();
132
133
        return $posX;
134
    }
135
    private function getRelevantDataYCoordinate(SpacecraftCountData $data): int
136
    {
137
        $dataSystemId = $data->getSystemId();
138
139
        if ($dataSystemId !== null) {
140
            $dataSystem = $this->starSystemRepository->find($dataSystemId);
141
            if ($dataSystem !== null) {
142
                $cy = $dataSystem->getCy();
143
                if ($cy !== null) {
144
                    return $cy;
145
                }
146
            }
147
        }
148
149
        return $data->getPosY();
150
    }
151
}
152