Completed
Pull Request — master (#5)
by Peter
09:39
created

Participant::createFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 72
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 72
ccs 64
cts 64
cp 1
rs 9.102
cc 2
eloc 65
nc 2
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PtrTn\Battlerite\Dto\Match;
4
5
use Webmozart\Assert\Assert;
6
7
/**
8
 * @SuppressWarnings(PHPMD.TooManyFields)
9
 */
10
class Participant
11
{
12
    /**
13
     * @var string
14
     */
15
    public $type;
16
    
17
    /**
18
     * @var string
19
     */
20
    public $id;
21
22
    /**
23
     * @var string
24
     */
25
    public $actor;
26
27
    /**
28
     * @var string
29
     */
30
    public $shardId;
31
32
    /**
33
     * @var int|null
34
     */
35
    public $abilityUses;
36
37
    /**
38
     * @var int
39
     */
40
    public $attachment;
41
42
    /**
43
     * @var int|null
44
     */
45
    public $damageDone;
46
47
    /**
48
     * @var int|null
49
     */
50
    public $damageReceived;
51
52
    /**
53
     * @var int|null
54
     */
55
    public $deaths;
56
57
    /**
58
     * @var int|null
59
     */
60
    public $disablesDone;
61
62
    /**
63
     * @var int|null
64
     */
65
    public $disablesReceived;
66
67
    /**
68
     * @var int
69
     */
70
    public $emote;
71
72
    /**
73
     * @var int|null
74
     */
75
    public $energyGained;
76
77
    /**
78
     * @var int|null
79
     */
80
    public $energyUsed;
81
82
    /**
83
     * @var int|null
84
     */
85
    public $healingDone;
86
87
    /**
88
     * @var int|null
89
     */
90
    public $healingReceived;
91
92
    /**
93
     * @var int|null
94
     */
95
    public $kills;
96
97
    /**
98
     * @var int
99
     */
100
    public $mount;
101
102
    /**
103
     * @var int
104
     */
105
    public $outfit;
106
107
    /**
108
     * @var int|null
109
     */
110
    public $score;
111
112
    /**
113
     * @var int
114
     */
115
    public $side;
116
117
    /**
118
     * @var int|null
119
     */
120
    public $timeAlive;
121
122
    /**
123
     * @var string
124
     */
125
    public $userID;
126
127
    /**
128
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
129
     */
130 12
    public function __construct(
131
        string $type,
132
        string $id,
133
        string $actor,
134
        string $shardId,
135
        ?int $abilityUses,
136
        int $attachment,
137
        ?int $damageDone,
138
        ?int $damageReceived,
139
        ?int $deaths,
140
        ?int $disablesDone,
141
        ?int $disablesReceived,
142
        int $emote,
143
        ?int $energyGained,
144
        ?int $energyUsed,
145
        ?int $healingDone,
146
        ?int $healingReceived,
147
        ?int $kills,
148
        int $mount,
149
        int $outfit,
150
        ?int $score,
151
        int $side,
152
        ?int $timeAlive,
153
        string $userID
154
    ) {
155 12
        $this->type = $type;
156 12
        $this->id = $id;
157 12
        $this->actor = $actor;
158 12
        $this->shardId = $shardId;
159 12
        $this->abilityUses = $abilityUses;
160 12
        $this->attachment = $attachment;
161 12
        $this->damageDone = $damageDone;
162 12
        $this->damageReceived = $damageReceived;
163 12
        $this->deaths = $deaths;
164 12
        $this->disablesDone = $disablesDone;
165 12
        $this->disablesReceived = $disablesReceived;
166 12
        $this->emote = $emote;
167 12
        $this->energyGained = $energyGained;
168 12
        $this->energyUsed = $energyUsed;
169 12
        $this->healingDone = $healingDone;
170 12
        $this->healingReceived = $healingReceived;
171 12
        $this->kills = $kills;
172 12
        $this->mount = $mount;
173 12
        $this->outfit = $outfit;
174 12
        $this->score = $score;
175 12
        $this->side = $side;
176 12
        $this->timeAlive = $timeAlive;
177 12
        $this->userID = $userID;
178 12
    }
179
180 12
    public static function createFromArray(array $participant): self
181
    {
182 12
        $abilityUses = $participant['attributes']['stats']['abilityUses'] ?? null;
183 12
        $damageDone = $participant['attributes']['stats']['damageDone'] ?? null;
184 12
        $damageReceived = $participant['attributes']['stats']['damageReceived'] ?? null;
185 12
        $deaths = $participant['attributes']['stats']['deaths'] ?? null;
186 12
        $disablesDone = $participant['attributes']['stats']['disablesDone'] ?? null;
187 12
        $disablesReceived = $participant['attributes']['stats']['disablesReceived'] ?? null;
188 12
        $energyGained = $participant['attributes']['stats']['energyGained'] ?? null;
189 12
        $energyUsed = $participant['attributes']['stats']['energyUsed'] ?? null;
190 12
        $healingDone = $participant['attributes']['stats']['healingDone'] ?? null;
191 12
        $healingReceived = $participant['attributes']['stats']['healingReceived'] ?? null;
192 12
        $kills = $participant['attributes']['stats']['kills'] ?? null;
193 12
        $score = $participant['attributes']['stats']['score'] ?? null;
194 12
        $timeAlive = $participant['attributes']['stats']['timeAlive'] ?? null;
195
196 12
        Assert::string($participant['type']);
197 12
        Assert::string($participant['id']);
198 12
        Assert::string($participant['attributes']['actor']);
199 12
        Assert::string($participant['attributes']['shardId']);
200 12
        Assert::nullOrInteger($abilityUses);
201 12
        Assert::integer($participant['attributes']['stats']['attachment']);
202 12
        Assert::nullOrInteger($damageDone);
203 12
        Assert::nullOrInteger($damageReceived);
204 12
        Assert::nullOrInteger($deaths);
205 12
        Assert::nullOrInteger($disablesDone);
206 12
        Assert::nullOrInteger($disablesReceived);
207 12
        Assert::integer($participant['attributes']['stats']['emote']);
208 12
        Assert::nullOrInteger($energyGained);
209 12
        Assert::nullOrInteger($energyUsed);
210 12
        Assert::nullOrInteger($healingDone);
211 12
        Assert::nullOrInteger($healingReceived);
212 12
        Assert::nullOrInteger($kills);
213 12
        Assert::integer($participant['attributes']['stats']['mount']);
214 12
        Assert::integer($participant['attributes']['stats']['outfit']);
215 12
        Assert::nullOrInteger($score);
216 12
        Assert::integer($participant['attributes']['stats']['side']);
217 12
        Assert::nullOrInteger($timeAlive);
218
219 12
        if (isset($participant['attributes']['stats']['userID'])) {
220 11
            $userId = (string) $participant['attributes']['stats']['userID'];
221
        } else {
222 4
            $userId = (string) $participant['relationships']['player']['data']['id'];
223
        }
224 12
        Assert::string($userId);
225
226 12
        return new self(
227 12
            $participant['type'],
228 12
            $participant['id'],
229 12
            $participant['attributes']['actor'],
230 12
            $participant['attributes']['shardId'],
231 12
            $abilityUses,
232 12
            $participant['attributes']['stats']['attachment'],
233 12
            $damageDone,
234 12
            $damageReceived,
235 12
            $deaths,
236 12
            $disablesDone,
237 12
            $disablesReceived,
238 12
            $participant['attributes']['stats']['emote'],
239 12
            $energyGained,
240 12
            $energyUsed,
241 12
            $healingDone,
242 12
            $healingReceived,
243 12
            $kills,
244 12
            $participant['attributes']['stats']['mount'],
245 12
            $participant['attributes']['stats']['outfit'],
246 12
            $score,
247 12
            $participant['attributes']['stats']['side'],
248 12
            $timeAlive,
249 12
            $userId
250
        );
251
    }
252
}
253