Completed
Push — master ( 706672...0f2117 )
by Daniel
02:07
created

MergeRequestApproval::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 10
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0

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 declare(strict_types=1);
2
3
namespace DanielPieper\MergeReminder\ValueObject;
4
5
use Carbon\Carbon;
6
7
class MergeRequestApproval
8
{
9
    public const MERGE_STATUS_CANNOT_BE_MERGED = 'cannot_be_merged';
10
    public const MERGE_STATUS_CAN_BE_MERGED = 'can_be_merged';
11
12
    /** @var string */
13
    private $mergeStatus;
14
15
    /** @var int */
16
    private $approvalsRequired;
17
18
    /** @var int */
19
    private $approvalsLeft;
20
21
    /** @var User[] */
22
    private $approvedBy;
23
24
    /** @var User[] */
25
    private $approvers;
26
27
    /** @var Group[] */
28
    private $approverGroups;
29
30
    /** @var User[] */
31
    private $suggestedApprovers;
32
33
    /** @var Carbon */
34
    private $updatedAt;
35
36
    /** @var Carbon */
37
    private $createdAt;
38
39
    /** @var MergeRequest */
40
    private $mergeRequest;
41
42
43
    /**
44
     * Project constructor.
45
     * @param string $mergeStatus
46
     * @param int $approvalsRequired
47
     * @param int $approvalsLeft
48
     * @param array $approvedBy
49
     * @param array $approvers
50
     * @param array $approverGroups
51
     * @param array $suggestedApprovers
52
     * @param Carbon $updatedAt
53
     * @param Carbon $createdAt
54
     * @param MergeRequest $mergeRequest
55
     *
56
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
57
     */
58 11
    public function __construct(
59
        string $mergeStatus,
60
        int $approvalsRequired,
61
        int $approvalsLeft,
62
        array $approvedBy,
63
        array $approvers,
64
        array $approverGroups,
65
        array $suggestedApprovers,
66
        Carbon $updatedAt,
67
        Carbon $createdAt,
68
        MergeRequest $mergeRequest
69
    ) {
70 11
        $this->mergeStatus = $mergeStatus;
71 11
        $this->approvalsRequired = $approvalsRequired;
72 11
        $this->approvalsLeft = $approvalsLeft;
73 11
        $this->approvedBy = $approvedBy;
74 11
        $this->approvers = $approvers;
75 11
        $this->approverGroups = $approverGroups;
76 11
        $this->suggestedApprovers = $suggestedApprovers;
77 11
        $this->updatedAt = $updatedAt;
78 11
        $this->createdAt = $createdAt;
79 11
        $this->mergeRequest = $mergeRequest;
80 11
    }
81
82
    /**
83
     * @param array $mergeRequestApproval
84
     * @return MergeRequestApproval
85
     * @throws \Exception
86
     */
87 11
    public static function fromArray(array $mergeRequestApproval): self
88
    {
89 11
        return new self(
90 11
            (string)$mergeRequestApproval['merge_status'],
91 11
            (int)$mergeRequestApproval['approvals_required'],
92 11
            (int)$mergeRequestApproval['approvals_left'],
93 11
            $mergeRequestApproval['approved_by'],
94 11
            $mergeRequestApproval['approvers'],
95 11
            $mergeRequestApproval['approver_groups'],
96 11
            $mergeRequestApproval['suggested_approvers'],
97 11
            new Carbon($mergeRequestApproval['updated_at']),
98 11
            new Carbon($mergeRequestApproval['created_at']),
99 11
            $mergeRequestApproval['merge_request']
100
        );
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getMergeStatus(): string
107
    {
108
        return $this->mergeStatus;
109
    }
110
111
    /**
112
     * @return int
113
     */
114
    public function getApprovalsRequired(): int
115
    {
116
        return $this->approvalsRequired;
117
    }
118
119
    /**
120
     * @return int
121
     */
122
    public function getApprovalsLeft(): int
123
    {
124
        return $this->approvalsLeft;
125
    }
126
127
    /**
128
     * @return User[]
129
     */
130
    public function getApprovedBy(): array
131
    {
132
        return $this->approvedBy;
133
    }
134
135
    /**
136
     * @return User[]
137
     */
138 1
    public function getApprovers(): array
139
    {
140 1
        return $this->approvers;
141
    }
142
143
    /**
144
     * @return array
145
     */
146 2
    public function getApproverNames(): array
147
    {
148 2
        $result = [];
149 2
        foreach ($this->approvers as $approver) {
150 1
            $result[] = $approver->getUsername();
151
        }
152 2
        sort($result, SORT_LOCALE_STRING);
153
154 2
        return $result;
155
    }
156
157
    /**
158
     * @return Group[]
159
     */
160 1
    public function getApproverGroups(): array
161
    {
162 1
        return $this->approverGroups;
163
    }
164
165
    /**
166
     * @return array
167
     */
168 2
    public function getApproverGroupNames(): array
169
    {
170 2
        $result = [];
171 2
        foreach ($this->approverGroups as $approverGroup) {
172 1
            $result[] = $approverGroup->getName();
173
        }
174 2
        sort($result, SORT_LOCALE_STRING);
175
176 2
        return $result;
177
    }
178
179
    /**
180
     * @return User[]
181
     */
182 1
    public function getSuggestedApprovers(): array
183
    {
184 1
        return $this->suggestedApprovers;
185
    }
186
187
    /**
188
     * @return array
189
     */
190
    public function getSuggestedApproverNames(): array
191
    {
192
        $result = [];
193
        foreach ($this->suggestedApprovers as $approver) {
194
            $result[] = $approver->getUsername();
195
        }
196
        sort($result, SORT_LOCALE_STRING);
197
198
        return $result;
199
    }
200
201
    /**
202
     * @return Carbon
203
     */
204 6
    public function getCreatedAt(): Carbon
205
    {
206 6
        return $this->createdAt;
207
    }
208
209
    /**
210
     * @return Carbon
211
     */
212 2
    public function getUpdatedAt(): Carbon
213
    {
214 2
        return $this->updatedAt;
215
    }
216
217
    /**
218
     * @return MergeRequest
219
     */
220 3
    public function getMergeRequest(): MergeRequest
221
    {
222 3
        return $this->mergeRequest;
223
    }
224
}
225