Passed
Push — master ( 95eee2...124208 )
by Nico
15:21 queued 07:35
created

YRow::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Starmap\Lib;
6
7
use RuntimeException;
8
use Stu\Component\Map\EncodedMapInterface;
9
use Stu\Lib\Map\VisualPanel\SystemCellData;
10
use Stu\Module\Admin\View\Map\EditSection\MapItem;
11
use Stu\Orm\Entity\LayerInterface;
12
use Stu\Orm\Entity\StarSystemInterface;
13
use Stu\Orm\Entity\StarSystemMapInterface;
14
use Stu\Orm\Repository\MapRepositoryInterface;
15
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
16
17
class YRow
18
{
19
    protected ?LayerInterface $layer;
20
21
    protected int $row;
22
23
    protected int $minx;
24
25
    protected int $maxx;
26
27
    protected int|StarSystemInterface $system;
28
29
    /** @var null|array<MapItem>|array<SystemCellData> */
30
    protected $fields;
31
32
    private MapRepositoryInterface $mapRepository;
33
34
    private StarSystemMapRepositoryInterface $systemMapRepository;
35
36
    private EncodedMapInterface $encodedMap;
37
38 2
    public function __construct(
39
        MapRepositoryInterface $mapRepository,
40
        StarSystemMapRepositoryInterface $systemMapRepository,
41
        EncodedMapInterface $encodedMap,
42
        ?LayerInterface $layer,
43
        int $cury,
44
        int $minx,
45
        int $maxx,
46
        int|StarSystemInterface $system
47
    ) {
48 2
        $this->layer = $layer;
49 2
        $this->row = $cury;
50 2
        $this->minx = $minx;
51 2
        $this->maxx = $maxx;
52 2
        $this->system = $system;
53 2
        $this->mapRepository = $mapRepository;
54 2
        $this->systemMapRepository = $systemMapRepository;
55 2
        $this->encodedMap = $encodedMap;
56
    }
57
58
    /**
59
     * @return array<MapItem>|array<SystemCellData>
60
     */
61
    public function getFields(): array
62
    {
63
        if ($this->fields === null) {
64
            $this->fields = [];
65
            for ($i = $this->minx; $i <= $this->maxx; $i++) {
66
                $layer = $this->layer;
67
                if ($layer === null) {
68
                    throw new RuntimeException('this should not happen');
69
                }
70
71
                $map = $this->mapRepository->getByCoordinates(
72
                    $layer->getId(),
73
                    $i,
74
                    $this->row
75
                );
76
77
                if ($map !== null) {
78
                    $this->fields[] = new MapItem(
79
                        $this->encodedMap,
80
                        $map
81
                    );
82
                }
83
            }
84
        }
85
        return $this->fields;
86
    }
87
88
    /**
89
     * @return array<MapItem>|array<SystemCellData>
90
     */
91
    public function getSystemFields(): array
92
    {
93
        if ($this->fields === null) {
94
            $this->fields = [];
95
96
            if ($this->system instanceof StarSystemInterface) {
97
                $this->fields = array_map(
98
                    fn (StarSystemMapInterface $systemMap) => $this->mapSystemMapToSystemCellData($systemMap),
99
                    array_filter(
100
                        $this->system->getFields()->toArray(),
101
                        fn (StarSystemMapInterface $systemMap) => $systemMap->getSy() === $this->row
102
                    )
103
                );
104
            } else {
105
106
                for ($i = $this->minx; $i <= $this->maxx; $i++) {
107
                    $systemMap = $this->systemMapRepository->getByCoordinates(
108
                        $this->system,
109
                        $i,
110
                        $this->row
111
                    );
112
                    if ($systemMap !== null) {
113
                        $this->fields[] = $this->mapSystemMapToSystemCellData($systemMap);
114
                    }
115
                }
116
            }
117
        }
118
        return $this->fields;
119
    }
120
121
    private function mapSystemMapToSystemCellData(StarSystemMapInterface $systemMap): SystemCellData
122
    {
123
        return new SystemCellData(
124
            $systemMap->getSx(),
125
            $systemMap->getSy(),
126
            $systemMap->getFieldId(),
127
            false,
128
            null,
129
            null
130
        );
131
    }
132
133
    /**
134
     * @return int
135
     */
136
    public function getRow()
137
    {
138
        return $this->row;
139
    }
140
}
141