Passed
Push — master ( 8f0f73...d2306b )
by Peter
02:30
created

ContentList::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

341
            'with_body'  => $this->/** @scrutinizer ignore-call */ withBody(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
342
            'with_html'  => $this->isWithHtml(),
343
        ];
344
345
        if ($this->items !== null) {
346
            $items = [];
347
            foreach ($this->items as $item) {
348
                $items[] = $item->getData();
349
            }
350
            $data['items'] = $items;
351
        }
352
353
        return $data;
354
    }
355
356
    /**
357
     * @return string
358
     */
359
    public function toJSON(): string
360
    {
361
        return json_encode($this->getData());
362
    }
363
}
364