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

NetworkUser   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 247
Duplicated Lines 9.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 44
c 5
b 0
f 2
lcom 1
cbo 2
dl 23
loc 247
rs 8.3396

27 Methods

Rating   Name   Duplication   Size   Complexity  
D fromJson() 10 30 17
A fromProperties() 0 15 1
B __construct() 0 28 1
A getId() 0 4 1
A setId() 0 6 1
A getAnswerCount() 0 4 1
A setAnswerCount() 0 6 1
A getBadgeCounts() 0 4 1
A setBadgeCounts() 0 6 1
A getCreationDate() 0 4 1
A setCreationDate() 0 6 1
A getLastAccessDate() 0 4 1
A setLastAccessDate() 0 6 1
A getQuestionCount() 0 4 1
A setQuestionCount() 0 6 1
A getReputation() 0 4 1
A setReputation() 0 6 1
A getSiteName() 0 4 1
A setSiteName() 0 6 1
A getSiteUrl() 0 4 1
A setSiteUrl() 0 6 1
A getTopAnswers() 0 4 1
A setTopAnswers() 0 6 1
A getTopQuestions() 0 4 1
A setTopQuestions() 0 6 1
A getUserType() 0 4 1
A setUserType() 13 13 2

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 NetworkUser 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 NetworkUser, 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 network user model class.
16
 *
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class NetworkUser
20
{
21
    const USER_TYPE_DOES_NOT_EXIST = 'does_not_exist';
22
    const USER_TYPE_MODERATOR = 'moderator';
23
    const USER_TYPE_REGISTERED = 'registered';
24
    const USER_TYPE_UNREGISTERED = 'unregistered';
25
26
    private $id;
27
    private $answerCount;
28
    private $badgeCounts;
29
    private $creationDate;
30
    private $lastAccessDate;
31
    private $questionCount;
32
    private $reputation;
33
    private $siteName;
34
    private $siteUrl;
35
    private $topAnswers;
36
    private $topQuestions;
37
    private $userType;
38
39
    public static function fromJson(array $data)
40
    {
41
        $topAnswers = [];
42
        $topQuestions = [];
43 View Code Duplication
        if (array_key_exists('top_answers', $data) && is_array($data['top_answers'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
            foreach ($data['top_answers'] as $topAnswer) {
45
                $topAnswers[] = NetworkPost::fromJson($topAnswer);
46
            }
47
        }
48 View Code Duplication
        if (array_key_exists('top_questions', $data) && is_array($data['top_questions'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
            foreach ($data['top_questions'] as $topQuestion) {
50
                $topQuestions[] = NetworkPost::fromJson($topQuestion);
51
            }
52
        }
53
54
        return new self(
55
            array_key_exists('user_id', $data) ? $data['user_id'] : null,
56
            array_key_exists('answer_count', $data) ? $data['answer_count'] : null,
57
            array_key_exists('badge_counts', $data) ? BadgeCount::fromJson($data['badge_counts']) : null,
58
            array_key_exists('creation_date', $data) ? new \DateTime('@' . $data['creation_date']) : null,
59
            array_key_exists('last_access_date', $data) ? new \DateTime('@' . $data['last_access_date']) : null,
60
            array_key_exists('question_count', $data) ? $data['question_count'] : null,
61
            array_key_exists('reputation', $data) ? $data['reputation'] : null,
62
            array_key_exists('site_name', $data) ? $data['site_name'] : null,
63
            array_key_exists('site_url', $data) ? $data['site_url'] : null,
64
            $topAnswers,
65
            $topQuestions,
66
            array_key_exists('user_type', $data) ? $data['user_type'] : null
67
        );
68
    }
69
70
    public static function fromProperties(
71
        $id,
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
        $answerCount,
0 ignored issues
show
Unused Code introduced by
The parameter $answerCount is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
        BadgeCount $badgeCounts,
0 ignored issues
show
Unused Code introduced by
The parameter $badgeCounts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
        \DateTime $creationDate,
0 ignored issues
show
Unused Code introduced by
The parameter $creationDate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
        \DateTime $lastAccessDate,
0 ignored issues
show
Unused Code introduced by
The parameter $lastAccessDate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
        $questionCount,
0 ignored issues
show
Unused Code introduced by
The parameter $questionCount is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
        $reputation,
0 ignored issues
show
Unused Code introduced by
The parameter $reputation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
        $siteName,
0 ignored issues
show
Unused Code introduced by
The parameter $siteName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
        $siteUrl,
0 ignored issues
show
Unused Code introduced by
The parameter $siteUrl is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
        array $topAnswers,
0 ignored issues
show
Unused Code introduced by
The parameter $topAnswers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
        array $topQuestions,
0 ignored issues
show
Unused Code introduced by
The parameter $topQuestions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
        $userType
0 ignored issues
show
Unused Code introduced by
The parameter $userType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    ) {
84
    }
85
86
    private function __construct(
87
        $id = null,
88
        $answerCount = null,
89
        BadgeCount $badgeCounts = null,
90
        \DateTime $creationDate = null,
91
        \DateTime $lastAccessDate = null,
92
        $questionCount = null,
93
        $reputation = null,
94
        $siteName = null,
95
        $siteUrl = null,
96
        array $topAnswers = [],
97
        array $topQuestions = [],
98
        $userType = null
99
    ) {
100
        $this->id = $id;
101
        $this->answerCount = $answerCount;
102
        $this->badgeCounts = $badgeCounts;
103
        $this->creationDate = $creationDate;
104
        $this->lastAccessDate = $lastAccessDate;
105
        $this->questionCount = $questionCount;
106
        $this->reputation = $reputation;
107
        $this->siteName = $siteName;
108
        $this->siteUrl = $siteUrl;
109
        $this->topAnswers = $topAnswers;
110
        $this->topQuestions = $topQuestions;
111
        $this->userType = $userType;
112
        $this->setUserType($userType);
113
    }
114
115
    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...
116
    {
117
        return $this->id;
118
    }
119
120
    public function setId($id)
121
    {
122
        $this->id = $id;
123
124
        return $this;
125
    }
126
127
    public function getAnswerCount()
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...
128
    {
129
        return $this->answerCount;
130
    }
131
132
    public function setAnswerCount($answerCount)
133
    {
134
        $this->answerCount = $answerCount;
135
136
        return $this;
137
    }
138
139
    public function getBadgeCounts()
140
    {
141
        return $this->badgeCounts;
142
    }
143
144
    public function setBadgeCounts(BadgeCount $badgeCounts)
145
    {
146
        $this->badgeCounts = $badgeCounts;
147
148
        return $this;
149
    }
150
151
    public function getCreationDate()
152
    {
153
        return $this->creationDate;
154
    }
155
156
    public function setCreationDate(\DateTime $creationDate)
157
    {
158
        $this->creationDate = $creationDate;
159
160
        return $this;
161
    }
162
163
    public function getLastAccessDate()
164
    {
165
        return $this->lastAccessDate;
166
    }
167
168
    public function setLastAccessDate(\DateTime $lastAccessDate)
169
    {
170
        $this->lastAccessDate = $lastAccessDate;
171
172
        return $this;
173
    }
174
175
    public function getQuestionCount()
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...
176
    {
177
        return $this->questionCount;
178
    }
179
180
    public function setQuestionCount($questionCount)
181
    {
182
        $this->questionCount = $questionCount;
183
184
        return $this;
185
    }
186
187
    public function getReputation()
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...
188
    {
189
        return $this->reputation;
190
    }
191
192
    public function setReputation($reputation)
193
    {
194
        $this->reputation = $reputation;
195
196
        return $this;
197
    }
198
199
    public function getSiteName()
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...
200
    {
201
        return $this->siteName;
202
    }
203
204
    public function setSiteName($siteName)
205
    {
206
        $this->siteName = $siteName;
207
208
        return $this;
209
    }
210
211
    public function getSiteUrl()
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...
212
    {
213
        return $this->siteUrl;
214
    }
215
216
    public function setSiteUrl($siteUrl)
217
    {
218
        $this->siteUrl = $siteUrl;
219
220
        return $this;
221
    }
222
223
    public function getTopAnswers()
224
    {
225
        return $this->topAnswers;
226
    }
227
228
    public function setTopAnswers($topAnswers)
229
    {
230
        $this->topAnswers = $topAnswers;
231
232
        return $this;
233
    }
234
235
    public function getTopQuestions()
236
    {
237
        return $this->topQuestions;
238
    }
239
240
    public function setTopQuestions($topQuestions)
241
    {
242
        $this->topQuestions = $topQuestions;
243
244
        return $this;
245
    }
246
247
    public function getUserType()
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...
248
    {
249
        return $this->userType;
250
    }
251
252 View Code Duplication
    public function setUserType($userType)
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...
253
    {
254
        if (in_array($userType, [
255
            self::USER_TYPE_DOES_NOT_EXIST,
256
            self::USER_TYPE_MODERATOR,
257
            self::USER_TYPE_REGISTERED,
258
            self::USER_TYPE_UNREGISTERED,
259
        ])) {
260
            $this->userType = $userType;
261
        }
262
263
        return $this;
264
    }
265
}
266