Passed
Pull Request — master (#1)
by Peter
02:41
created

ContentList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 22
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
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Framework\Helper\DateHelper;
9
use DateTime;
10
11
/**
12
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
13
 */
14
class ContentList implements IStringerEntity
15
{
16
    /** @var string */
17
    protected $id;
18
19
    /** @var string */
20
    protected $typeId;
21
22
    /** @var string */
23
    protected $name;
24
25
    /** @var string */
26
    protected $identifier;
27
28
    /** @var bool */
29
    protected $protected;
30
31
    /** @var bool */
32
    protected $withImage;
33
34
    /** @var bool */
35
    protected $withLinks;
36
37
    /** @var bool */
38
    protected $withHtml;
39
40
    /** @var ContentListItem[]|null */
41
    protected $items;
42
43
    /** @var DateTime|null */
44
    protected $deletedAt;
45
46
    /**
47
     * ContentList constructor.
48
     *
49
     * @param string                 $id
50
     * @param string                 $typeId
51
     * @param string                 $name
52
     * @param string                 $identifier
53
     * @param bool                   $protected
54
     * @param bool                   $withImage
55
     * @param bool                   $withLinks
56
     * @param bool                   $withHtml
57
     * @param ContentListItem[]|null $items
58
     * @param DateTime|null          $deletedAt
59
     */
60
    public function __construct(
61
        string $id,
62
        string $typeId,
63
        string $name,
64
        string $identifier,
65
        bool $protected,
66
        bool $withImage,
67
        bool $withLinks,
68
        bool $withHtml,
69
        array $items = null,
70
        ?DateTime $deletedAt = null
71
    ) {
72
        $this->id         = $id;
73
        $this->typeId     = $typeId;
74
        $this->name       = $name;
75
        $this->identifier = $identifier;
76
        $this->protected  = $protected;
77
        $this->withImage  = $withImage;
78
        $this->withLinks  = $withLinks;
79
        $this->withHtml   = $withHtml;
80
        $this->items      = $items;
81
        $this->deletedAt  = $deletedAt;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getId()
88
    {
89
        return $this->id;
90
    }
91
92
    /**
93
     * @param string $id
94
     */
95
    public function setId($id)
96
    {
97
        $this->id = $id;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getTypeId(): string
104
    {
105
        return $this->typeId;
106
    }
107
108
    /**
109
     * @param string $typeId
110
     *
111
     * @return $this
112
     */
113
    public function setTypeId(string $typeId): ContentList
114
    {
115
        $this->typeId = $typeId;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getIdentifier(): string
124
    {
125
        return $this->identifier;
126
    }
127
128
    /**
129
     * @param string $identifier
130
     *
131
     * @return $this
132
     */
133
    public function setIdentifier(string $identifier): ContentList
134
    {
135
        $this->identifier = $identifier;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getName(): string
144
    {
145
        return $this->name;
146
    }
147
148
    /**
149
     * @param string $name
150
     *
151
     * @return $this
152
     */
153
    public function setName(string $name): ContentList
154
    {
155
        $this->name = $name;
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isProtected(): bool
164
    {
165
        return $this->protected;
166
    }
167
168
    /**
169
     * @param bool $protected
170
     *
171
     * @return $this
172
     */
173
    public function setProtected(bool $protected): ContentList
174
    {
175
        $this->protected = $protected;
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function isWithImage(): bool
184
    {
185
        return $this->withImage;
186
    }
187
188
    /**
189
     * @param bool $withImage
190
     *
191
     * @return $this
192
     */
193
    public function setWithImage(bool $withImage): ContentList
194
    {
195
        $this->withImage = $withImage;
196
197
        return $this;
198
    }
199
200
    /**
201
     * @return bool
202
     */
203
    public function isWithLinks(): bool
204
    {
205
        return $this->withLinks;
206
    }
207
208
    /**
209
     * @param bool $withLinks
210
     *
211
     * @return $this
212
     */
213
    public function setWithLinks(bool $withLinks): ContentList
214
    {
215
        $this->withLinks = $withLinks;
216
217
        return $this;
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function isWithHtml(): bool
224
    {
225
        return $this->withHtml;
226
    }
227
228
    /**
229
     * @param bool $withHtml
230
     *
231
     * @return $this
232
     */
233
    public function setWithHtml(bool $withHtml): ContentList
234
    {
235
        $this->withHtml = $withHtml;
236
237
        return $this;
238
    }
239
240
    /**
241
     * @return ContentListItem[]|null
242
     */
243
    public function getItems(): ?array
244
    {
245
        return $this->items;
246
    }
247
248
    /**
249
     * @param ContentListItem[]|null $items
250
     *
251
     * @return $this
252
     */
253
    public function setItems(?array $items = null): ContentList
254
    {
255
        $this->items = $items;
256
257
        return $this;
258
    }
259
260
    /**
261
     * @return DateTime|null
262
     */
263
    public function getDeletedAt(): ?DateTime
264
    {
265
        return $this->deletedAt;
266
    }
267
268
    /**
269
     * @param DateTime|null $deletedAt
270
     *
271
     * @return $this
272
     */
273
    public function setDeletedAt(?DateTime $deletedAt): ContentList
274
    {
275
        $this->deletedAt = $deletedAt;
276
277
        return $this;
278
    }
279
280
    /**
281
     * @return string
282
     */
283
    public function __toString(): string
284
    {
285
        return $this->getIdentifier();
286
    }
287
288
    /**
289
     * @return string
290
     */
291
    public function toJSON(): string
292
    {
293
        $data = [
294
            'id'         => $this->getId(),
295
            'name'       => $this->getName(),
296
            'identifier' => $this->getIdentifier(),
297
            'protected'  => $this->isProtected(),
298
            'with_image' => $this->isWithImage(),
299
            'with_links' => $this->isWithLinks(),
300
            'with_html'  => $this->isWithHtml(),
301
        ];
302
303
        if ($this->items !== null) {
304
            $items = [];
305
            foreach ($this->items as $item) {
306
                $items[] = json_decode($item->toJSON(), true);
307
            }
308
            $data['items'] = $items;
309
        }
310
311
        if ($this->getDeletedAt()) {
312
            $data['deleted_at'] = DateHelper::formatDateTime($this->getDeletedAt());
313
        }
314
315
        return json_encode($data);
316
    }
317
}
318