Passed
Push — master ( bdc105...ec8122 )
by Peter
01:57
created

Match::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 13
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\Matches;
4
5
use DateTime;
6
use PtrTn\Battlerite\Assert\Assert;
7
8
class Match
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 Map
47
     */
48
    public $map;
49
50
    /**
51
     * @var Assets
52
     */
53
    public $assets;
54
55
    /**
56
     * @var Rosters
57
     */
58
    public $rosters;
59
60
    /**
61
     * @var Rounds
62
     */
63
    public $rounds;
64
65
    /**
66
     * @var Spectators
67
     */
68
    public $spectators;
69
70
    /**
71
     * @var string
72
     */
73
    public $titleId;
74
75
    /**
76
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
77
     */
78 3
    private function __construct(
79
        string $type,
80
        string $id,
81
        DateTime $createdAt,
82
        int $duration,
83
        string $gameMode,
84
        string $patchVersion,
85
        string $shardId,
86
        Map $map,
87
        Assets $assets,
88
        Rosters $rosters,
89
        Rounds $rounds,
90
        Spectators $spectators,
91
        string $titleId
92
    ) {
93 3
        $this->type = $type;
94 3
        $this->id = $id;
95 3
        $this->createdAt = $createdAt;
96 3
        $this->duration = $duration;
97 3
        $this->gameMode = $gameMode;
98 3
        $this->patchVersion = $patchVersion;
99 3
        $this->shardId = $shardId;
100 3
        $this->map = $map;
101 3
        $this->assets = $assets;
102 3
        $this->rosters = $rosters;
103 3
        $this->rounds = $rounds;
104 3
        $this->spectators = $spectators;
105 3
        $this->titleId = $titleId;
106 3
    }
107
108 3
    public static function createFromArray(array $match): self
109
    {
110 3
        Assert::string($match['type']);
111 3
        Assert::string($match['id']);
112 3
        Assert::date($match['attributes']['createdAt'], DateTime::ISO8601);
113 3
        Assert::integer($match['attributes']['duration']);
114 3
        Assert::string($match['attributes']['gameMode']);
115 3
        Assert::string($match['attributes']['patchVersion']);
116 3
        Assert::string($match['attributes']['shardId']);
117 3
        Assert::isArray($match['attributes']['stats']);
118 3
        Assert::isArray($match['relationships']['assets']['data']);
119 3
        Assert::isArray($match['relationships']['rosters']['data']);
120 3
        Assert::isArray($match['relationships']['rounds']['data']);
121 3
        Assert::isArray($match['relationships']['spectators']['data']);
122 3
        Assert::string($match['attributes']['titleId']);
123
124 3
        return new self(
125 3
            $match['type'],
126 3
            $match['id'],
127 3
            DateTime::createFromFormat(DateTime::ISO8601, $match['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\Matches\Match::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
128 3
            $match['attributes']['duration'],
129 3
            $match['attributes']['gameMode'],
130 3
            $match['attributes']['patchVersion'],
131 3
            $match['attributes']['shardId'],
132 3
            Map::createFromArray($match['attributes']['stats']),
133 3
            Assets::createFromArray($match['relationships']['assets']['data']),
134 3
            Rosters::createFromArray($match['relationships']['rosters']['data']),
135 3
            Rounds::createFromArray($match['relationships']['rounds']['data']),
136 3
            Spectators::createFromArray($match['relationships']['spectators']['data']),
137 3
            $match['attributes']['titleId']
138
        );
139
    }
140
}
141