Completed
Push — master ( 15f393...42b28c )
by Carlos
03:29 queued 01:06
created

NumberAndSecondaryStat::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace CarlosIO\Geckoboard\Widgets;
4
5
/**
6
 * Class NumberAndSecondaryStat.
7
 */
8
class NumberAndSecondaryStat extends Widget
9
{
10
    const TYPE_REGULAR = null;
11
    const TYPE_REVERSE = 'reverse';
12
13
    /**
14
     * @var null Main value
15
     */
16
    private $mainValue = null;
17
    /**
18
     * @var null Secondary value
19
     */
20
    private $secondaryValue = null;
21
    /**
22
     * @var null Main value prefix
23
     */
24
    private $mainPrefix = null;
25
    /**
26
     * @var null Main value prefix
27
     */
28
    private $type = null;
29
30
    /**
31
     * @var string
32
     */
33
    private $mainText = '';
34
35
    /**
36
     * @var string
37
     */
38
    private $secondaryText = '';
39
40
    /**
41
     * @var bool
42
     */
43
    private $absolute = false;
44
45
    /**
46
     * Set data main prefix (€, $, etc.).
47
     *
48
     * @param string $mainPrefix
49
     *
50
     * @return $this
51
     */
52 1
    public function setMainPrefix($mainPrefix)
53
    {
54 1
        $this->mainPrefix = $mainPrefix;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mainPrefix of type string is incompatible with the declared type null of property $mainPrefix.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
56 1
        return $this;
57
    }
58
59
    /**
60
     * Get data main prefix (€, $, etc.).
61
     *
62
     * @return string
63
     */
64 7
    public function getMainPrefix()
65
    {
66 7
        return $this->mainPrefix;
67
    }
68
69
    /**
70
     * Set main value.
71
     *
72
     * @param int $mainValue
73
     *
74
     * @return $this
75
     */
76 3
    public function setMainValue($mainValue)
77
    {
78 3
        $this->mainValue = $mainValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mainValue of type integer is incompatible with the declared type null of property $mainValue.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
80 3
        return $this;
81
    }
82
83
    /**
84
     * Get main value.
85
     *
86
     * @return int
87
     */
88 7
    public function getMainValue()
89
    {
90 7
        return $this->mainValue;
91
    }
92
93
    /**
94
     * Set the primary text value. (Visible if widget is 2x2
95
     * or 1x1 without a secondary value).
96
     *
97
     * @param string $mainText The text body
98
     *
99
     * @return NumberAndSecondaryStat
100
     */
101 1
    public function setMainText($mainText)
102
    {
103 1
        $this->mainText = $mainText;
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * Return the main text body.
110
     *
111
     * @return string
112
     */
113 1
    public function getMainText()
114
    {
115 1
        return $this->mainText;
116
    }
117
118
    /**
119
     * Set secondary value.
120
     *
121
     * @param int $secondaryValue
122
     *
123
     * @return $this
124
     */
125 2
    public function setSecondaryValue($secondaryValue)
126
    {
127 2
        $this->secondaryValue = $secondaryValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $secondaryValue of type integer is incompatible with the declared type null of property $secondaryValue.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
128
129 2
        return $this;
130
    }
131
132
    /**
133
     * Get secondary value.
134
     *
135
     * @return string
136
     */
137 7
    public function getSecondaryValue()
138
    {
139 7
        return $this->secondaryValue;
140
    }
141
142
    /**
143
     * Set the secondary text value. (Visible if widget is 2x2).
144
     *
145
     * @param string $secondaryText The text body
146
     *
147
     * @return NumberAndSecondaryStat
148
     */
149 1
    public function setSecondaryText($secondaryText)
150
    {
151 1
        $this->secondaryText = $secondaryText;
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * Return the secondary text body.
158
     *
159
     * @return string
160
     */
161 1
    public function getSecondaryText()
162
    {
163 1
        return $this->secondaryText;
164
    }
165
166
    /**
167
     * @param string|null $prefix
168
     *
169
     * @return $this
170
     */
171 1
    public function setType($prefix)
172
    {
173 1
        $this->type = $prefix;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prefix can also be of type string. However, the property $type is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * @return string|null
180
     */
181 7
    public function getType()
182
    {
183 7
        return $this->type;
184
    }
185
186
    /**
187
     * Mark this widget as absolute.
188
     *
189
     * @param bool $absolute The absolute value
190
     *
191
     * @return NumberAndSecondaryStat
192
     */
193 1
    public function setAbsolute($absolute)
194
    {
195 1
        $this->absolute = $absolute;
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 7
    public function getData()
204
    {
205
        $data = array(
206 7
            'text' => $this->mainText,
207 7
            'value' => (float) $this->getMainValue(),
208 7
        );
209
210 7
        $prefix = $this->getMainPrefix();
211 7
        if (null !== $prefix) {
212 1
            $data['prefix'] = (string) $prefix;
213 1
        }
214
215
        $result = array(
216 7
            'item' => array($data),
217 7
            'type' => $this->getType(),
218 7
        );
219
220 7
        if ($this->absolute) {
221 1
            $result['absolute'] = $this->absolute;
222 1
        }
223
224 7
        $secondaryValue = $this->getSecondaryValue();
225 7
        if (is_array($secondaryValue)) {
226
            $result['item'][] = $secondaryValue;
227 7
        } elseif (null !== $secondaryValue) {
228 2
            $result['item'][] = array(
229 2
                'text' => $this->secondaryText,
230 2
                'value' => (float) $secondaryValue,
231
            );
232 2
        }
233
234 7
        return $result;
235
    }
236
}
237