Completed
Push — v2 ( 507181...9b4b78 )
by Beñat
02:57
created

Post::getUpvoted()   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 post model class.
16
 *
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class Post implements Model
20
{
21
    const POST_TYPES = [
22
        'question',
23
        'answer',
24
    ];
25
26
    protected $postId;
27
    protected $postType;
28
    protected $downvoted;
29
    protected $lastActivityDate;
30
    protected $shareLink;
31
    protected $title;
32
    protected $commentCount;
33
    protected $comments;
34
    protected $lastEditDate;
35
    protected $lastEditor;
36
    protected $downVoteCount;
37
    protected $upVoteCount;
38
    protected $creationDate;
39
    protected $link;
40
    protected $owner;
41
    protected $score;
42
    protected $body;
43
    protected $bodyMarkdown;
44
    protected $upvoted;
45
46
    public static function fromJson(array $data)
47
    {
48
        $comments = [];
49 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...
50
            foreach ($data['comments'] as $comment) {
51
                $comments[] = Comment::fromJson($comment);
52
            }
53
        }
54
55
        $instance = new self();
56
        $instance
57
            ->setPostId(array_key_exists('post_id', $data) ? $data['post_id'] : null)
58
            ->setPostType(array_key_exists('post_type', $data) ? $data['post_type'] : null)
59
            ->setCreationDate(
60
                array_key_exists('creation_date', $data)
61
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
62
                    : null
63
            )
64
            ->setLastActivityDate(
65
                array_key_exists('last_activity_date', $data)
66
                    ? new \DateTimeImmutable('@' . $data['last_activity_date'])
67
                    : null
68
            )
69
            ->setLastEditDate(
70
                array_key_exists('last_edit_date', $data)
71
                    ? new \DateTimeImmutable('@' . $data['last_edit_date'])
72
                    : null
73
            )
74
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
75
            ->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...\Model\Post::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...
76
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
77
            ->setBody(array_key_exists('body', $data) ? $data['body'] : null)
78
            ->setBodyMarkdown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null)
79
            ->setShareLink(array_key_exists('share_link', $data) ? $data['share_link'] : null)
80
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null)
81
            ->setCommentCount(array_key_exists('comment_count', $data) ? $data['comment_count'] : null)
82
            ->setComments($comments)
83
            ->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...l\Post::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...
84
            ->setDownvoted(array_key_exists('downvoted', $data) ? $data['downvoted'] : null)
85
            ->setDownVoteCount(array_key_exists('down_vote_count', $data) ? $data['down_vote_count'] : null)
86
            ->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null)
87
            ->setUpVoteCount(array_key_exists('up_vote_count', $data) ? $data['up_vote_count'] : null);
88
89
        return $instance;
90
    }
91
92
    public static function fromProperties(
93
        $postId,
94
        $postType,
95
        \DateTimeInterface $creationDate,
96
        \DateTimeInterface $lastActivityDate,
97
        \DateTimeInterface $lastEditDate,
98
        $link,
99
        ShallowUser $owner,
100
        $score,
101
        $body = null,
102
        $bodyMarkdown = null,
103
        $shareLink = null,
104
        $title = null,
105
        $commentCount = null,
106
        array $comments = [],
107
        $lastEditDate = null,
0 ignored issues
show
Bug introduced by
The parameter $lastEditDate is used multiple times.
Loading history...
Coding Style introduced by
Variable "$lastEditDate" appears more than once in function declaration
Loading history...
108
        $lastEditor = null,
109
        $downvoted = null,
110
        $downVoteCount = null,
111
        $upvoted = null,
112
        $upVoteCount = null
113
    ) {
114
        $instance = new self();
115
        $instance
116
            ->setPostId($postId)
117
            ->setPostType($postType)
118
            ->setCreationDate($creationDate)
119
            ->setLastActivityDate($lastActivityDate)
120
            ->setLastEditDate($lastEditDate)
121
            ->setLink($link)
122
            ->setOwner($owner)
123
            ->setScore($score)
124
            ->setBody($body)
125
            ->setBodyMarkdown($bodyMarkdown)
126
            ->setShareLink($shareLink)
127
            ->setTitle($title)
128
            ->setCommentCount($commentCount)
129
            ->setComments($comments)
130
            ->setLastEditor($lastEditor)
131
            ->setDownvoted($downvoted)
132
            ->setDownVoteCount($downVoteCount)
133
            ->setUpvoted($upvoted)
134
            ->setUpVoteCount($upVoteCount);
135
136
        return $instance;
137
    }
138
139
    public function getPostId()
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...
140
    {
141
        return $this->postId;
142
    }
143
144
    public function setPostId($postId)
145
    {
146
        $this->postId = $postId;
147
148
        return $this;
149
    }
150
151
    public function getCreationDate()
152
    {
153
        return $this->creationDate;
154
    }
155
156
    public function setCreationDate(\DateTimeInterface $creationDate = null)
157
    {
158
        $this->creationDate = $creationDate;
159
160
        return $this;
161
    }
162
163
    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...
164
    {
165
        return $this->link;
166
    }
167
168
    public function setLink($link)
169
    {
170
        $this->link = $link;
171
172
        return $this;
173
    }
174
175
    public function getOwner()
176
    {
177
        return $this->owner;
178
    }
179
180
    public function setOwner(ShallowUser $owner = null)
181
    {
182
        $this->owner = $owner;
183
184
        return $this;
185
    }
186
187
    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...
188
    {
189
        return $this->score;
190
    }
191
192
    public function setScore($score)
193
    {
194
        $this->score = $score;
195
196
        return $this;
197
    }
198
199
    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...
200
    {
201
        return $this->body;
202
    }
203
204
    public function setBody($body)
205
    {
206
        $this->body = $body;
207
208
        return $this;
209
    }
210
211
    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...
212
    {
213
        return $this->bodyMarkdown;
214
    }
215
216
    public function setBodyMarkdown($bodyMarkdown)
217
    {
218
        $this->bodyMarkdown = $bodyMarkdown;
219
220
        return $this;
221
    }
222
223
    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...
224
    {
225
        return $this->upvoted;
226
    }
227
228
    public function setUpvoted($upvoted)
229
    {
230
        $this->upvoted = $upvoted;
231
232
        return $this;
233
    }
234
235
    public function setPostType($postType)
236
    {
237
        if (in_array($postType, self::POST_TYPES, true)) {
238
            $this->postType = $postType;
239
        }
240
241
        return $this;
242
    }
243
244
    public function getPostType()
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->postType;
247
    }
248
249
    public function setDownvoted($downvoted)
250
    {
251
        $this->downvoted = $downvoted;
252
253
        return $this;
254
    }
255
256
    public function isDownvoted()
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->downvoted;
259
    }
260
261
    public function setLastActivityDate(\DateTimeInterface $lastActivityDate = null)
262
    {
263
        $this->lastActivityDate = $lastActivityDate;
264
265
        return $this;
266
    }
267
268
    public function getLastActivityDate()
269
    {
270
        return $this->lastActivityDate;
271
    }
272
273
    public function setShareLink($shareLink)
274
    {
275
        $this->shareLink = $shareLink;
276
277
        return $this;
278
    }
279
280
    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...
281
    {
282
        return $this->shareLink;
283
    }
284
285
    public function setTitle($title)
286
    {
287
        $this->title = $title;
288
289
        return $this;
290
    }
291
292
    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...
293
    {
294
        return $this->title;
295
    }
296
297
    public function setCommentCount($commentCount)
298
    {
299
        $this->commentCount = $commentCount;
300
301
        return $this;
302
    }
303
304
    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...
305
    {
306
        return $this->commentCount;
307
    }
308
309
    public function setComments(array $comments = [])
310
    {
311
        $this->comments = $comments;
312
313
        return $this;
314
    }
315
316
    public function getComments()
317
    {
318
        return $this->comments;
319
    }
320
321
    public function setLastEditDate(\DateTimeInterface $lastEditDate = null)
322
    {
323
        $this->lastEditDate = $lastEditDate;
324
325
        return $this;
326
    }
327
328
    public function getLastEditDate()
329
    {
330
        return $this->lastEditDate;
331
    }
332
333
    public function setLastEditor(ShallowUser $lastEditor = null)
334
    {
335
        $this->lastEditor = $lastEditor;
336
337
        return $this;
338
    }
339
340
    public function getLastEditor()
341
    {
342
        return $this->lastEditor;
343
    }
344
345
    public function setDownVoteCount($downVoteCount)
346
    {
347
        $this->downVoteCount = $downVoteCount;
348
349
        return $this;
350
    }
351
352
    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...
353
    {
354
        return $this->downVoteCount;
355
    }
356
357
    public function setUpVoteCount($upVoteCount)
358
    {
359
        $this->upVoteCount = $upVoteCount;
360
361
        return $this;
362
    }
363
364
    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...
365
    {
366
        return $this->upVoteCount;
367
    }
368
}
369