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

Badge::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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