Completed
Pull Request — master (#4)
by Peter
02:12
created

Team::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 18
cts 18
cp 1
rs 8.8571
cc 1
eloc 33
nc 1
nop 16
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\Teams;
4
5
use DateTime;
6
use PtrTn\Battlerite\Assert\Assert;
7
8
class Team
9
{
10
    /**
11
     * @var string
12
     */
13
    public $type;
14
15
    /**
16
     * @var string
17
     */
18
    public $id;
19
20
    /**
21
     * @var string
22
     */
23
    public $titleId;
24
25
    /**
26
     * @var string
27
     */
28
    public $name;
29
30
    /**
31
     * @var string
32
     */
33
    public $shardId;
34
35
    /**
36
     * @var int
37
     */
38
    public $avatar;
39
40
    /**
41
     * @var int
42
     */
43
    public $division;
44
45
    /**
46
     * @var int
47
     */
48
    public $divisionRating;
49
50
    /**
51
     * @var int
52
     */
53
    public $league;
54
55
    /**
56
     * @var int
57
     */
58
    public $losses;
59
60
    /**
61
     * @var Members
62
     */
63
    public $members;
64
65
    /**
66
     * @var int
67
     */
68
    public $placementGamesLeft;
69
70
    /**
71
     * @var int
72
     */
73
    public $topDivision;
74
75
    /**
76
     * @var int
77
     */
78
    public $topDivisionRating;
79
80
    /**
81
     * @var int
82
     */
83
    public $topLeague;
84
85
    /**
86
     * @var int
87
     */
88
    public $wins;
89
90
    /**
91
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
92
     */
93 1
    private function __construct(
94
        string $type,
95
        string $id,
96
        string $name,
97
        string $shardId,
98
        int $avatar,
99
        int $division,
100
        int $divisionRating,
101
        int $league,
102
        int $losses,
103
        Members $members,
104
        int $placementGamesLeft,
105
        int $topDivision,
106
        int $topDivisionRating,
107
        int $topLeague,
108
        int $wins,
109
        string $titleId
110
    ) {
111 1
        $this->type = $type;
112 1
        $this->id = $id;
113 1
        $this->name = $name;
114 1
        $this->shardId = $shardId;
115 1
        $this->avatar = $avatar;
116 1
        $this->division = $division;
117 1
        $this->divisionRating = $divisionRating;
118 1
        $this->league = $league;
119 1
        $this->losses = $losses;
120 1
        $this->members = $members;
121 1
        $this->placementGamesLeft = $placementGamesLeft;
122 1
        $this->topDivision = $topDivision;
123 1
        $this->topDivisionRating = $topDivisionRating;
124 1
        $this->topLeague = $topLeague;
125 1
        $this->wins = $wins;
126 1
        $this->titleId = $titleId;
127 1
    }
128
129 1
    public static function createFromArray(array $match): self
130
    {
131 1
        Assert::string($match['type']);
132 1
        Assert::string($match['id']);
133 1
        Assert::string($match['attributes']['name']);
134 1
        Assert::string($match['attributes']['shardId']);
135 1
        Assert::integer($match['attributes']['stats']['avatar']);
136 1
        Assert::integer($match['attributes']['stats']['division']);
137 1
        Assert::integer($match['attributes']['stats']['divisionRating']);
138 1
        Assert::integer($match['attributes']['stats']['league']);
139 1
        Assert::integer($match['attributes']['stats']['losses']);
140 1
        Assert::integer($match['attributes']['stats']['placementGamesLeft']);
141 1
        Assert::integer($match['attributes']['stats']['topDivision']);
142 1
        Assert::integer($match['attributes']['stats']['topDivisionRating']);
143 1
        Assert::integer($match['attributes']['stats']['topLeague']);
144 1
        Assert::integer($match['attributes']['stats']['wins']);
145 1
        Assert::string($match['attributes']['titleId']);
146
147 1
        return new self(
148 1
            $match['type'],
149 1
            $match['id'],
150 1
            $match['attributes']['name'],
151 1
            $match['attributes']['shardId'],
152 1
            $match['attributes']['stats']['avatar'],
153 1
            $match['attributes']['stats']['division'],
154 1
            $match['attributes']['stats']['divisionRating'],
155 1
            $match['attributes']['stats']['league'],
156 1
            $match['attributes']['stats']['losses'],
157 1
            Members::createFromArray($match['attributes']['stats']['members']),
158 1
            $match['attributes']['stats']['placementGamesLeft'],
159 1
            $match['attributes']['stats']['topDivision'],
160 1
            $match['attributes']['stats']['topDivisionRating'],
161 1
            $match['attributes']['stats']['topLeague'],
162 1
            $match['attributes']['stats']['wins'],
163 1
            $match['attributes']['titleId']
164
        );
165
    }
166
}
167