Completed
Push — master ( f5580e...4e6c29 )
by Beñat
01:39
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
 * This file is part of the Stack Exchange Api Client library.
15
 *
16
 * (c) Beñat Espiña <[email protected]>
17
 *
18
 * For the full copyright and license information, please view the LICENSE
19
 * file that was distributed with this source code.
20
 */
21
22
namespace BenatEspina\StackExchangeApiClient\Model;
23
24
/**
25
 * The answer model class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class Answer implements Model
30
{
31
    protected $id;
32
    protected $accepted;
33
    protected $awardedBountyAmount;
34
    protected $awardedBountyUsers;
35
    protected $canFlag;
36
    protected $isAccepted;
37
    protected $questionId;
38
    protected $communityOwnedDate;
39
    protected $lockedDate;
40
    protected $tags;
41
    protected $downvoted;
42
    protected $lastActivityDate;
43
    protected $shareLink;
44
    protected $title;
45
    protected $commentCount;
46
    protected $comments;
47
    protected $lastEditDate;
48
    protected $lastEditor;
49
    protected $downVoteCount;
50
    protected $upVoteCount;
51
    protected $body;
52
    protected $bodyMarkDown;
53
    protected $creationDate;
54
    protected $link;
55
    protected $owner;
56
    protected $score;
57
    protected $upvoted;
58
59
    public static function fromProperties(
60
        $id,
61
        $accepted,
62
        $canFlag,
63
        $isAccepted,
64
        $questionId,
65
        array $tags,
66
        $downvoted,
67
        \DateTimeInterface $lastActivityDate,
68
        $shareLink,
69
        $title,
70
        $commentCount,
71
        $downVoteCount,
72
        $upVoteCount,
73
        $body,
74
        $bodyMarkDown,
75
        \DateTimeInterface $creationDate,
76
        $link,
77
        $score,
78
        $upvoted,
79
        $awardedBountyAmount,
80
        array $awardedBountyUsers = [],
81
        array $comments = [],
82
        \DateTimeInterface $communityOwnedDate = null,
83
        ShallowUser $lastEditor = null,
84
        \DateTimeInterface $lastEditDate = null,
85
        \DateTimeInterface $lockedDate = null,
86
        ShallowUser $owner = null
87
    ) {
88
        $instance = new self();
89
        $instance
90
            ->setId($id)
91
            ->setAccepted($accepted)
92
            ->setCanFlag($canFlag)
93
            ->setIsAccepted($isAccepted)
94
            ->setQuestionId($questionId)
95
            ->setTags($tags)
96
            ->setDownvoted($downvoted)
97
            ->setLastActivityDate($lastActivityDate)
98
            ->setShareLink($shareLink)
99
            ->setTitle($title)
100
            ->setCommentCount($commentCount)
101
            ->setDownVoteCount($downVoteCount)
102
            ->setUpVoteCount($upVoteCount)
103
            ->setBody($body)
104
            ->setBodyMarkDown($bodyMarkDown)
105
            ->setCreationDate($creationDate)
106
            ->setLink($link)
107
            ->setScore($score)
108
            ->setUpvoted($upvoted)
109
            ->setAwardedBountyAmount($awardedBountyAmount)
110
            ->setAwardedBountyUsers($awardedBountyUsers)
111
            ->setComments($comments)
112
            ->setCommunityOwnedDate($communityOwnedDate)
113
            ->setLastEditor($lastEditor)
114
            ->setLastEditDate($lastEditDate)
115
            ->setLockedDate($lockedDate)
116
            ->setOwner($owner);
117
118
        return $instance;
119
    }
120
121
    public static function fromJson(array $data)
122
    {
123
        $tags = [];
124
        $awardedBountyUsers = [];
125
        $comments = [];
126
127 View Code Duplication
        if (array_key_exists('tags', $data) && is_array($data['tags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            foreach ($data['tags'] as $tag) {
129
                $tags[] = Tag::fromJson($tag);
130
            }
131
        }
132 View Code Duplication
        if (array_key_exists('awarded_bounty_users', $data) && is_array($data['awarded_bounty_users'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            foreach ($data['awarded_bounty_users'] as $awardedBountyUser) {
134
                $awardedBountyUsers[] = ShallowUser::fromJson($awardedBountyUser);
135
            }
136
        }
137 View Code Duplication
        if (array_key_exists('comments', $data) && is_array($data['comments'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
            foreach ($data['comments'] as $comment) {
139
                $comments[] = Comment::fromJson($comment);
140
            }
141
        }
142
143
        $instance = new self();
144
        $instance
145
            ->setId(array_key_exists('answer_id', $data) ? $data['answer_id'] : null)
146
            ->setAccepted(array_key_exists('accepted', $data) ? $data['accepted'] : null)
147
            ->setCanFlag(array_key_exists('can_flag', $data) ? $data['can_flag'] : null)
148
            ->setIsAccepted(array_key_exists('is_accepted', $data) ? $data['is_accepted'] : null)
149
            ->setQuestionId(array_key_exists('question_id', $data) ? $data['question_id'] : null)
150
            ->setTags($tags)
151
            ->setDownvoted(array_key_exists('downvoted', $data) ? $data['downvoted'] : null)
152
            ->setLastActivityDate(
153
                array_key_exists('last_activity_date', $data)
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('last_a...activity_date']) : null can be null; however, setLastActivityDate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
154
                    ? new \DateTimeImmutable('@' . $data['last_activity_date'])
155
                    : null
156
            )
157
            ->setShareLink(array_key_exists('share_link', $data) ? $data['share_link'] : null)
158
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null)
159
            ->setCommentCount(array_key_exists('comment_count', $data) ? $data['comment_count'] : null)
160
            ->setDownVoteCount(array_key_exists('down_vote_count', $data) ? $data['down_vote_count'] : null)
161
            ->setUpVoteCount(array_key_exists('up_vote_count', $data) ? $data['up_vote_count'] : null)
162
            ->setBody(array_key_exists('body', $data) ? $data['body'] : null)
163
            ->setBodyMarkDown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null)
164
            ->setCreationDate(
165
                array_key_exists('creation_date', $data)
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('creati...creation_date']) : null can be null; however, setCreationDate() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
166
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
167
                    : null
168
            )
169
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
170
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
171
            ->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null)
172
            ->setAwardedBountyAmount(
173
                array_key_exists('awarded_bounty_amount', $data)
174
                    ? $data['awarded_bounty_amount']
175
                    : null
176
            )
177
            ->setAwardedBountyUsers($awardedBountyUsers)
178
            ->setComments($comments)
179
            ->setCommunityOwnedDate(
180
                array_key_exists('community_owned_date', $data)
181
                    ? new \DateTimeImmutable('@' . $data['community_owned_date'])
182
                    : null
183
            )
184
            ->setLastEditor(array_key_exists('last_editor', $data) ? ShallowUser::fromJson($data['last_editor']) : null)
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('last_e...['last_editor']) : null can also be of type object<BenatEspina\Stack...eApiClient\Model\Model>; however, BenatEspina\StackExchang...Answer::setLastEditor() does only seem to accept null|object<BenatEspina\...ient\Model\ShallowUser>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
185
            ->setLastEditDate(
186
                array_key_exists('last_edit_date', $data)
187
                    ? new \DateTimeImmutable('@' . $data['last_edit_date'])
188
                    : null
189
            )
190
            ->setLockedDate(array_key_exists('locked_date', $data) ? new \DateTime('@' . $data['locked_date']) : null)
191
            ->setOwner(array_key_exists('owner', $data) ? ShallowUser::fromJson($data['owner']) : null);
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('owner'...($data['owner']) : null can also be of type object<BenatEspina\Stack...eApiClient\Model\Model>; however, BenatEspina\StackExchang...odel\Answer::setOwner() does only seem to accept null|object<BenatEspina\...ient\Model\ShallowUser>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
192
193
        return $instance;
194
    }
195
196
    public function getId()
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->id;
199
    }
200
201
    public function setId($id)
202
    {
203
        $this->id = $id;
204
205
        return $this;
206
    }
207
208
    public function getAccepted()
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->accepted;
211
    }
212
213
    public function setAccepted($accepted)
214
    {
215
        $this->accepted = $accepted;
216
217
        return $this;
218
    }
219
220
    public function getAwardedBountyAmount()
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->awardedBountyAmount;
223
    }
224
225
    public function setAwardedBountyAmount($awardedBountyAmount)
226
    {
227
        $this->awardedBountyAmount = $awardedBountyAmount;
228
229
        return $this;
230
    }
231
232
    public function getAwardedBountyUsers()
233
    {
234
        return $this->awardedBountyUsers;
235
    }
236
237
    public function setAwardedBountyUsers(array $awardedBountyUsers)
238
    {
239
        $this->awardedBountyUsers = $awardedBountyUsers;
240
241
        return $this;
242
    }
243
244
    public function getCanFlag()
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->canFlag;
247
    }
248
249
    public function setCanFlag($canFlag)
250
    {
251
        $this->canFlag = $canFlag;
252
253
        return $this;
254
    }
255
256
    public function getIsAccepted()
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...
257
    {
258
        return $this->isAccepted;
259
    }
260
261
    public function setIsAccepted($isAccepted)
262
    {
263
        $this->isAccepted = $isAccepted;
264
265
        return $this;
266
    }
267
268
    public function getQuestionId()
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->questionId;
271
    }
272
273
    public function setQuestionId($questionId)
274
    {
275
        $this->questionId = $questionId;
276
277
        return $this;
278
    }
279
280
    public function getCommunityOwnedDate()
281
    {
282
        return $this->communityOwnedDate;
283
    }
284
285
    public function setCommunityOwnedDate(\DateTimeInterface $communityOwnedDate = null)
286
    {
287
        $this->communityOwnedDate = $communityOwnedDate;
288
289
        return $this;
290
    }
291
292
    public function getLockedDate()
293
    {
294
        return $this->lockedDate;
295
    }
296
297
    public function setLockedDate(\DateTimeInterface $lockedDate = null)
298
    {
299
        $this->lockedDate = $lockedDate;
300
301
        return $this;
302
    }
303
304
    public function getTags()
305
    {
306
        return $this->tags;
307
    }
308
309
    public function setTags(array $tags)
310
    {
311
        $this->tags = $tags;
312
313
        return $this;
314
    }
315
316
    public function getDownvoted()
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->downvoted;
319
    }
320
321
    public function setDownvoted($downvoted)
322
    {
323
        $this->downvoted = $downvoted;
324
325
        return $this;
326
    }
327
328
    public function getLastActivityDate()
329
    {
330
        return $this->lastActivityDate;
331
    }
332
333
    public function setLastActivityDate(\DateTimeInterface $lastActivityDate)
334
    {
335
        $this->lastActivityDate = $lastActivityDate;
336
337
        return $this;
338
    }
339
340
    public function getShareLink()
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->shareLink;
343
    }
344
345
    public function setShareLink($shareLink)
346
    {
347
        $this->shareLink = $shareLink;
348
349
        return $this;
350
    }
351
352
    public function getTitle()
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->title;
355
    }
356
357
    public function setTitle($title)
358
    {
359
        $this->title = $title;
360
361
        return $this;
362
    }
363
364
    public function getCommentCount()
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->commentCount;
367
    }
368
369
    public function setCommentCount($commentCount)
370
    {
371
        $this->commentCount = $commentCount;
372
373
        return $this;
374
    }
375
376
    public function getComments()
377
    {
378
        return $this->comments;
379
    }
380
381
    public function setComments(array $comments)
382
    {
383
        $this->comments = $comments;
384
385
        return $this;
386
    }
387
388
    public function getLastEditDate()
389
    {
390
        return $this->lastEditDate;
391
    }
392
393
    public function setLastEditDate(\DateTimeInterface $lastEditDate = null)
394
    {
395
        $this->lastEditDate = $lastEditDate;
396
397
        return $this;
398
    }
399
400
    public function getLastEditor()
401
    {
402
        return $this->lastEditor;
403
    }
404
405
    public function setLastEditor(ShallowUser $lastEditor = null)
406
    {
407
        $this->lastEditor = $lastEditor;
408
409
        return $this;
410
    }
411
412
    public function getDownVoteCount()
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->downVoteCount;
415
    }
416
417
    public function setDownVoteCount($downVoteCount)
418
    {
419
        $this->downVoteCount = $downVoteCount;
420
421
        return $this;
422
    }
423
424
    public function getUpVoteCount()
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->upVoteCount;
427
    }
428
429
    public function setUpVoteCount($upVoteCount)
430
    {
431
        $this->upVoteCount = $upVoteCount;
432
433
        return $this;
434
    }
435
436
    public function getBody()
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->body;
439
    }
440
441
    public function setBody($body)
442
    {
443
        $this->body = $body;
444
445
        return $this;
446
    }
447
448
    public function getBodyMarkDown()
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...
449
    {
450
        return $this->bodyMarkDown;
451
    }
452
453
    public function setBodyMarkDown($bodyMarkDown)
454
    {
455
        $this->bodyMarkDown = $bodyMarkDown;
456
457
        return $this;
458
    }
459
460
    public function getCreationDate()
461
    {
462
        return $this->creationDate;
463
    }
464
465
    public function setCreationDate(\DateTimeInterface $creationDate)
466
    {
467
        $this->creationDate = $creationDate;
468
469
        return $this;
470
    }
471
472
    public function getLink()
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...
473
    {
474
        return $this->link;
475
    }
476
477
    public function setLink($link)
478
    {
479
        $this->link = $link;
480
481
        return $this;
482
    }
483
484
    public function getOwner()
485
    {
486
        return $this->owner;
487
    }
488
489
    public function setOwner(ShallowUser $owner = null)
490
    {
491
        $this->owner = $owner;
492
493
        return $this;
494
    }
495
496
    public function getScore()
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...
497
    {
498
        return $this->score;
499
    }
500
501
    public function setScore($score)
502
    {
503
        $this->score = $score;
504
505
        return $this;
506
    }
507
508
    public function getUpvoted()
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...
509
    {
510
        return $this->upvoted;
511
    }
512
513
    public function setUpvoted($upvoted)
514
    {
515
        $this->upvoted = $upvoted;
516
517
        return $this;
518
    }
519
}
520