Passed
Push — master ( bdc105...ec8122 )
by Peter
01:57
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\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 1
    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 1
        $this->type = $type;
109 1
        $this->id = $id;
110 1
        $this->createdAt = $createdAt;
111 1
        $this->duration = $duration;
112 1
        $this->gameMode = $gameMode;
113 1
        $this->patchVersion = $patchVersion;
114 1
        $this->shardId = $shardId;
115 1
        $this->titleId = $titleId;
116 1
        $this->map = $map;
117 1
        $this->assets = $assets;
118 1
        $this->spectators = $spectators;
119 1
        $this->rosters = $rosters;
120 1
        $this->rounds = $rounds;
121 1
        $this->participants = $participants;
122 1
        $this->players = $players;
123 1
    }
124
125 1
    public static function createFromArray(array $match): self
126
    {
127 1
        $matchData = $match['data'];
128 1
        Assert::string($matchData['type']);
129 1
        Assert::string($matchData['id']);
130 1
        Assert::date($matchData['attributes']['createdAt'], DateTime::ISO8601);
131 1
        Assert::integer($matchData['attributes']['duration']);
132 1
        Assert::string($matchData['attributes']['gameMode']);
133 1
        Assert::string($matchData['attributes']['patchVersion']);
134 1
        Assert::string($matchData['attributes']['shardId']);
135 1
        Assert::isArray($matchData['attributes']['stats']);
136 1
        Assert::string($matchData['attributes']['titleId']);
137 1
        Assert::isArray($matchData['relationships']['assets']['data']);
138 1
        Assert::isArray($matchData['relationships']['rosters']['data']);
139 1
        Assert::isArray($matchData['relationships']['rounds']['data']);
140 1
        Assert::isArray($matchData['relationships']['spectators']['data']);
141
142 1
        $matchIncludes = $match['included'];
143
144 1
        $participants = [];
145 1
        $rosters = [];
146 1
        $rounds = [];
147 1
        $players = [];
148 1
        foreach ($matchIncludes as $include) {
149 1
            Assert::string($include['type']);
150 1
            switch ($include['type']) {
151 1
                case 'participant':
152 1
                    $participants[] = Participant::createFromArray($include);
153 1
                    break;
154 1
                case 'roster':
155 1
                    $rosters[] = Roster::createFromArray($include);
156 1
                    break;
157 1
                case 'round':
158 1
                    $rounds[] = Round::createFromArray($include);
159 1
                    break;
160 1
                case 'player':
161 1
                    $players[] = Player::createFromArray($include);
162 1
                    break;
163
            }
164
        }
165
166 1
        return new self(
167 1
            $matchData['type'],
168 1
            $matchData['id'],
169 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\Mat...ledMatch::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
170 1
            $matchData['attributes']['duration'],
171 1
            $matchData['attributes']['gameMode'],
172 1
            $matchData['attributes']['patchVersion'],
173 1
            $matchData['attributes']['shardId'],
174 1
            $matchData['attributes']['titleId'],
175 1
            Map::createFromArray($matchData['attributes']['stats']),
176 1
            Assets::createFromArray($matchData['relationships']['assets']['data']),
177 1
            Spectators::createFromArray($matchData['relationships']['spectators']['data']),
178 1
            new Rosters($rosters),
179 1
            new Rounds($rounds),
180 1
            new Participants($participants),
181 1
            new Players($players)
182
        );
183
    }
184
}
185