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