NetworkUser   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 238
Duplicated Lines 4.2 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 43
lcom 1
cbo 2
dl 10
loc 238
rs 8.3157
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
D fromJson() 10 42 17
B fromProperties() 0 31 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() 0 8 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
 * (c) 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
declare(strict_types=1);
13
/*
14
 * This file is part of the Stack Exchange Api Client library.
15
 *
16
 * (c) Beñat Espiña <[email protected]>
17
 *
18
 * For the full copyright and license information, please view the LICENSE
19
 * file that was distributed with this source code.
20
 */
21
22
namespace BenatEspina\StackExchangeApiClient\Model;
23
24
/**
25
 * The network user model class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class NetworkUser implements Model
30
{
31
    const USER_TYPES = ['does_not_exist', 'moderator', 'registered', 'unregistered'];
32
33
    protected $id;
34
    protected $answerCount;
35
    protected $badgeCounts;
36
    protected $creationDate;
37
    protected $lastAccessDate;
38
    protected $questionCount;
39
    protected $reputation;
40
    protected $siteName;
41
    protected $siteUrl;
42
    protected $topAnswers;
43
    protected $topQuestions;
44
    protected $userType;
45
46
    public static function fromJson(array $data)
47
    {
48
        $topAnswers = [];
49
        $topQuestions = [];
50 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...
51
            foreach ($data['top_answers'] as $topAnswer) {
52
                $topAnswers[] = NetworkPost::fromJson($topAnswer);
53
            }
54
        }
55 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...
56
            foreach ($data['top_questions'] as $topQuestion) {
57
                $topQuestions[] = NetworkPost::fromJson($topQuestion);
58
            }
59
        }
60
61
        $instance = new self();
62
        $instance
63
            ->setId(array_key_exists('user_id', $data) ? $data['user_id'] : null)
64
            ->setAnswerCount(array_key_exists('answer_count', $data) ? $data['answer_count'] : null)
65
            ->setBadgeCounts(
66
                array_key_exists('badge_counts', $data)
67
                    ? BadgeCount::fromJson($data['badge_counts'])
68
                    : null
69
            )
70
            ->setCreationDate(array_key_exists('creation_date', $data)
71
                ? new \DateTimeImmutable('@' . $data['creation_date'])
72
                : null
73
            )
74
            ->setLastAccessDate(array_key_exists('last_access_date', $data)
75
                ? new \DateTimeImmutable('@' . $data['last_access_date'])
76
                : null
77
            )
78
            ->setQuestionCount(array_key_exists('question_count', $data) ? $data['question_count'] : null)
79
            ->setQuestionCount(array_key_exists('reputation', $data) ? $data['reputation'] : null)
80
            ->setQuestionCount(array_key_exists('site_name', $data) ? $data['site_name'] : null)
81
            ->setQuestionCount(array_key_exists('site_url', $data) ? $data['site_url'] : null)
82
            ->setTopAnswers($topAnswers)
83
            ->setTopQuestions($topQuestions)
84
            ->setUserType(array_key_exists('user_type', $data) ? $data['user_type'] : null);
85
86
        return $instance;
87
    }
88
89
    public static function fromProperties(
90
        $id,
91
        $answerCount,
92
        BadgeCount $badgeCounts,
93
        \DateTimeInterface $creationDate,
94
        \DateTimeInterface $lastAccessDate,
95
        $questionCount,
96
        $reputation,
97
        $siteName,
98
        $siteUrl,
99
        array $topAnswers,
100
        array $topQuestions,
101
        $userType
102
    ) {
103
        $instance = new self();
104
        $instance
105
            ->setId($id)
106
            ->setAnswerCount($answerCount)
107
            ->setBadgeCounts($badgeCounts)
108
            ->setCreationDate($creationDate)
109
            ->setLastAccessDate($lastAccessDate)
110
            ->setQuestionCount($questionCount)
111
            ->setReputation($reputation)
112
            ->setSiteName($siteName)
113
            ->setSiteUrl($siteUrl)
114
            ->setTopAnswers($topAnswers)
115
            ->setTopQuestions($topQuestions)
116
            ->setUserType($userType);
117
118
        return $instance;
119
    }
120
121
    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...
122
    {
123
        return $this->id;
124
    }
125
126
    public function setId($id)
127
    {
128
        $this->id = $id;
129
130
        return $this;
131
    }
132
133
    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...
134
    {
135
        return $this->answerCount;
136
    }
137
138
    public function setAnswerCount($answerCount)
139
    {
140
        $this->answerCount = $answerCount;
141
142
        return $this;
143
    }
144
145
    public function getBadgeCounts()
146
    {
147
        return $this->badgeCounts;
148
    }
149
150
    public function setBadgeCounts(BadgeCount $badgeCounts = null)
151
    {
152
        $this->badgeCounts = $badgeCounts;
153
154
        return $this;
155
    }
156
157
    public function getCreationDate()
158
    {
159
        return $this->creationDate;
160
    }
161
162
    public function setCreationDate(\DateTimeInterface $creationDate = null)
163
    {
164
        $this->creationDate = $creationDate;
165
166
        return $this;
167
    }
168
169
    public function getLastAccessDate()
170
    {
171
        return $this->lastAccessDate;
172
    }
173
174
    public function setLastAccessDate(\DateTimeInterface $lastAccessDate = null)
175
    {
176
        $this->lastAccessDate = $lastAccessDate;
177
178
        return $this;
179
    }
180
181
    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...
182
    {
183
        return $this->questionCount;
184
    }
185
186
    public function setQuestionCount($questionCount)
187
    {
188
        $this->questionCount = $questionCount;
189
190
        return $this;
191
    }
192
193
    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...
194
    {
195
        return $this->reputation;
196
    }
197
198
    public function setReputation($reputation)
199
    {
200
        $this->reputation = $reputation;
201
202
        return $this;
203
    }
204
205
    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...
206
    {
207
        return $this->siteName;
208
    }
209
210
    public function setSiteName($siteName)
211
    {
212
        $this->siteName = $siteName;
213
214
        return $this;
215
    }
216
217
    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...
218
    {
219
        return $this->siteUrl;
220
    }
221
222
    public function setSiteUrl($siteUrl)
223
    {
224
        $this->siteUrl = $siteUrl;
225
226
        return $this;
227
    }
228
229
    public function getTopAnswers()
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...
230
    {
231
        return $this->topAnswers;
232
    }
233
234
    public function setTopAnswers($topAnswers)
235
    {
236
        $this->topAnswers = $topAnswers;
237
238
        return $this;
239
    }
240
241
    public function getTopQuestions()
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...
242
    {
243
        return $this->topQuestions;
244
    }
245
246
    public function setTopQuestions($topQuestions)
247
    {
248
        $this->topQuestions = $topQuestions;
249
250
        return $this;
251
    }
252
253
    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...
254
    {
255
        return $this->userType;
256
    }
257
258
    public function setUserType($userType)
259
    {
260
        if (in_array($userType, self::USER_TYPES, true)) {
261
            $this->userType = $userType;
262
        }
263
264
        return $this;
265
    }
266
}
267