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

Comment   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 258
Duplicated Lines 3.1 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 43
c 7
b 0
f 2
lcom 1
cbo 1
dl 8
loc 258
rs 8.3158

29 Methods

Rating   Name   Duplication   Size   Complexity  
B fromProperties() 0 31 1
B fromJson() 0 18 14
B __construct() 0 29 1
A getId() 0 4 1
A setId() 0 6 1
A getBody() 0 4 1
A setBody() 0 6 1
A getBodyMarkdown() 0 4 1
A setBodyMarkdown() 0 6 1
A getCanFlag() 0 4 1
A setCanFlag() 0 6 1
A getCreationDate() 0 4 1
A setCreationDate() 0 6 1
A getEdited() 0 4 1
A setEdited() 0 6 1
A getLink() 0 4 1
A setLink() 0 6 1
A getOwner() 0 4 1
A setOwner() 0 6 1
A getPostId() 0 4 1
A setPostId() 0 6 1
A getPostType() 0 4 1
A setPostType() 8 8 2
A getReplyToUser() 0 4 1
A setReplyToUser() 0 6 1
A getScore() 0 4 1
A setScore() 0 6 1
A getUpvoted() 0 4 1
A setUpvoted() 0 6 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Comment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Comment, and based on these observations, apply Extract Interface, too.

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