Passed
Push — master ( 56d79f...0f1631 )
by Peter
02:35
created

ContentListItem::__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
9
/**
10
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
11
 */
12
class ContentListItem implements IStringerEntity
13
{
14
    /** @var string */
15
    protected $id;
16
17
    /** @var string */
18
    protected $listId;
19
20
    /** @var string */
21
    protected $name;
22
23
    /** @var string */
24
    protected $nameHref;
25
26
    /** @var string */
27
    protected $body;
28
29
    /** @var string */
30
    protected $bodyHref;
31
32
    /** @var string */
33
    protected $imgSrc;
34
35
    /** @var string */
36
    protected $imgHref;
37
38
    /** @var string */
39
    protected $imgAlt;
40
41
    /** @var bool */
42
    protected $deleted;
43
44
    /**
45
     * ContentListItem constructor.
46
     *
47
     * @param string $id
48
     * @param string $listId
49
     * @param string $name
50
     * @param string $nameHref
51
     * @param string $body
52
     * @param string $bodyHref
53
     * @param string $imgSrc
54
     * @param string $imgHref
55
     * @param string $imgAlt
56
     * @param bool   $isDeleted
57
     */
58
    public function __construct(
59
        string $id,
60
        string $listId,
61
        string $name,
62
        string $nameHref,
63
        string $body,
64
        string $bodyHref,
65
        string $imgSrc,
66
        string $imgHref,
67
        string $imgAlt,
68
        bool $isDeleted = false
69
    ) {
70
        $this->id       = $id;
71
        $this->listId   = $listId;
72
        $this->name     = $name;
73
        $this->nameHref = $nameHref;
74
        $this->body     = $body;
75
        $this->bodyHref = $bodyHref;
76
        $this->imgSrc   = $imgSrc;
77
        $this->imgHref  = $imgHref;
78
        $this->imgAlt   = $imgAlt;
79
        $this->deleted  = $isDeleted;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getId()
86
    {
87
        return $this->id;
88
    }
89
90
    /**
91
     * @param string $id
92
     */
93
    public function setId($id)
94
    {
95
        $this->id = $id;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getListId(): string
102
    {
103
        return $this->listId;
104
    }
105
106
    /**
107
     * @param string $listId
108
     *
109
     * @return $this
110
     */
111
    public function setListId(string $listId): ContentListItem
112
    {
113
        $this->listId = $listId;
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getName(): string
122
    {
123
        return $this->name;
124
    }
125
126
    /**
127
     * @param string $name
128
     *
129
     * @return $this
130
     */
131
    public function setName(string $name): ContentListItem
132
    {
133
        $this->name = $name;
134
135
        return $this;
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function getNameHref(): string
142
    {
143
        return $this->nameHref;
144
    }
145
146
    /**
147
     * @param string $nameHref
148
     *
149
     * @return $this
150
     */
151
    public function setNameHref(string $nameHref): ContentListItem
152
    {
153
        $this->nameHref = $nameHref;
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getBody(): string
162
    {
163
        return $this->body;
164
    }
165
166
    /**
167
     * @param string $body
168
     *
169
     * @return $this
170
     */
171
    public function setBody(string $body): ContentListItem
172
    {
173
        $this->body = $body;
174
175
        return $this;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getBodyHref(): string
182
    {
183
        return $this->bodyHref;
184
    }
185
186
    /**
187
     * @param string $bodyHref
188
     *
189
     * @return $this
190
     */
191
    public function setBodyHref(string $bodyHref): ContentListItem
192
    {
193
        $this->bodyHref = $bodyHref;
194
195
        return $this;
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function getImgSrc(): string
202
    {
203
        return $this->imgSrc;
204
    }
205
206
    /**
207
     * @param string $imgSrc
208
     *
209
     * @return $this
210
     */
211
    public function setImgSrc(string $imgSrc): ContentListItem
212
    {
213
        $this->imgSrc = $imgSrc;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getImgHref(): string
222
    {
223
        return $this->imgHref;
224
    }
225
226
    /**
227
     * @param string $imgHref
228
     *
229
     * @return $this
230
     */
231
    public function setImgHref(string $imgHref): ContentListItem
232
    {
233
        $this->imgHref = $imgHref;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getImgAlt(): string
242
    {
243
        return $this->imgAlt;
244
    }
245
246
    /**
247
     * @param string $imgAlt
248
     *
249
     * @return $this
250
     */
251
    public function setImgAlt(string $imgAlt): ContentListItem
252
    {
253
        $this->imgAlt = $imgAlt;
254
255
        return $this;
256
    }
257
258
    /**
259
     * @return bool
260
     */
261
    public function isDeleted(): bool
262
    {
263
        return $this->deleted;
264
    }
265
266
    /**
267
     * @param bool $deleted
268
     */
269
    public function setDeleted(bool $deleted): ContentListItem
270
    {
271
        $this->deleted = $deleted;
272
273
        return $this;
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public function __toString(): string
280
    {
281
        return $this->getName();
282
    }
283
284
    /**
285
     * @return array|null
286
     */
287
    public function toData(): ?array
288
    {
289
        if ($this->deleted) {
290
            return null;
291
        }
292
293
        $data = [
294
            'id'        => $this->getId(),
295
            'list_id'   => $this->getListId(),
296
            'name'      => $this->getName(),
297
            'name_href' => $this->getNameHref(),
298
            'body'      => $this->getBody(),
299
            'body_href' => $this->getBodyHref(),
300
            'img_src'   => $this->getImgSrc(),
301
            'img_href'  => $this->getImgHref(),
302
            'img_alt'   => $this->getImgAlt(),
303
        ];
304
305
        return $data;
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    public function toJSON(): string
312
    {
313
        return json_encode($this->toData());
314
    }
315
}
316