Completed
Push — v2 ( 0c2cc9...f96eb8 )
by Beñat
05:19
created

Comment::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 1
eloc 27
nc 1
nop 13

How to fix   Many Parameters   

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