MergeRequestApproval::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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
    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
        $this->mergeStatus = $mergeStatus;
71
        $this->approvalsRequired = $approvalsRequired;
72
        $this->approvalsLeft = $approvalsLeft;
73
        $this->approvedBy = $approvedBy;
74
        $this->approvers = $approvers;
75
        $this->approverGroups = $approverGroups;
76
        $this->suggestedApprovers = $suggestedApprovers;
77
        $this->updatedAt = $updatedAt;
78
        $this->createdAt = $createdAt;
79
        $this->mergeRequest = $mergeRequest;
80
    }
81
82
    /**
83
     * @param array $mergeRequestApproval
84
     * @return MergeRequestApproval
85
     * @throws \Exception
86
     */
87
    public static function fromArray(array $mergeRequestApproval): self
88
    {
89
        return new self(
90
            (string)$mergeRequestApproval['merge_status'],
91
            (int)$mergeRequestApproval['approvals_required'],
92
            (int)$mergeRequestApproval['approvals_left'],
93
            $mergeRequestApproval['approved_by'],
94
            $mergeRequestApproval['approvers'],
95
            $mergeRequestApproval['approver_groups'],
96
            $mergeRequestApproval['suggested_approvers'],
97
            new Carbon($mergeRequestApproval['updated_at']),
98
            new Carbon($mergeRequestApproval['created_at']),
99
            $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
    public function getApprovers(): array
139
    {
140
        return $this->approvers;
141
    }
142
143
    /**
144
     * @return array
145
     */
146
    public function getApproverNames(): array
147
    {
148
        $result = [];
149
        foreach ($this->approvers as $approver) {
150
            $result[] = $approver->getUsername();
151
        }
152
        sort($result, SORT_LOCALE_STRING);
153
154
        return $result;
155
    }
156
157
    /**
158
     * @return Group[]
159
     */
160
    public function getApproverGroups(): array
161
    {
162
        return $this->approverGroups;
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function getApproverGroupNames(): array
169
    {
170
        $result = [];
171
        foreach ($this->approverGroups as $approverGroup) {
172
            $result[] = $approverGroup->getName();
173
        }
174
        sort($result, SORT_LOCALE_STRING);
175
176
        return $result;
177
    }
178
179
    /**
180
     * @return User[]
181
     */
182
    public function getSuggestedApprovers(): array
183
    {
184
        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
    public function getCreatedAt(): Carbon
205
    {
206
        return $this->createdAt;
207
    }
208
209
    /**
210
     * @return Carbon
211
     */
212
    public function getUpdatedAt(): Carbon
213
    {
214
        return $this->updatedAt;
215
    }
216
217
    /**
218
     * @return MergeRequest
219
     */
220
    public function getMergeRequest(): MergeRequest
221
    {
222
        return $this->mergeRequest;
223
    }
224
}
225