Player::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 17
nc 1
nop 8
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\Players;
4
5
use Webmozart\Assert\Assert;
6
7
class Player
8
{
9
    /**
10
     * @var string
11
     */
12
    public $type;
13
14
    /**
15
     * @var string
16
     */
17
    public $id;
18
19
    /**
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * @var string
26
     */
27
    public $patchVersion;
28
29
    /**
30
     * @var string
31
     */
32
    public $shardId;
33
34
    /**
35
     * @var int
36
     */
37
    public $picture;
38
39
    /**
40
     * @var int
41
     */
42
    public $title;
43
44
    /**
45
     * @var string
46
     */
47
    public $titleId;
48
49 6
    private function __construct(
50
        string $type,
51
        string $id,
52
        string $name,
53
        string $patchVersion,
54
        string $shardId,
55
        int $picture,
56
        int $title,
57
        string $titleId
58
    ) {
59 6
        $this->type = $type;
60 6
        $this->id = $id;
61 6
        $this->name = $name;
62 6
        $this->patchVersion = $patchVersion;
63 6
        $this->shardId = $shardId;
64 6
        $this->picture = $picture;
65 6
        $this->title = $title;
66 6
        $this->titleId = $titleId;
67 6
    }
68
69 6
    public static function createFromArray(array $player): self
70
    {
71 6
        Assert::string($player['type']);
72 6
        Assert::string($player['id']);
73 6
        Assert::string($player['attributes']['name']);
74 6
        Assert::string($player['attributes']['patchVersion']);
75 6
        Assert::string($player['attributes']['shardId']);
76 6
        Assert::integer($player['attributes']['stats']['picture']);
77 6
        Assert::integer($player['attributes']['stats']['title']);
78 6
        Assert::string($player['attributes']['titleId']);
79
80 6
        return new self(
81 6
            $player['type'],
82 6
            $player['id'],
83 6
            $player['attributes']['name'],
84 6
            $player['attributes']['patchVersion'],
85 6
            $player['attributes']['shardId'],
86 6
            $player['attributes']['stats']['picture'],
87 6
            $player['attributes']['stats']['title'],
88 6
            $player['attributes']['titleId']
89
        );
90
    }
91
}
92