Completed
Push — v2 ( b8f0e3...507181 )
by Beñat
02:46
created

Info   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 44
c 4
b 0
f 2
lcom 1
cbo 1
dl 0
loc 259
rs 8.3396

30 Methods

Rating   Name   Duplication   Size   Complexity  
C fromJson() 0 37 15
B fromProperties() 0 35 1
A setTotalAccepted() 0 6 1
A getTotalAccepted() 0 4 1
A setTotalAnswers() 0 6 1
A getTotalAnswers() 0 4 1
A setTotalBadges() 0 6 1
A getTotalBadges() 0 4 1
A setTotalComments() 0 6 1
A getTotalComments() 0 4 1
A setTotalQuestions() 0 6 1
A getTotalQuestions() 0 4 1
A setTotalUnanswered() 0 6 1
A getTotalUnanswered() 0 4 1
A setTotalUsers() 0 6 1
A getTotalUsers() 0 4 1
A setTotalVotes() 0 6 1
A getTotalVotes() 0 4 1
A setAnswersPerMinute() 0 6 1
A getAnswersPerMinute() 0 4 1
A setApiRevision() 0 6 1
A getApiRevision() 0 4 1
A setBadgesPerMinute() 0 6 1
A getBadgesPerMinute() 0 4 1
A setNewActiveUsers() 0 6 1
A getNewActiveUsers() 0 4 1
A setQuestionsPerMinute() 0 6 1
A getQuestionsPerMinute() 0 4 1
A setSite() 0 6 1
A getSite() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Info 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 Info, 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\Model;
13
14
/**
15
 * Class info model class.
16
 *
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class Info implements Model
20
{
21
    protected $totalAccepted;
22
    protected $totalAnswers;
23
    protected $totalBadges;
24
    protected $totalComments;
25
    protected $totalQuestions;
26
    protected $totalUnanswered;
27
    protected $totalUsers;
28
    protected $totalVotes;
29
    protected $answersPerMinute;
30
    protected $apiRevision;
31
    protected $badgesPerMinute;
32
    protected $newActiveUsers;
33
    protected $questionsPerMinute;
34
    protected $site;
35
36
    public static function fromJson(array $data)
37
    {
38
        $instance = new self();
39
        $instance
40
            ->setTotalAccepted(array_key_exists('total_accepted', $data) ? $data['total_accepted'] : null)
41
            ->setTotalAnswers(array_key_exists('total_answers', $data) ? $data['total_answers'] : null)
42
            ->setTotalBadges(array_key_exists('total_badges', $data) ? $data['total_badges'] : null)
43
            ->setTotalComments(array_key_exists('total_comments', $data) ? $data['total_comments'] : null)
44
            ->setTotalQuestions(array_key_exists('total_questions', $data) ? $data['total_questions'] : null)
45
            ->setTotalUnanswered(array_key_exists('total_unanswered', $data) ? $data['total_unanswered'] : null)
46
            ->setTotalUsers(array_key_exists('total_users', $data) ? $data['total_users'] : null)
47
            ->setTotalVotes(array_key_exists('total_votes', $data) ? $data['total_votes'] : null)
48
            ->setAnswersPerMinute(
49
                array_key_exists('answers_per_minute', $data)
50
                    ? $data['answers_per_minute']
51
                    : null
52
            )
53
            ->setApiRevision(array_key_exists('api_revision', $data) ? $data['api_revision'] : null)
54
            ->setBadgesPerMinute(
55
                array_key_exists('badges_per_minute', $data)
56
                    ? $data['badges_per_minute']
57
                    : null
58
            )
59
            ->setNewActiveUsers(
60
                array_key_exists('new_active_users', $data)
61
                    ? $data['new_active_users']
62
                    : null
63
            )
64
            ->setQuestionsPerMinute(
65
                array_key_exists('questions_per_minute', $data)
66
                    ? $data['questions_per_minute']
67
                    : null
68
            )
69
            ->setSite(array_key_exists('site', $data) ? Site::fromJson($data['site']) : null);
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('site',...n($data['site']) : null can also be of type object<BenatEspina\Stack...eApiClient\Model\Model>; however, BenatEspina\StackExchang...t\Model\Info::setSite() does only seem to accept null|object<BenatEspina\...geApiClient\Model\Site>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
70
71
        return $instance;
72
    }
73
74
    public static function fromProperties(
75
        $totalAccepted,
76
        $totalAnswers,
77
        $totalBadges,
78
        $totalComments,
79
        $totalQuestions,
80
        $totalUnanswered,
81
        $totalUsers,
82
        $totalVotes,
83
        $answersPerMinute,
84
        $apiRevision,
85
        $badgesPerMinute,
86
        $newActiveUsers,
87
        $questionsPerMinute,
88
        Site $site = null
89
    ) {
90
        $instance = new self();
91
        $instance
92
            ->setTotalAccepted($totalAccepted)
93
            ->setTotalAnswers($totalAnswers)
94
            ->setTotalBadges($totalBadges)
95
            ->setTotalComments($totalComments)
96
            ->setTotalQuestions($totalQuestions)
97
            ->setTotalUnanswered($totalUnanswered)
98
            ->setTotalUsers($totalUsers)
99
            ->setTotalVotes($totalVotes)
100
            ->setAnswersPerMinute($answersPerMinute)
101
            ->setApiRevision($apiRevision)
102
            ->setBadgesPerMinute($badgesPerMinute)
103
            ->setNewActiveUsers($newActiveUsers)
104
            ->setQuestionsPerMinute($questionsPerMinute)
105
            ->setSite($site);
106
107
        return $instance;
108
    }
109
110
    public function setTotalAccepted($totalAccepted)
111
    {
112
        $this->totalAccepted = $totalAccepted;
113
114
        return $this;
115
    }
116
117
    public function getTotalAccepted()
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...
118
    {
119
        return $this->totalAccepted;
120
    }
121
122
    public function setTotalAnswers($totalAnswers)
123
    {
124
        $this->totalAnswers = $totalAnswers;
125
126
        return $this;
127
    }
128
129
    public function getTotalAnswers()
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...
130
    {
131
        return $this->totalAnswers;
132
    }
133
134
    public function setTotalBadges($totalBadges)
135
    {
136
        $this->totalBadges = $totalBadges;
137
138
        return $this;
139
    }
140
141
    public function getTotalBadges()
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...
142
    {
143
        return $this->totalBadges;
144
    }
145
146
    public function setTotalComments($totalComments)
147
    {
148
        $this->totalComments = $totalComments;
149
150
        return $this;
151
    }
152
153
    public function getTotalComments()
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...
154
    {
155
        return $this->totalComments;
156
    }
157
158
    public function setTotalQuestions($totalQuestions)
159
    {
160
        $this->totalQuestions = $totalQuestions;
161
162
        return $this;
163
    }
164
165
    public function getTotalQuestions()
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...
166
    {
167
        return $this->totalQuestions;
168
    }
169
170
    public function setTotalUnanswered($totalUnanswered)
171
    {
172
        $this->totalUnanswered = $totalUnanswered;
173
174
        return $this;
175
    }
176
177
    public function getTotalUnanswered()
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...
178
    {
179
        return $this->totalUnanswered;
180
    }
181
182
    public function setTotalUsers($totalUsers)
183
    {
184
        $this->totalUsers = $totalUsers;
185
186
        return $this;
187
    }
188
189
    public function getTotalUsers()
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...
190
    {
191
        return $this->totalUsers;
192
    }
193
194
    public function setTotalVotes($totalVotes)
195
    {
196
        $this->totalVotes = $totalVotes;
197
198
        return $this;
199
    }
200
201
    public function getTotalVotes()
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...
202
    {
203
        return $this->totalVotes;
204
    }
205
206
    public function setAnswersPerMinute($answersPerMinute)
207
    {
208
        $this->answersPerMinute = $answersPerMinute;
209
210
        return $this;
211
    }
212
213
    public function getAnswersPerMinute()
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...
214
    {
215
        return $this->answersPerMinute;
216
    }
217
218
    public function setApiRevision($apiRevision)
219
    {
220
        $this->apiRevision = $apiRevision;
221
222
        return $this;
223
    }
224
225
    public function getApiRevision()
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...
226
    {
227
        return $this->apiRevision;
228
    }
229
230
    public function setBadgesPerMinute($badgesPerMinute)
231
    {
232
        $this->badgesPerMinute = $badgesPerMinute;
233
234
        return $this;
235
    }
236
237
    public function getBadgesPerMinute()
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...
238
    {
239
        return $this->badgesPerMinute;
240
    }
241
242
    public function setNewActiveUsers($newActiveUsers)
243
    {
244
        $this->newActiveUsers = $newActiveUsers;
245
246
        return $this;
247
    }
248
249
    public function getNewActiveUsers()
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...
250
    {
251
        return $this->newActiveUsers;
252
    }
253
254
    public function setQuestionsPerMinute($questionsPerMinute)
255
    {
256
        $this->questionsPerMinute = $questionsPerMinute;
257
258
        return $this;
259
    }
260
261
    public function getQuestionsPerMinute()
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...
262
    {
263
        return $this->questionsPerMinute;
264
    }
265
266
    public function setSite(Site $site = null)
267
    {
268
        $this->site = $site;
269
270
        return $this;
271
    }
272
273
    public function getSite()
274
    {
275
        return $this->site;
276
    }
277
}
278