Badge::fromJson()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 7.756
c 0
b 0
f 0
cc 9
eloc 12
nc 1
nop 1
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 badge model class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class Badge implements Model
30
{
31
    const BADGE_TYPES = ['named', 'tag_based'];
32
    const RANKS = ['gold', 'silver', 'bronze'];
33
34
    protected $id;
35
    protected $awardCount;
36
    protected $badgeType;
37
    protected $description;
38
    protected $link;
39
    protected $name;
40
    protected $rank;
41
    protected $user;
42
43 View Code Duplication
    public static function fromJson(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45
        $instance = new self();
46
        $instance
47
            ->setId(array_key_exists('badge_id', $data) ? $data['badge_id'] : null)
48
            ->setAwardCount(array_key_exists('award_count', $data) ? $data['award_count'] : null)
49
            ->setBadgeType(array_key_exists('badge_type', $data) ? $data['badge_type'] : null)
50
            ->setLink(array_key_exists('link', $data) ? $data['link'] : null)
51
            ->setName(array_key_exists('name', $data) ? $data['name'] : null)
52
            ->setRank(array_key_exists('rank', $data) ? $data['rank'] : null)
53
            ->setUser(array_key_exists('user', $data) ? ShallowUser::fromJson($data['user']) : null)
54
            ->setDescription(array_key_exists('description', $data) ? $data['description'] : null);
55
56
        return $instance;
57
    }
58
59
    public static function fromProperties(
60
        $id,
61
        $awardCount,
62
        $badgeType,
63
        $link,
64
        $name,
65
        $rank,
66
        $user,
67
        $description = null
68
    ) {
69
        $instance = new self();
70
        $instance
71
            ->setId($id)
72
            ->setAwardCount($awardCount)
73
            ->setBadgeType($badgeType)
74
            ->setLink($link)
75
            ->setName($name)
76
            ->setRank($rank)
77
            ->setUser($user)
78
            ->setDescription($description);
79
80
        return $instance;
81
    }
82
83
    public function setId($id)
84
    {
85
        $this->id = $id;
86
87
        return $this;
88
    }
89
90
    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...
91
    {
92
        return $this->id;
93
    }
94
95
    public function setAwardCount($awardCount)
96
    {
97
        $this->awardCount = $awardCount;
98
99
        return $this;
100
    }
101
102
    public function getAwardCount()
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...
103
    {
104
        return $this->awardCount;
105
    }
106
107
    public function setBadgeType($badgeType)
108
    {
109
        if (in_array($badgeType, self::BADGE_TYPES, true)) {
110
            $this->badgeType = $badgeType;
111
        }
112
113
        return $this;
114
    }
115
116
    public function getBadgeType()
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...
117
    {
118
        return $this->badgeType;
119
    }
120
121
    public function setDescription($description)
122
    {
123
        $this->description = $description;
124
125
        return $this;
126
    }
127
128
    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...
129
    {
130
        return $this->description;
131
    }
132
133
    public function setLink($link)
134
    {
135
        $this->link = $link;
136
137
        return $this;
138
    }
139
140
    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...
141
    {
142
        return $this->link;
143
    }
144
145
    public function setName($name)
146
    {
147
        $this->name = $name;
148
149
        return $this;
150
    }
151
152
    public function getName()
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...
153
    {
154
        return $this->name;
155
    }
156
157
    public function setRank($rank)
158
    {
159
        if (in_array($rank, self::RANKS, true)) {
160
            $this->rank = $rank;
161
        }
162
163
        return $this;
164
    }
165
166
    public function getRank()
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...
167
    {
168
        return $this->rank;
169
    }
170
171
    public function setUser(ShallowUser $user = null)
172
    {
173
        $this->user = $user;
174
175
        return $this;
176
    }
177
178
    public function getUser()
179
    {
180
        return $this->user;
181
    }
182
}
183