Completed
Push — v2 ( e6c7b3...40717e )
by Beñat
06:35
created

NetworkUser::fromProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 31
rs 8.8571
cc 1
eloc 28
nc 1
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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