Passed
Push — master ( 26f223...98f436 )
by Peter
03:08
created

ContentList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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