Passed
Push — master ( 2978a7...f80fed )
by Nico
21:04 queued 09:57
created

SubspaceLayerRenderer::getSubspaceCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\SubspaceData;
10
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerEnum;
11
use Stu\Lib\Map\VisualPanel\PanelAttributesInterface;
12
13
final class SubspaceLayerRenderer implements LayerRendererInterface
14
{
15
    /** @param SubspaceData $data */
16 4
    #[Override]
17
    public function render(CellDataInterface $data, PanelAttributesInterface $panel): string
18
    {
19 4
        if ($data->isDisabled()) {
0 ignored issues
show
Bug introduced by
The method isDisabled() does not exist on Stu\Lib\Map\VisualPanel\...\Data\CellDataInterface. It seems like you code against a sub-type of Stu\Lib\Map\VisualPanel\...\Data\CellDataInterface such as Stu\Lib\Map\VisualPanel\Layer\Data\SubspaceData. ( Ignorable by Annotation )

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

19
        if ($data->/** @scrutinizer ignore-call */ isDisabled()) {
Loading history...
20 1
            return '';
21
        }
22
23 3
        $subspaceCode = $this->getSubspaceCode($data);
24 3
        if ($subspaceCode === null) {
25 1
            return '';
26
        }
27
28 2
        return sprintf(
29 2
            '<img src="/assets/subspace/generated/%s.png" class="visualPanelLayer"
30 2
                style="z-index: %d; %s" />',
31 2
            $subspaceCode,
32 2
            PanelLayerEnum::SUBSPACE_SIGNATURES->value,
33 2
            $panel->getHeightAndWidth()
34 2
        );
35
    }
36
37 3
    private function getSubspaceCode(SubspaceData $data): ?string
38
    {
39 3
        if (!$this->isSubspaceCodeAvailable($data)) {
40 1
            return null;
41
        }
42
43 2
        return sprintf(
44 2
            '%d%d%d%d',
45 2
            $this->getCode($data->getDirection1Count()),
46 2
            $this->getCode($data->getDirection2Count()),
47 2
            $this->getCode($data->getDirection3Count()),
48 2
            $this->getCode($data->getDirection4Count())
49 2
        );
50
    }
51
52 3
    private function isSubspaceCodeAvailable(SubspaceData $data): bool
53
    {
54 3
        return $data->getDirection1Count() > 0
55 3
            || $data->getDirection2Count() > 0
56 3
            || $data->getDirection3Count() > 0
57 3
            || $data->getDirection4Count() > 0;
58
    }
59
60 2
    private function getCode(int $shipCount): int
61
    {
62 2
        if ($shipCount == 0) {
63
            return 0;
64
        }
65 2
        if ($shipCount == 1) {
66 1
            return 1;
67
        }
68 2
        if ($shipCount < 6) {
69 2
            return 2;
70
        }
71 2
        if ($shipCount < 11) {
72 2
            return 3;
73
        }
74 2
        if ($shipCount < 21) {
75 2
            return 4;
76
        }
77
78 1
        return 5;
79
    }
80
}
81