Completed
Push — v2 ( e6c7b3...40717e )
by Beñat
06:35
created

Answer::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 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
namespace BenatEspina\StackExchangeApiClient\Model;
13
14
/**
15
 * The answer model class.
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 fromProperties(
50
        $id,
51
        $accepted,
52
        $canFlag,
53
        $isAccepted,
54
        $questionId,
55
        array $tags,
56
        $downvoted,
57
        \DateTimeInterface $lastActivityDate,
58
        $shareLink,
59
        $title,
60
        $commentCount,
61
        $downVoteCount,
62
        $upVoteCount,
63
        $body,
64
        $bodyMarkDown,
65
        \DateTimeInterface $creationDate,
66
        $link,
67
        $score,
68
        $upvoted,
69
        $awardedBountyAmount,
70
        array $awardedBountyUsers = [],
71
        array $comments = [],
72
        \DateTimeInterface $communityOwnedDate = null,
73
        ShallowUser $lastEditor = null,
74
        \DateTimeInterface $lastEditDate = null,
75
        \DateTimeInterface $lockedDate = null,
76
        ShallowUser $owner = null
77
    ) {
78
        $instance = new self();
79
        $instance
80
            ->setId($id)
81
            ->setAccepted($accepted)
82
            ->setCanFlag($canFlag)
83
            ->setIsAccepted($isAccepted)
84
            ->setQuestionId($questionId)
85
            ->setTags($tags)
86
            ->setDownvoted($downvoted)
87
            ->setLastActivityDate($lastActivityDate)
88
            ->setShareLink($shareLink)
89
            ->setTitle($title)
90
            ->setCommentCount($commentCount)
91
            ->setDownVoteCount($downVoteCount)
92
            ->setUpVoteCount($upVoteCount)
93
            ->setBody($body)
94
            ->setBodyMarkDown($bodyMarkDown)
95
            ->setCreationDate($creationDate)
96
            ->setLink($link)
97
            ->setScore($score)
98
            ->setUpvoted($upvoted)
99
            ->setAwardedBountyAmount($awardedBountyAmount)
100
            ->setAwardedBountyUsers($awardedBountyUsers)
101
            ->setComments($comments)
102
            ->setCommunityOwnedDate($communityOwnedDate)
103
            ->setLastEditor($lastEditor)
104
            ->setLastEditDate($lastEditDate)
105
            ->setLockedDate($lockedDate)
106
            ->setOwner($owner);
107
108
        return $instance;
109
    }
110
111
    public static function fromJson(array $data)
112
    {
113
        $tags = [];
114
        $awardedBountyUsers = [];
115
        $comments = [];
116
117 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...
118
            foreach ($data['tags'] as $tag) {
119
                $tags[] = Tag::fromJson($tag);
120
            }
121
        }
122 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...
123
            foreach ($data['awarded_bounty_users'] as $awardedBountyUser) {
124
                $awardedBountyUsers[] = ShallowUser::fromJson($awardedBountyUser);
125
            }
126
        }
127 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...
128
            foreach ($data['comments'] as $comment) {
129
                $comments[] = Comment::fromJson($comment);
130
            }
131
        }
132
133
        $instance = new self();
134
        $instance
135
            ->setId(array_key_exists('answer_id', $data) ? $data['answer_id'] : null)
136
            ->setAccepted(array_key_exists('accepted', $data) ? $data['accepted'] : null)
137
            ->setCanFlag(array_key_exists('can_flag', $data) ? $data['can_flag'] : null)
138
            ->setIsAccepted(array_key_exists('is_accepted', $data) ? $data['is_accepted'] : null)
139
            ->setQuestionId(array_key_exists('question_id', $data) ? $data['question_id'] : null)
140
            ->setTags($tags)
141
            ->setDownvoted(array_key_exists('downvoted', $data) ? $data['downvoted'] : null)
142
            ->setLastActivityDate(
143
                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...
144
                    ? new \DateTimeImmutable('@' . $data['last_activity_date'])
145
                    : null
146
            )
147
            ->setShareLink(array_key_exists('share_link', $data) ? $data['share_link'] : null)
148
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null)
149
            ->setCommentCount(array_key_exists('comment_count', $data) ? $data['comment_count'] : null)
150
            ->setDownVoteCount(array_key_exists('down_vote_count', $data) ? $data['down_vote_count'] : null)
151
            ->setUpVoteCount(array_key_exists('up_vote_count', $data) ? $data['up_vote_count'] : null)
152
            ->setBody(array_key_exists('body', $data) ? $data['body'] : null)
153
            ->setBodyMarkDown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null)
154
            ->setCreationDate(
155
                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...
156
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
157
                    : null
158
            )
159
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
160
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
161
            ->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null)
162
            ->setAwardedBountyAmount(
163
                array_key_exists('awarded_bounty_amount', $data)
164
                    ? $data['awarded_bounty_amount']
165
                    : null
166
            )
167
            ->setAwardedBountyUsers($awardedBountyUsers)
168
            ->setComments($comments)
169
            ->setCommunityOwnedDate(
170
                array_key_exists('community_owned_date', $data)
171
                    ? new \DateTimeImmutable('@' . $data['community_owned_date'])
172
                    : null
173
            )
174
            ->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...
175
            ->setLastEditDate(
176
                array_key_exists('last_edit_date', $data)
177
                    ? new \DateTimeImmutable('@' . $data['last_edit_date'])
178
                    : null
179
            )
180
            ->setLockedDate(array_key_exists('locked_date', $data) ? new \DateTime('@' . $data['locked_date']) : null)
181
            ->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...
182
183
        return $instance;
184
    }
185
186
    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...
187
    {
188
        return $this->id;
189
    }
190
191
    public function setId($id)
192
    {
193
        $this->id = $id;
194
195
        return $this;
196
    }
197
198
    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...
199
    {
200
        return $this->accepted;
201
    }
202
203
    public function setAccepted($accepted)
204
    {
205
        $this->accepted = $accepted;
206
207
        return $this;
208
    }
209
210
    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...
211
    {
212
        return $this->awardedBountyAmount;
213
    }
214
215
    public function setAwardedBountyAmount($awardedBountyAmount)
216
    {
217
        $this->awardedBountyAmount = $awardedBountyAmount;
218
219
        return $this;
220
    }
221
222
    public function getAwardedBountyUsers()
223
    {
224
        return $this->awardedBountyUsers;
225
    }
226
227
    public function setAwardedBountyUsers(array $awardedBountyUsers)
228
    {
229
        $this->awardedBountyUsers = $awardedBountyUsers;
230
231
        return $this;
232
    }
233
234
    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...
235
    {
236
        return $this->canFlag;
237
    }
238
239
    public function setCanFlag($canFlag)
240
    {
241
        $this->canFlag = $canFlag;
242
243
        return $this;
244
    }
245
246
    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...
247
    {
248
        return $this->isAccepted;
249
    }
250
251
    public function setIsAccepted($isAccepted)
252
    {
253
        $this->isAccepted = $isAccepted;
254
255
        return $this;
256
    }
257
258
    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...
259
    {
260
        return $this->questionId;
261
    }
262
263
    public function setQuestionId($questionId)
264
    {
265
        $this->questionId = $questionId;
266
267
        return $this;
268
    }
269
270
    public function getCommunityOwnedDate()
271
    {
272
        return $this->communityOwnedDate;
273
    }
274
275
    public function setCommunityOwnedDate(\DateTimeInterface $communityOwnedDate = null)
276
    {
277
        $this->communityOwnedDate = $communityOwnedDate;
278
279
        return $this;
280
    }
281
282
    public function getLockedDate()
283
    {
284
        return $this->lockedDate;
285
    }
286
287
    public function setLockedDate(\DateTimeInterface $lockedDate = null)
288
    {
289
        $this->lockedDate = $lockedDate;
290
291
        return $this;
292
    }
293
294
    public function getTags()
295
    {
296
        return $this->tags;
297
    }
298
299
    public function setTags(array $tags)
300
    {
301
        $this->tags = $tags;
302
303
        return $this;
304
    }
305
306
    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...
307
    {
308
        return $this->downvoted;
309
    }
310
311
    public function setDownvoted($downvoted)
312
    {
313
        $this->downvoted = $downvoted;
314
315
        return $this;
316
    }
317
318
    public function getLastActivityDate()
319
    {
320
        return $this->lastActivityDate;
321
    }
322
323
    public function setLastActivityDate(\DateTimeInterface $lastActivityDate)
324
    {
325
        $this->lastActivityDate = $lastActivityDate;
326
327
        return $this;
328
    }
329
330
    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...
331
    {
332
        return $this->shareLink;
333
    }
334
335
    public function setShareLink($shareLink)
336
    {
337
        $this->shareLink = $shareLink;
338
339
        return $this;
340
    }
341
342
    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...
343
    {
344
        return $this->title;
345
    }
346
347
    public function setTitle($title)
348
    {
349
        $this->title = $title;
350
351
        return $this;
352
    }
353
354
    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...
355
    {
356
        return $this->commentCount;
357
    }
358
359
    public function setCommentCount($commentCount)
360
    {
361
        $this->commentCount = $commentCount;
362
363
        return $this;
364
    }
365
366
    public function getComments()
367
    {
368
        return $this->comments;
369
    }
370
371
    public function setComments(array $comments)
372
    {
373
        $this->comments = $comments;
374
375
        return $this;
376
    }
377
378
    public function getLastEditDate()
379
    {
380
        return $this->lastEditDate;
381
    }
382
383
    public function setLastEditDate(\DateTimeInterface $lastEditDate = null)
384
    {
385
        $this->lastEditDate = $lastEditDate;
386
387
        return $this;
388
    }
389
390
    public function getLastEditor()
391
    {
392
        return $this->lastEditor;
393
    }
394
395
    public function setLastEditor(ShallowUser $lastEditor = null)
396
    {
397
        $this->lastEditor = $lastEditor;
398
399
        return $this;
400
    }
401
402
    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...
403
    {
404
        return $this->downVoteCount;
405
    }
406
407
    public function setDownVoteCount($downVoteCount)
408
    {
409
        $this->downVoteCount = $downVoteCount;
410
411
        return $this;
412
    }
413
414
    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...
415
    {
416
        return $this->upVoteCount;
417
    }
418
419
    public function setUpVoteCount($upVoteCount)
420
    {
421
        $this->upVoteCount = $upVoteCount;
422
423
        return $this;
424
    }
425
426
    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...
427
    {
428
        return $this->body;
429
    }
430
431
    public function setBody($body)
432
    {
433
        $this->body = $body;
434
435
        return $this;
436
    }
437
438
    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...
439
    {
440
        return $this->bodyMarkDown;
441
    }
442
443
    public function setBodyMarkDown($bodyMarkDown)
444
    {
445
        $this->bodyMarkDown = $bodyMarkDown;
446
447
        return $this;
448
    }
449
450
    public function getCreationDate()
451
    {
452
        return $this->creationDate;
453
    }
454
455
    public function setCreationDate(\DateTimeInterface $creationDate)
456
    {
457
        $this->creationDate = $creationDate;
458
459
        return $this;
460
    }
461
462
    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...
463
    {
464
        return $this->link;
465
    }
466
467
    public function setLink($link)
468
    {
469
        $this->link = $link;
470
471
        return $this;
472
    }
473
474
    public function getOwner()
475
    {
476
        return $this->owner;
477
    }
478
479
    public function setOwner(ShallowUser $owner = null)
480
    {
481
        $this->owner = $owner;
482
483
        return $this;
484
    }
485
486
    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...
487
    {
488
        return $this->score;
489
    }
490
491
    public function setScore($score)
492
    {
493
        $this->score = $score;
494
495
        return $this;
496
    }
497
498
    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...
499
    {
500
        return $this->upvoted;
501
    }
502
503
    public function setUpvoted($upvoted)
504
    {
505
        $this->upvoted = $upvoted;
506
507
        return $this;
508
    }
509
}
510