Completed
Pull Request — master (#36)
by
unknown
08:48
created

NumberAndSecondaryStat::setPrecision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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
     * @var boolean
47
     */
48
    private $displayAsTimeDuration = false;
49
50
    /**
51
     * Set data main prefix (€, $, etc.).
52
     *
53
     * @param string $mainPrefix
54
     *
55
     * @return $this
56
     */
57 1
    public function setMainPrefix($mainPrefix)
58
    {
59 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...
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * Get data main prefix (€, $, etc.).
66
     *
67
     * @return string
68
     */
69 8
    public function getMainPrefix()
70
    {
71 8
        return $this->mainPrefix;
72
    }
73
74
    /**
75
     * Set main value.
76
     *
77
     * @param int $mainValue
78
     *
79
     * @return $this
80
     */
81 3
    public function setMainValue($mainValue)
82
    {
83 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...
84
85 3
        return $this;
86
    }
87
88
    /**
89
     * Get main value.
90
     *
91
     * @return int
92
     */
93 8
    public function getMainValue()
94
    {
95 8
        return $this->mainValue;
96
    }
97
98
    /**
99
     * Set the primary text value. (Visible if widget is 2x2
100
     * or 1x1 without a secondary value).
101
     *
102
     * @param string $mainText The text body
103
     *
104
     * @return NumberAndSecondaryStat
105
     */
106 1
    public function setMainText($mainText)
107
    {
108 1
        $this->mainText = $mainText;
109
110 1
        return $this;
111
    }
112
113
    /**
114
     * Return the main text body.
115
     *
116
     * @return string
117
     */
118 1
    public function getMainText()
119
    {
120 1
        return $this->mainText;
121
    }
122
123
    /**
124
     * Set secondary value.
125
     *
126
     * @param int $secondaryValue
127
     *
128
     * @return $this
129
     */
130 2
    public function setSecondaryValue($secondaryValue)
131
    {
132 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...
133
134 2
        return $this;
135
    }
136
137
    /**
138
     * Get secondary value.
139
     *
140
     * @return string
141
     */
142
    public function getSecondaryValue()
143
    {
144
        return $this->secondaryValue;
145
    }
146
147
    /**
148
     * Set the secondary text value. (Visible if widget is 2x2).
149
     *
150
     * @param string $secondaryText The text body
151
     *
152
     * @return NumberAndSecondaryStat
153
     */
154 1
    public function setSecondaryText($secondaryText)
155
    {
156 1
        $this->secondaryText = $secondaryText;
157
158 1
        return $this;
159
    }
160
161
    /**
162
     * Return the secondary text body.
163
     *
164
     * @return string
165
     */
166 1
    public function getSecondaryText()
167
    {
168 1
        return $this->secondaryText;
169
    }
170
171
    /**
172
     * @param string|null $prefix
173
     *
174
     * @return $this
175
     */
176 1
    public function setType($prefix)
177
    {
178 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...
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * @return string|null
185
     */
186 8
    public function getType()
187
    {
188 8
        return $this->type;
189
    }
190
191
    /**
192
     * Mark this widget as absolute.
193
     *
194
     * @param bool $absolute The absolute value
195
     *
196
     * @return NumberAndSecondaryStat
197
     */
198 1
    public function setAbsolute($absolute)
199
    {
200 1
        $this->absolute = $absolute;
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * Mark this widget for display main value as time duration (from milliseconds).
207
     *
208
     * @param boolean $displayAsTimeDuration
209
     *
210
     * @return NumberAndSecondaryStat
211
     */
212 1
    public function setDisplayAsTimeDuration($displayAsTimeDuration)
213
    {
214 1
        $this->displayAsTimeDuration = $displayAsTimeDuration;
215
216 1
        return $this;
217
    }
218
    
219
    /**
220
     * Sets the precision for decimal values.
221
     *
222
     * @param int $precision The precision value, from 0 to 6
223
     *
224
     * @return NumberAndSecondaryStat
225
     */
226
    public function setPrecision($precision)
227
    {
228
        $this->precision = $precision;
0 ignored issues
show
Bug introduced by
The property precision does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
229
230
        return $this;
231
    }
232
233
    /**
234
     * Sets the abbreviation (suffix) for values.
235
     *
236
     * @param string $abbreviation The abbreviation suffix, Possible values K, M, B, T and none
237
     *
238
     * @return NumberAndSecondaryStat
239
     */
240
    public function setAbbreviation($abbreviation)
241
    {
242
        $this->abbreviation = $abbreviation;
0 ignored issues
show
Bug introduced by
The property abbreviation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
243
244
        return $this;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 8
    public function getData()
251
    {
252
        $data = array(
253 8
            'text' => $this->mainText,
254 8
            'value' => (float) $this->getMainValue(),
255
        );
256
257 8
        $prefix = $this->getMainPrefix();
258 8
        if (null !== $prefix) {
259 1
            $data['prefix'] = (string) $prefix;
260
        }
261
262 8
        if ($this->displayAsTimeDuration) {
263 1
            $data['type'] = 'time_duration';
264
        }
265
266
        $result = array(
267 8
            'item' => array($data),
268 8
            'type' => $this->getType(),
269
        );
270
271 8
        if ($this->absolute) {
272 1
            $result['absolute'] = $this->absolute;
273
        }
274
        
275 8
        if ($this->precision !== false) {
276
            $result['precision'] = $this->precision;
277
        }
278
279
        if ($this->abbreviation) {
280
            $result['abbreviation'] = $this->abbreviation;
281
        }
282
283
        $secondaryValue = $this->getSecondaryValue();
284
        if (is_array($secondaryValue)) {
285
            $result['item'][] = $secondaryValue;
286
        } elseif (null !== $secondaryValue) {
287
            $result['item'][] = array(
288
                'text' => $this->secondaryText,
289
                'value' => (float) $secondaryValue,
290
            );
291
        }
292
293
        return $result;
294
    }
295
}
296