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

Comment::setBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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