ProjectDtoImpl::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
rs 9.8333
cc 1
nc 1
nop 13

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 ProjectDtoImpl
8
{
9
    private $archived;
10
    private $billable;
11
    private $clientId;
12
    private $clientName;
13
    private $color;
14
    private $duration;
15
    private $estimate;
16
    private $hourlyRate;
17
    private $id;
18
    private $memberships;
19
    private $name;
20
    private $public;
21
    private $workspaceId;
22
23
    public static function fromArray(array $data): self
24
    {
25
        return new self(
26
            $data['archived'],
27
            $data['billable'],
28
            $data['clientId'],
29
            $data['clientName'],
30
            $data['color'],
31
            $data['duration'],
32
            EstimateDto::fromArray($data['estimate']),
33
            $data['hourlyRate'] ? HourlyRateDto::fromArray($data['hourlyRate']) : null,
34
            $data['id'],
35
            array_map(
36
                static function(array $membership): MembershipDto {
37
                    return MembershipDto::fromArray($membership);
38
                },
39
                $data['memberships']
40
            ),
41
            $data['name'],
42
            $data['public'],
43
            $data['workspaceId']
44
        );
45
    }
46
47
    /**
48
     * @param MembershipDto[] $memberships
49
     */
50
    public function __construct(
51
        bool $archived,
52
        bool $billable,
53
        string $clientId,
54
        string $clientName,
55
        string $color,
56
        string $duration,
57
        EstimateDto $estimate,
58
        ?HourlyRateDto $hourlyRate,
59
        string $id,
60
        array $memberships,
61
        string $name,
62
        bool $public,
63
        string $workspaceId
64
    ) {
65
        $this->archived = $archived;
66
        $this->billable = $billable;
67
        $this->clientId = $clientId;
68
        $this->clientName = $clientName;
69
        $this->color = $color;
70
        $this->duration = $duration;
71
        $this->estimate = $estimate;
72
        $this->hourlyRate = $hourlyRate;
73
        $this->id = $id;
74
        $this->memberships = $memberships;
75
        $this->name = $name;
76
        $this->public = $public;
77
        $this->workspaceId = $workspaceId;
78
    }
79
80
    public function archived(): bool
81
    {
82
        return $this->archived;
83
    }
84
85
    public function billable(): bool
86
    {
87
        return $this->billable;
88
    }
89
90
    public function clientId(): string
91
    {
92
        return $this->clientId;
93
    }
94
95
    public function clientName()
96
    {
97
        return $this->clientName;
98
    }
99
100
    public function color(): string
101
    {
102
        return $this->color;
103
    }
104
105
    public function duration(): string
106
    {
107
        return $this->duration;
108
    }
109
110
    public function estimate(): EstimateDto
111
    {
112
        return $this->estimate;
113
    }
114
115
    public function hourlyRate(): ?HourlyRateDto
116
    {
117
        return $this->hourlyRate;
118
    }
119
120
    public function id(): string
121
    {
122
        return $this->id;
123
    }
124
125
    /**
126
     * @return MembershipDto[]
127
     */
128
    public function memberships(): array
129
    {
130
        return $this->memberships;
131
    }
132
133
    public function name(): string
134
    {
135
        return $this->name;
136
    }
137
138
    public function public(): bool
139
    {
140
        return $this->public;
141
    }
142
143
    public function workspaceId(): string
144
    {
145
        return $this->workspaceId;
146
    }
147
}
148