NetworkActivity   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 215
Duplicated Lines 2.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 1
dl 5
loc 215
rs 8.3999
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
C fromJson() 5 29 14
B fromProperties() 0 29 1
A setAccountId() 0 6 1
A getAccountId() 0 4 1
A setActivityType() 0 8 2
A getActivityType() 0 4 1
A setApiSiteParameter() 0 6 1
A getApiSiteParameter() 0 4 1
A setBadgeId() 0 6 1
A getBadgeId() 0 4 1
A setDescription() 0 6 1
A getDescription() 0 4 1
A setLink() 0 6 1
A getLink() 0 4 1
A setPostId() 0 6 1
A getPostId() 0 4 1
A setScore() 0 6 1
A getScore() 0 4 1
A setCreationDate() 0 6 1
A getCreationDate() 0 4 1
A setTags() 0 6 1
A getTags() 0 4 1
A setTitle() 0 6 1
A getTitle() 0 4 1

How to fix   Duplicated Code   

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:

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
 * Class network activity model class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class NetworkActivity implements Model
30
{
31
    const ACTIVITY_TYPES = [
32
        'answer_posted',
33
        'badge_earned',
34
        'comment_posted',
35
        'question_posted',
36
    ];
37
38
    protected $accountId;
39
    protected $activityType;
40
    protected $apiSiteParameter;
41
    protected $badgeId;
42
    protected $description;
43
    protected $link;
44
    protected $postId;
45
    protected $score;
46
    protected $creationDate;
47
    protected $tags;
48
    protected $title;
49
50
    public static function fromJson(array $data)
51
    {
52
        $tags = [];
53 View Code Duplication
        if (array_key_exists('tags', $data) && is_array($data['tags'])) {
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...
54
            foreach ($data['tags'] as $tag) {
55
                $tags[] = Tag::fromJson($tag);
56
            }
57
        }
58
59
        $instance = new self();
60
        $instance
61
            ->setAccountId(array_key_exists('account_id', $data) ? $data['account_id'] : null)
62
            ->setActivityType(array_key_exists('activity_type', $data) ? $data['activity_type'] : null)
63
            ->setApiSiteParameter(array_key_exists('api_site_parameter', $data) ? $data['api_site_parameter'] : null)
64
            ->setBadgeId(array_key_exists('badge_id', $data) ? $data['badge_id'] : null)
65
            ->setDescription(array_key_exists('description', $data) ? $data['description'] : null)
66
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
67
            ->setPostId(array_key_exists('post_id', $data) ? $data['post_id'] : null)
68
            ->setScore(array_key_exists('score', $data) ? $data['score'] : null)
69
            ->setCreationDate(
70
                array_key_exists('creation_date', $data)
71
                    ? new \DateTimeImmutable('@' . $data['creation_date'])
72
                    : null
73
            )
74
            ->setTags($tags)
75
            ->setTitle(array_key_exists('title', $data) ? $data['title'] : null);
76
77
        return $instance;
78
    }
79
80
    public static function fromProperties(
81
        $accountId,
82
        $activityType,
83
        $apiSiteParameter,
84
        $badgeId,
85
        $description,
86
        $link,
87
        $postId,
88
        $score,
89
        \DateTimeInterface $creationDate,
90
        $tags,
91
        $title
92
    ) {
93
        $instance = new self();
94
        $instance
95
            ->setAccountId($accountId)
96
            ->setActivityType($activityType)
97
            ->setApiSiteParameter($apiSiteParameter)
98
            ->setBadgeId($badgeId)
99
            ->setDescription($description)
100
            ->setLink($link)
101
            ->setPostId($postId)
102
            ->setScore($score)
103
            ->setCreationDate($creationDate)
104
            ->setTags($tags)
105
            ->setTitle($title);
106
107
        return $instance;
108
    }
109
110
    public function setAccountId($accountId)
111
    {
112
        $this->accountId = $accountId;
113
114
        return $this;
115
    }
116
117
    public function getAccountId()
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->accountId;
120
    }
121
122
    public function setActivityType($activityType)
123
    {
124
        if (in_array($activityType, self::ACTIVITY_TYPES, true)) {
125
            $this->activityType = $activityType;
126
        }
127
128
        return $this;
129
    }
130
131
    public function getActivityType()
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...
132
    {
133
        return $this->activityType;
134
    }
135
136
    public function setApiSiteParameter($apiSiteParameter)
137
    {
138
        $this->apiSiteParameter = $apiSiteParameter;
139
140
        return $this;
141
    }
142
143
    public function getApiSiteParameter()
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...
144
    {
145
        return $this->apiSiteParameter;
146
    }
147
148
    public function setBadgeId($badgeId)
149
    {
150
        $this->badgeId = $badgeId;
151
152
        return $this;
153
    }
154
155
    public function getBadgeId()
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...
156
    {
157
        return $this->badgeId;
158
    }
159
160
    public function setDescription($description)
161
    {
162
        $this->description = $description;
163
164
        return $this;
165
    }
166
167
    public function getDescription()
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...
168
    {
169
        return $this->description;
170
    }
171
172
    public function setLink($link)
173
    {
174
        $this->link = $link;
175
176
        return $this;
177
    }
178
179
    public function getLink()
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...
180
    {
181
        return $this->link;
182
    }
183
184
    public function setPostId($postId)
185
    {
186
        $this->postId = $postId;
187
188
        return $this;
189
    }
190
191
    public function getPostId()
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...
192
    {
193
        return $this->postId;
194
    }
195
196
    public function setScore($score)
197
    {
198
        $this->score = $score;
199
200
        return $this;
201
    }
202
203
    public function getScore()
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...
204
    {
205
        return $this->score;
206
    }
207
208
    public function setCreationDate(\DateTimeInterface $creationDate = null)
209
    {
210
        $this->creationDate = $creationDate;
211
212
        return $this;
213
    }
214
215
    public function getCreationDate()
216
    {
217
        return $this->creationDate;
218
    }
219
220
    public function setTags($tags)
221
    {
222
        $this->tags = $tags;
223
224
        return $this;
225
    }
226
227
    public function getTags()
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...
228
    {
229
        return $this->tags;
230
    }
231
232
    public function setTitle($title)
233
    {
234
        $this->title = $title;
235
236
        return $this;
237
    }
238
239
    public function getTitle()
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...
240
    {
241
        return $this->title;
242
    }
243
}
244