Completed
Push — v3 ( d12fea )
by Beñat
05:39
created

AnemicComment   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 258
Duplicated Lines 3.1 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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