Passed
Push — master ( df80bc...5cc7cd )
by Jérémy
03:15 queued 01:22
created

UserDto::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 9.9666
cc 1
nc 1
nop 9

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
declare(strict_types=1);
4
5
namespace JDecool\Clockify\Model;
6
7
class UserDto
8
{
9
    private $activeWorkspace;
10
    private $defaultWorkspace;
11
    private $email;
12
    private $id;
13
    private $memberships;
14
    private $name;
15
    private $profilePicture;
16
    private $settings;
17
    private $status;
18
19
    public static function fromArray(array $data): self
20
    {
21
        return new self(
22
            $data['activeWorkspace'],
23
            $data['defaultWorkspace'],
24
            $data['email'],
25
            $data['id'],
26
            array_map(
27
                static function(array $membership): MembershipDto {
28
                    return MembershipDto::fromArray($membership);
29
                },
30
                $data['memberships']
31
            ),
32
            $data['name'],
33
            $data['profilePicture'],
34
            UserSettingsDto::fromArray($data['settings']),
35
            new UserStatus($data['status'])
36
        );
37
    }
38
39
    /**
40
     * @param MembershipDto[] $memberships
41
     */
42
    public function __construct(
43
        string $activeWorkspace,
44
        string $defaultWorkspace,
45
        string $email,
46
        string $id,
47
        array $memberships,
48
        string $name,
49
        string $profilePicture,
50
        UserSettingsDto $settings,
51
        UserStatus $status
52
    ) {
53
        $this->activeWorkspace = $activeWorkspace;
54
        $this->defaultWorkspace = $defaultWorkspace;
55
        $this->email = $email;
56
        $this->id = $id;
57
        $this->memberships = $memberships;
58
        $this->name = $name;
59
        $this->profilePicture = $profilePicture;
60
        $this->settings = $settings;
61
        $this->status = $status;
62
    }
63
64
    public function activeWorkspace(): string
65
    {
66
        return $this->activeWorkspace;
67
    }
68
69
    public function defaultWorkspace(): string
70
    {
71
        return $this->defaultWorkspace;
72
    }
73
74
    public function email(): string
75
    {
76
        return $this->email;
77
    }
78
79
    public function id(): string
80
    {
81
        return $this->id;
82
    }
83
84
    /**
85
     * @return MembershipDto[]
86
     */
87
    public function memberships(): array
88
    {
89
        return $this->memberships;
90
    }
91
92
    public function name(): string
93
    {
94
        return $this->name;
95
    }
96
97
    public function profilePicture(): string
98
    {
99
        return $this->profilePicture;
100
    }
101
102
    public function settings(): UserSettingsDto
103
    {
104
        return $this->settings;
105
    }
106
107
    public function status(): UserStatus
108
    {
109
        return $this->status;
110
    }
111
}
112