DetailedMatch::__construct()   B
last analyzed

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