Completed
Push — master ( f5580e...4e6c29 )
by Beñat
01:39
created

Comment::getPostId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
 * (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 comment model class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class Comment implements Model
30
{
31
    const POST_TYPES = ['question', 'answer'];
32
33
    protected $id;
34
    protected $body;
35
    protected $bodyMarkdown;
36
    protected $canFlag;
37
    protected $creationDate;
38
    protected $edited;
39
    protected $link;
40
    protected $owner;
41
    protected $postId;
42
    protected $postType;
43
    protected $replyToUser;
44
    protected $score;
45
    protected $upvoted;
46
47
    public static function fromProperties(
48
        $id,
49
        $body,
50
        $bodyMarkdown,
51
        $canFlag,
52
        \DateTimeInterface $creationDate,
53
        $edited,
54
        $link,
55
        $postId,
56
        $postType,
57
        $score,
58
        $upvoted,
59
        ShallowUser $owner = null,
60
        ShallowUser $replyToUser = null
61
    ) {
62
        $instance = new self();
63
        $instance
64
            ->setId($id)
65
            ->setBody($body)
66
            ->setBodyMarkdown($bodyMarkdown)
67
            ->setCanFlag($canFlag)
68
            ->setCreationDate($creationDate)
69
            ->setEdited($edited)
70
            ->setLink($link)
71
            ->setOwner($owner)
72
            ->setPostId($postId)
73
            ->setPostType($postType)
74
            ->setReplyToUser($replyToUser)
75
            ->setScore($score)
76
            ->setUpvoted($upvoted);
77
78
        return $instance;
79
    }
80
81
    public static function fromJson(array $data)
82
    {
83
        $instance = new self();
84
        $instance
85
            ->setId(array_key_exists('comment_id', $data) ? $data['comment_id'] : null)
86
            ->setBody(array_key_exists('body', $data) ? $data['body'] : null)
87
            ->setBodyMarkdown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null)
88
            ->setCanFlag(array_key_exists('can_flag', $data) ? $data['can_flag'] : null)
89
            ->setCreationDate(
90
                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...
91
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
92
                    : null
93
            )
94
            ->setEdited(array_key_exists('edited', $data) ? $data['edited'] : null)
95
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
96
            ->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...del\Comment::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...
97
            ->setPostId(array_key_exists('postId', $data) ? $data['postId'] : null)
98
            ->setPostType(array_key_exists('post_type', $data) ? $data['post_type'] : null)
99
            ->setReplyToUser(array_key_exists(
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('reply_...reply_to_user']) : null can also be of type object<BenatEspina\Stack...eApiClient\Model\Model>; however, BenatEspina\StackExchang...mment::setReplyToUser() 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...
100
                'reply_to_user', $data)
101
                ? ShallowUser::fromJson($data['reply_to_user'])
102
                : null
103
            )
104
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
105
            ->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null);
106
107
        return $instance;
108
    }
109
110
    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...
111
    {
112
        return $this->id;
113
    }
114
115
    public function setId($id)
116
    {
117
        $this->id = $id;
118
119
        return $this;
120
    }
121
122
    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...
123
    {
124
        return $this->body;
125
    }
126
127
    public function setBody($body)
128
    {
129
        $this->body = $body;
130
131
        return $this;
132
    }
133
134
    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...
135
    {
136
        return $this->bodyMarkdown;
137
    }
138
139
    public function setBodyMarkdown($bodyMarkdown)
140
    {
141
        $this->bodyMarkdown = $bodyMarkdown;
142
143
        return $this;
144
    }
145
146
    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...
147
    {
148
        return $this->canFlag;
149
    }
150
151
    public function setCanFlag($canFlag)
152
    {
153
        $this->canFlag = $canFlag;
154
155
        return $this;
156
    }
157
158
    public function getCreationDate()
159
    {
160
        return $this->creationDate;
161
    }
162
163
    public function setCreationDate(\DateTimeInterface $creationDate)
164
    {
165
        $this->creationDate = $creationDate;
166
167
        return $this;
168
    }
169
170
    public function getEdited()
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...
171
    {
172
        return $this->edited;
173
    }
174
175
    public function setEdited($edited)
176
    {
177
        $this->edited = $edited;
178
179
        return $this;
180
    }
181
182
    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...
183
    {
184
        return $this->link;
185
    }
186
187
    public function setLink($link)
188
    {
189
        $this->link = $link;
190
191
        return $this;
192
    }
193
194
    public function getOwner()
195
    {
196
        return $this->owner;
197
    }
198
199
    public function setOwner(ShallowUser $owner = null)
200
    {
201
        $this->owner = $owner;
202
203
        return $this;
204
    }
205
206
    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...
207
    {
208
        return $this->postId;
209
    }
210
211
    public function setPostId($postId)
212
    {
213
        $this->postId = $postId;
214
215
        return $this;
216
    }
217
218
    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...
219
    {
220
        return $this->postType;
221
    }
222
223
    public function setPostType($postType)
224
    {
225
        if (in_array($postType, self::POST_TYPES, true)) {
226
            $this->postType = $postType;
227
        }
228
229
        return $this;
230
    }
231
232
    public function getReplyToUser()
233
    {
234
        return $this->replyToUser;
235
    }
236
237
    public function setReplyToUser(ShallowUser $replyToUser = null)
238
    {
239
        $this->replyToUser = $replyToUser;
240
241
        return $this;
242
    }
243
244
    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...
245
    {
246
        return $this->score;
247
    }
248
249
    public function setScore($score)
250
    {
251
        $this->score = $score;
252
253
        return $this;
254
    }
255
256
    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...
257
    {
258
        return $this->upvoted;
259
    }
260
261
    public function setUpvoted($upvoted)
262
    {
263
        $this->upvoted = $upvoted;
264
265
        return $this;
266
    }
267
}
268