Passed
Pull Request — master (#2028)
by Nico
23:40 queued 11:07
created

SpacecraftCountLayerRenderer::getDisplayCount()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 16
nc 9
nop 1
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 12
rs 6.9666
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 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\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\SpacecraftInterface;
13
14
final class SpacecraftCountLayerRenderer implements LayerRendererInterface
15
{
16 48
    public function __construct(private bool $showCloakedEverywhere, private ?SpacecraftInterface $currentSpacecraft) {}
17
18
    /** @param SpacecraftCountData $data */
19 48
    #[Override]
20
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
21
    {
22 48
        $displayCount = $this->getDisplayCount($data);
23 48
        if ($displayCount === null) {
24 16
            return '';
25
        }
26
27 36
        return sprintf(
28 36
            '<div style="%s z-index: %d;" class="centered">%s</div>',
29 36
            $panel->getFontSize(),
30 36
            PanelLayerEnum::SPACECRAFT_COUNT->value,
31 36
            $displayCount
32 36
        );
33
    }
34
35 48
    private function getDisplayCount(SpacecraftCountData $data): ?string
36
    {
37 48
        if (!$data->isEnabled()) {
38 1
            return null;
39
        }
40
41 47
        $spacecraftCount = $data->getSpacecraftCount();
42 47
        if ($spacecraftCount > 0) {
43 6
            return $data->isDubious() ? '!'  : (string) $spacecraftCount;
44
        }
45 45
        if ($data->hasCloakedShips()) {
46 40
            if ($this->showCloakedEverywhere) {
47 2
                return $data->isDubious() ? '!'  : "?";
48
            }
49
50 38
            $currentSpacecraft = $this->currentSpacecraft;
51
52
            if (
53 38
                $currentSpacecraft !== null
54 38
                && $currentSpacecraft->getTachyonState()
55 38
                && abs($data->getPosX() - $currentSpacecraft->getPosX()) <= $this->getTachyonRange($currentSpacecraft)
56 38
                && abs($data->getPosY() - $currentSpacecraft->getPosY()) <= $this->getTachyonRange($currentSpacecraft)
57
            ) {
58 28
                return $data->isDubious() ? '!'  : "?";
59
            }
60
        }
61 15
        return null;
62
    }
63
64 36
    private function getTachyonRange(SpacecraftInterface $spacecraft): int
65
    {
66 36
        return $spacecraft->isStation() ? 7 : 3;
67
    }
68
}
69