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

DetailedMatch::createFromArray()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 59
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 51
cts 51
cp 1
rs 8.7117
c 0
b 0
f 0
cc 6
eloc 51
nc 6
nop 1
crap 6

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;
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