Completed
Push — master ( 5f86bb...797bc6 )
by Beñat
04:08
created

Answer::fromProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 9.5147
c 0
b 0
f 0
cc 1
eloc 58
nc 1
nop 27

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Model;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class Answer implements Model
20
{
21
    protected $id;
22
    protected $accepted;
23
    protected $awardedBountyAmount;
24
    protected $awardedBountyUsers;
25
    protected $canFlag;
26
    protected $isAccepted;
27
    protected $questionId;
28
    protected $communityOwnedDate;
29
    protected $lockedDate;
30
    protected $tags;
31
    protected $downvoted;
32
    protected $lastActivityDate;
33
    protected $shareLink;
34
    protected $title;
35
    protected $commentCount;
36
    protected $comments;
37
    protected $lastEditDate;
38
    protected $lastEditor;
39
    protected $downVoteCount;
40
    protected $upVoteCount;
41
    protected $body;
42
    protected $bodyMarkDown;
43
    protected $creationDate;
44
    protected $link;
45
    protected $owner;
46
    protected $score;
47
    protected $upvoted;
48
49
    public static function fromJson(array $data) : self
50
    {
51
        $tags = [];
52
        $awardedBountyUsers = [];
53
        $comments = [];
54
55
        if (array_key_exists('tags', $data) && is_array($data['tags'])) {
56
            foreach ($data['tags'] as $tag) {
57
                $tags[] = Tag::fromJson($tag);
58
            }
59
        }
60
        if (array_key_exists('awarded_bounty_users', $data) && is_array($data['awarded_bounty_users'])) {
61
            foreach ($data['awarded_bounty_users'] as $awardedBountyUser) {
62
                $awardedBountyUsers[] = ShallowUser::fromJson($awardedBountyUser);
63
            }
64
        }
65
        if (array_key_exists('comments', $data) && is_array($data['comments'])) {
66
            foreach ($data['comments'] as $comment) {
67
                $comments[] = Comment::fromJson($comment);
68
            }
69
        }
70
71
        $instance = new self();
72
        $instance
73
            ->setId(array_key_exists('answer_id', $data) ? $data['answer_id'] : null)
74
            ->setAccepted(array_key_exists('accepted', $data) ? $data['accepted'] : null)
75
            ->setCanFlag(array_key_exists('can_flag', $data) ? $data['can_flag'] : null)
76
            ->setIsAccepted(array_key_exists('is_accepted', $data) ? $data['is_accepted'] : null)
77
            ->setQuestionId(array_key_exists('question_id', $data) ? $data['question_id'] : null)
78
            ->setTags($tags)
79
            ->setDownvoted(array_key_exists('downvoted', $data) ? $data['downvoted'] : null)
80
            ->setLastActivityDate(
81
                array_key_exists('last_activity_date', $data)
82
                    ? new \DateTimeImmutable('@' . $data['last_activity_date'])
83
                    : null
84
            )
85
            ->setShareLink(array_key_exists('share_link', $data) ? $data['share_link'] : null)
86
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null)
87
            ->setCommentCount(array_key_exists('comment_count', $data) ? $data['comment_count'] : null)
88
            ->setDownVoteCount(array_key_exists('down_vote_count', $data) ? $data['down_vote_count'] : null)
89
            ->setUpVoteCount(array_key_exists('up_vote_count', $data) ? $data['up_vote_count'] : null)
90
            ->setBody(array_key_exists('body', $data) ? $data['body'] : null)
91
            ->setBodyMarkDown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null)
92
            ->setCreationDate(
93
                array_key_exists('creation_date', $data)
94
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
95
                    : null
96
            )
97
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
98
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
99
            ->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null)
100
            ->setAwardedBountyAmount(
101
                array_key_exists('awarded_bounty_amount', $data)
102
                    ? $data['awarded_bounty_amount']
103
                    : null
104
            )
105
            ->setAwardedBountyUsers($awardedBountyUsers)
106
            ->setComments($comments)
107
            ->setCommunityOwnedDate(
108
                array_key_exists('community_owned_date', $data)
109
                    ? new \DateTimeImmutable('@' . $data['community_owned_date'])
110
                    : null
111
            )
112
            ->setLastEditor(array_key_exists('last_editor', $data) ? ShallowUser::fromJson($data['last_editor']) : null)
113
            ->setLastEditDate(
114
                array_key_exists('last_edit_date', $data)
115
                    ? new \DateTimeImmutable('@' . $data['last_edit_date'])
116
                    : null
117
            )
118
            ->setLockedDate(array_key_exists('locked_date', $data) ? new \DateTime('@' . $data['locked_date']) : null)
119
            ->setOwner(array_key_exists('owner', $data) ? ShallowUser::fromJson($data['owner']) : null);
120
121
        return $instance;
122
    }
123
124
    public function getId() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
125
    {
126
        return $this->id;
127
    }
128
129
    public function setId(?int $id) : self
130
    {
131
        $this->id = $id;
132
133
        return $this;
134
    }
135
136
    public function getAccepted() : ?bool
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
137
    {
138
        return $this->accepted;
139
    }
140
141
    public function setAccepted(?bool $accepted) : self
142
    {
143
        $this->accepted = $accepted;
144
145
        return $this;
146
    }
147
148
    public function getAwardedBountyAmount() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
149
    {
150
        return $this->awardedBountyAmount;
151
    }
152
153
    public function setAwardedBountyAmount(?int $awardedBountyAmount) : self
154
    {
155
        $this->awardedBountyAmount = $awardedBountyAmount;
156
157
        return $this;
158
    }
159
160
    public function getAwardedBountyUsers() : array
161
    {
162
        return $this->awardedBountyUsers;
163
    }
164
165
    public function setAwardedBountyUsers(array $awardedBountyUsers) : self
166
    {
167
        $this->awardedBountyUsers = $awardedBountyUsers;
168
169
        return $this;
170
    }
171
172
    public function getCanFlag() : ?bool
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
173
    {
174
        return $this->canFlag;
175
    }
176
177
    public function setCanFlag(?bool $canFlag) : self
178
    {
179
        $this->canFlag = $canFlag;
180
181
        return $this;
182
    }
183
184
    public function getIsAccepted() : ?bool
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
185
    {
186
        return $this->isAccepted;
187
    }
188
189
    public function setIsAccepted(?bool $isAccepted) : self
190
    {
191
        $this->isAccepted = $isAccepted;
192
193
        return $this;
194
    }
195
196
    public function getQuestionId() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
197
    {
198
        return $this->questionId;
199
    }
200
201
    public function setQuestionId(?int $questionId) : self
202
    {
203
        $this->questionId = $questionId;
204
205
        return $this;
206
    }
207
208
    public function getCommunityOwnedDate() : ?\DateTimeInterface
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
209
    {
210
        return $this->communityOwnedDate;
211
    }
212
213
    public function setCommunityOwnedDate(?\DateTimeInterface $communityOwnedDate) : self
214
    {
215
        $this->communityOwnedDate = $communityOwnedDate;
216
217
        return $this;
218
    }
219
220
    public function getLockedDate() : ?\DateTimeInterface
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
221
    {
222
        return $this->lockedDate;
223
    }
224
225
    public function setLockedDate(?\DateTimeInterface $lockedDate) : self
226
    {
227
        $this->lockedDate = $lockedDate;
228
229
        return $this;
230
    }
231
232
    public function getTags() : array
233
    {
234
        return $this->tags;
235
    }
236
237
    public function setTags(array $tags) : self
238
    {
239
        $this->tags = $tags;
240
241
        return $this;
242
    }
243
244
    public function getDownvoted() : ?bool
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
245
    {
246
        return $this->downvoted;
247
    }
248
249
    public function setDownvoted(?bool $downvoted) : self
250
    {
251
        $this->downvoted = $downvoted;
252
253
        return $this;
254
    }
255
256
    public function getLastActivityDate() : \DateTimeInterface
257
    {
258
        return $this->lastActivityDate;
259
    }
260
261
    public function setLastActivityDate(\DateTimeInterface $lastActivityDate) : self
262
    {
263
        $this->lastActivityDate = $lastActivityDate;
264
265
        return $this;
266
    }
267
268
    public function getShareLink() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
269
    {
270
        return $this->shareLink;
271
    }
272
273
    public function setShareLink(?string $shareLink) : self
274
    {
275
        $this->shareLink = $shareLink;
276
277
        return $this;
278
    }
279
280
    public function getTitle() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
281
    {
282
        return $this->title;
283
    }
284
285
    public function setTitle(?string $title) : self
286
    {
287
        $this->title = $title;
288
289
        return $this;
290
    }
291
292
    public function getCommentCount() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
293
    {
294
        return $this->commentCount;
295
    }
296
297
    public function setCommentCount(?int $commentCount) : self
298
    {
299
        $this->commentCount = $commentCount;
300
301
        return $this;
302
    }
303
304
    public function getComments() : array
305
    {
306
        return $this->comments;
307
    }
308
309
    public function setComments(array $comments) : self
310
    {
311
        $this->comments = $comments;
312
313
        return $this;
314
    }
315
316
    public function getLastEditDate() : ?\DateTimeInterface
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
317
    {
318
        return $this->lastEditDate;
319
    }
320
321
    public function setLastEditDate(?\DateTimeInterface $lastEditDate) : self
322
    {
323
        $this->lastEditDate = $lastEditDate;
324
325
        return $this;
326
    }
327
328
    public function getLastEditor() : ?ShallowUser
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
329
    {
330
        return $this->lastEditor;
331
    }
332
333
    public function setLastEditor(?ShallowUser $lastEditor) : self
334
    {
335
        $this->lastEditor = $lastEditor;
336
337
        return $this;
338
    }
339
340
    public function getDownVoteCount() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
341
    {
342
        return $this->downVoteCount;
343
    }
344
345
    public function setDownVoteCount(?int $downVoteCount) : self
346
    {
347
        $this->downVoteCount = $downVoteCount;
348
349
        return $this;
350
    }
351
352
    public function getUpVoteCount() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
353
    {
354
        return $this->upVoteCount;
355
    }
356
357
    public function setUpVoteCount(?int $upVoteCount) : self
358
    {
359
        $this->upVoteCount = $upVoteCount;
360
361
        return $this;
362
    }
363
364
    public function getBody() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
365
    {
366
        return $this->body;
367
    }
368
369
    public function setBody(?string $body) : self
370
    {
371
        $this->body = $body;
372
373
        return $this;
374
    }
375
376
    public function getBodyMarkDown() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
377
    {
378
        return $this->bodyMarkDown;
379
    }
380
381
    public function setBodyMarkDown(?string $bodyMarkDown) : self
382
    {
383
        $this->bodyMarkDown = $bodyMarkDown;
384
385
        return $this;
386
    }
387
388
    public function getCreationDate() : \DateTimeInterface
389
    {
390
        return $this->creationDate;
391
    }
392
393
    public function setCreationDate(\DateTimeInterface $creationDate) : self
394
    {
395
        $this->creationDate = $creationDate;
396
397
        return $this;
398
    }
399
400
    public function getLink() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
401
    {
402
        return $this->link;
403
    }
404
405
    public function setLink(?string $link) : self
406
    {
407
        $this->link = $link;
408
409
        return $this;
410
    }
411
412
    public function getOwner() : ?ShallowUser
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
413
    {
414
        return $this->owner;
415
    }
416
417
    public function setOwner(?ShallowUser $owner) : self
418
    {
419
        $this->owner = $owner;
420
421
        return $this;
422
    }
423
424
    public function getScore() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
425
    {
426
        return $this->score;
427
    }
428
429
    public function setScore(?int $score) : self
430
    {
431
        $this->score = $score;
432
433
        return $this;
434
    }
435
436
    public function getUpvoted() : ?bool
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
437
    {
438
        return $this->upvoted;
439
    }
440
441
    public function setUpvoted(?bool $upvoted) : self
442
    {
443
        $this->upvoted = $upvoted;
444
445
        return $this;
446
    }
447
448
    public function jsonSerialize() : array
449
    {
450
        return [
451
            'accepted'              => $this->getAccepted(),
452
            'answer_id'             => $this->getId(),
453
            'awarded_bounty_amount' => $this->getAwardedBountyAmount(),
454
            'awarded_bounty_users'  => $this->getAwardedBountyUsers(),
455
            'body'                  => $this->getBody(),
456
            'body_markdown'         => $this->getBodyMarkDown(),
457
            'can_flag'              => $this->getCanFlag(),
458
            'comment_count'         => $this->getCommentCount(),
459
            'comments'              => $this->getComments(),
460
            'community_owned_date'  => $this->getCommunityOwnedDate(),
461
            'creation_date'         => $this->getCreationDate(),
462
            'down_vote_count'       => $this->getDownVoteCount(),
463
            'downvoted'             => $this->getDownvoted(),
464
            'is_accepted'           => $this->getIsAccepted(),
465
            'last_activity_date'    => $this->getLastActivityDate(),
466
            'last_edit_date'        => $this->getLastEditDate(),
467
            'last_editor'           => $this->getLastEditor(),
468
            'link'                  => $this->getLink(),
469
            'locked_date'           => $this->getLockedDate(),
470
            'owner'                 => $this->getOwner(),
471
            'question_id'           => $this->getQuestionId(),
472
            'score'                 => $this->getScore(),
473
            'share_link'            => $this->getShareLink(),
474
            'tags'                  => $this->getTags(),
475
            'title'                 => $this->getTitle(),
476
            'up_vote_count'         => $this->getUpVoteCount(),
477
            'upvoted'               => $this->getUpvoted(),
478
        ];
479
    }
480
}
481