Completed
Push — master ( 739f04...c64576 )
by Peter
01:52
created

DetailedMatch::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 15
crap 1

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
namespace PtrTn\Battlerite\Dto;
4
5
use DateTime;
6
use PtrTn\Battlerite\Assert\Assert;
7
8
class DetailedMatch
9
{
10
    /**
11
     * @var string
12
     */
13
    public $type;
14
    
15
    /**
16
     * @var string
17
     */
18
    public $id;
19
    
20
    /**
21
     * @var DateTime
22
     */
23
    public $createdAt;
24
    
25
    /**
26
     * @var int
27
     */
28
    public $duration;
29
    
30
    /**
31
     * @var string
32
     */
33
    public $gameMode;
34
    
35
    /**
36
     * @var string
37
     */
38
    public $patchVersion;
39
    
40
    /**
41
     * @var string
42
     */
43
    public $shardId;
44
    
45
    /**
46
     * @var string
47
     */
48
    public $titleId;
49
    
50
    /**
51
     * @var Map
52
     */
53
    public $map;
54
    
55
    /**
56
     * @var Assets
57
     */
58
    public $assets;
59
60
    /**
61
     * @var Spectators
62
     */
63
    public $spectators;
64
65
    /**
66
     * @var Rosters
67
     */
68
    public $rosters;
69
70
    /**
71
     * @var Rounds
72
     */
73
    public $rounds;
74
75
    /**
76
     * @var Participants
77
     */
78
    public $participants;
79
    
80
    /**
81
     * @var Players
82
     */
83
    public $players;
84
85
    /**
86
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
87
     */
88 1
    private function __construct(
89
        string $type,
90
        string $id,
91
        DateTime $createdAt,
92
        int $duration,
93
        string $gameMode,
94
        string $patchVersion,
95
        string $shardId,
96
        string $titleId,
97
        Map $map,
98
        Assets $assets,
99
        Spectators $spectators,
100
        Rosters $rosters,
101
        Rounds $rounds,
102
        Participants $participants,
103
        Players $players
104
    ) {
105 1
        $this->type = $type;
106 1
        $this->id = $id;
107 1
        $this->createdAt = $createdAt;
108 1
        $this->duration = $duration;
109 1
        $this->gameMode = $gameMode;
110 1
        $this->patchVersion = $patchVersion;
111 1
        $this->shardId = $shardId;
112 1
        $this->titleId = $titleId;
113 1
        $this->map = $map;
114 1
        $this->assets = $assets;
115 1
        $this->spectators = $spectators;
116 1
        $this->rosters = $rosters;
117 1
        $this->rounds = $rounds;
118 1
        $this->participants = $participants;
119 1
        $this->players = $players;
120 1
    }
121
122 1
    public static function createFromArray(array $match): self
123
    {
124 1
        $matchData = $match['data'];
125 1
        Assert::string($matchData['type']);
126 1
        Assert::string($matchData['id']);
127 1
        Assert::date($matchData['attributes']['createdAt'], DateTime::ISO8601);
128 1
        Assert::integer($matchData['attributes']['duration']);
129 1
        Assert::string($matchData['attributes']['gameMode']);
130 1
        Assert::string($matchData['attributes']['patchVersion']);
131 1
        Assert::string($matchData['attributes']['shardId']);
132 1
        Assert::isArray($matchData['attributes']['stats']);
133 1
        Assert::string($matchData['attributes']['titleId']);
134 1
        Assert::isArray($matchData['relationships']['assets']['data']);
135 1
        Assert::isArray($matchData['relationships']['rosters']['data']);
136 1
        Assert::isArray($matchData['relationships']['rounds']['data']);
137 1
        Assert::isArray($matchData['relationships']['spectators']['data']);
138
139 1
        $matchIncludes = $match['included'];
140
141 1
        $participants = [];
142 1
        $rosters = [];
143 1
        $rounds = [];
144 1
        $players = [];
145 1
        foreach ($matchIncludes as $include) {
146 1
            Assert::string($include['type']);
147 1
            switch ($include['type']) {
148 1
                case 'participant':
149 1
                    $participants[] = Participant::createFromArray($include);
150 1
                    break;
151 1
                case 'roster':
152 1
                    $rosters[] = Roster::createFromArray($include);
153 1
                    break;
154 1
                case 'round':
155 1
                    $rounds[] = Round::createFromArray($include);
156 1
                    break;
157 1
                case 'player':
158 1
                    $players[] = Player::createFromArray($include);
159 1
                    break;
160
            }
161
        }
162
163 1
        return new self(
164 1
            $matchData['type'],
165 1
            $matchData['id'],
166 1
            DateTime::createFromFormat(DateTime::ISO8601, $matchData['attributes']['createdAt']),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...ributes']['createdAt']) targeting DateTime::createFromFormat() can also be of type false; however, PtrTn\Battlerite\Dto\DetailedMatch::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
167 1
            $matchData['attributes']['duration'],
168 1
            $matchData['attributes']['gameMode'],
169 1
            $matchData['attributes']['patchVersion'],
170 1
            $matchData['attributes']['shardId'],
171 1
            $matchData['attributes']['titleId'],
172 1
            Map::createFromArray($matchData['attributes']['stats']),
173 1
            Assets::createFromArray($matchData['relationships']['assets']['data']),
174 1
            Spectators::createFromArray($matchData['relationships']['spectators']['data']),
175 1
            new Rosters($rosters),
176 1
            new Rounds($rounds),
177 1
            new Participants($participants),
178 1
            new Players($players)
179
        );
180
    }
181
}
182