Completed
Push — master ( f5580e...4e6c29 )
by Beñat
01:39
created

NetworkUser::fromJson()   D

Complexity

Conditions 17
Paths 4

Size

Total Lines 42
Code Lines 31

Duplication

Lines 10
Ratio 23.81 %

Importance

Changes 0
Metric Value
dl 10
loc 42
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 31
nc 4
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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)
0 ignored issues
show
Bug introduced by
It seems like array_key_exists('badge_...'badge_counts']) : null can also be of type object<BenatEspina\Stack...eApiClient\Model\Model>; however, BenatEspina\StackExchang...kUser::setBadgeCounts() does only seem to accept null|object<BenatEspina\...lient\Model\BadgeCount>, 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...
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