Completed
Pull Request — master (#3)
by Peter
02:10
created

Participant::createFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 59
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 50
cts 51
cp 0.9804
rs 9.597
c 0
b 0
f 0
cc 2
eloc 52
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
34
     */
35
    public $abilityUses;
36
37
    /**
38
     * @var int
39
     */
40
    public $attachment;
41
42
    /**
43
     * @var int
44
     */
45
    public $damageDone;
46
47
    /**
48
     * @var int
49
     */
50
    public $damageReceived;
51
52
    /**
53
     * @var int
54
     */
55
    public $deaths;
56
57
    /**
58
     * @var int
59
     */
60
    public $disablesDone;
61
62
    /**
63
     * @var int
64
     */
65
    public $disablesReceived;
66
67
    /**
68
     * @var int
69
     */
70
    public $emote;
71
72
    /**
73
     * @var int
74
     */
75
    public $energyGained;
76
77
    /**
78
     * @var int
79
     */
80
    public $energyUsed;
81
82
    /**
83
     * @var int
84
     */
85
    public $healingDone;
86
87
    /**
88
     * @var int
89
     */
90
    public $healingReceived;
91
92
    /**
93
     * @var int
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
109
     */
110
    public $score;
111
112
    /**
113
     * @var int
114
     */
115
    public $side;
116
117
    /**
118
     * @var int
119
     */
120
    public $timeAlive;
121
122
    /**
123
     * @var string
124
     */
125
    public $userID;
126
127
    /**
128
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
129
     */
130 4
    private 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 4
        $this->type = $type;
156 4
        $this->id = $id;
157 4
        $this->actor = $actor;
158 4
        $this->shardId = $shardId;
159 4
        $this->abilityUses = $abilityUses;
160 4
        $this->attachment = $attachment;
161 4
        $this->damageDone = $damageDone;
162 4
        $this->damageReceived = $damageReceived;
163 4
        $this->deaths = $deaths;
164 4
        $this->disablesDone = $disablesDone;
165 4
        $this->disablesReceived = $disablesReceived;
166 4
        $this->emote = $emote;
167 4
        $this->energyGained = $energyGained;
168 4
        $this->energyUsed = $energyUsed;
169 4
        $this->healingDone = $healingDone;
170 4
        $this->healingReceived = $healingReceived;
171 4
        $this->kills = $kills;
172 4
        $this->mount = $mount;
173 4
        $this->outfit = $outfit;
174 4
        $this->score = $score;
175 4
        $this->side = $side;
176 4
        $this->timeAlive = $timeAlive;
177 4
        $this->userID = $userID;
178 4
    }
179
180 4
    public static function createFromArray(array $participant): self
181
    {
182 4
        Assert::string($participant['type']);
183 4
        Assert::string($participant['id']);
184 4
        Assert::string($participant['attributes']['actor']);
185 4
        Assert::string($participant['attributes']['shardId']);
186 4
        Assert::integer($participant['attributes']['stats']['abilityUses']);
187 4
        Assert::integer($participant['attributes']['stats']['attachment']);
188 4
        Assert::integer($participant['attributes']['stats']['damageDone']);
189 4
        Assert::integer($participant['attributes']['stats']['damageReceived']);
190 4
        Assert::integer($participant['attributes']['stats']['deaths']);
191 4
        Assert::integer($participant['attributes']['stats']['disablesDone']);
192 4
        Assert::integer($participant['attributes']['stats']['disablesReceived']);
193 4
        Assert::integer($participant['attributes']['stats']['emote']);
194 4
        Assert::integer($participant['attributes']['stats']['energyGained']);
195 4
        Assert::integer($participant['attributes']['stats']['energyUsed']);
196 4
        Assert::integer($participant['attributes']['stats']['healingDone']);
197 4
        Assert::integer($participant['attributes']['stats']['healingReceived']);
198 4
        Assert::integer($participant['attributes']['stats']['kills']);
199 4
        Assert::integer($participant['attributes']['stats']['mount']);
200 4
        Assert::integer($participant['attributes']['stats']['outfit']);
201 4
        Assert::integer($participant['attributes']['stats']['score']);
202 4
        Assert::integer($participant['attributes']['stats']['side']);
203 4
        Assert::integer($participant['attributes']['stats']['timeAlive']);
204
205 4
        if (isset($participant['attributes']['stats']['userID'])) {
206 4
            $userId = $participant['attributes']['stats']['userID'];
207
        }
208
        else {
209
            $userId = $participant['relationships']['player']['data']['id'];
210
        }
211 4
        Assert::string($userId);
212
213 4
        return new self(
214 4
            $participant['type'],
215 4
            $participant['id'],
216 4
            $participant['attributes']['actor'],
217 4
            $participant['attributes']['shardId'],
218 4
            $participant['attributes']['stats']['abilityUses'],
219 4
            $participant['attributes']['stats']['attachment'],
220 4
            $participant['attributes']['stats']['damageDone'],
221 4
            $participant['attributes']['stats']['damageReceived'],
222 4
            $participant['attributes']['stats']['deaths'],
223 4
            $participant['attributes']['stats']['disablesDone'],
224 4
            $participant['attributes']['stats']['disablesReceived'],
225 4
            $participant['attributes']['stats']['emote'],
226 4
            $participant['attributes']['stats']['energyGained'],
227 4
            $participant['attributes']['stats']['energyUsed'],
228 4
            $participant['attributes']['stats']['healingDone'],
229 4
            $participant['attributes']['stats']['healingReceived'],
230 4
            $participant['attributes']['stats']['kills'],
231 4
            $participant['attributes']['stats']['mount'],
232 4
            $participant['attributes']['stats']['outfit'],
233 4
            $participant['attributes']['stats']['score'],
234 4
            $participant['attributes']['stats']['side'],
235 4
            $participant['attributes']['stats']['timeAlive'],
236 4
            $userId
237
        );
238
    }
239
}
240