Completed
Push — master ( 9529d6...d756e3 )
by Carlos
10s
created

NumberAndSecondaryStat::setDisplayAsTimeDuration()   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
rs 9.4285
ccs 3
cts 3
cp 1
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
     * @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 8
    public function getSecondaryValue()
143
    {
144 8
        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
     * {@inheritdoc}
221
     */
222 8
    public function getData()
223
    {
224
        $data = array(
225 8
            'text' => $this->mainText,
226 8
            'value' => (float) $this->getMainValue(),
227 8
        );
228
229 8
        $prefix = $this->getMainPrefix();
230 8
        if (null !== $prefix) {
231 1
            $data['prefix'] = (string) $prefix;
232 1
        }
233
234 8
        if ($this->displayAsTimeDuration) {
235 1
            $data['type'] = 'time_duration';
236 1
        }
237
238
        $result = array(
239 8
            'item' => array($data),
240 8
            'type' => $this->getType(),
241 8
        );
242
243 8
        if ($this->absolute) {
244 1
            $result['absolute'] = $this->absolute;
245 1
        }
246
247 8
        $secondaryValue = $this->getSecondaryValue();
248 8
        if (is_array($secondaryValue)) {
249
            $result['item'][] = $secondaryValue;
250 8
        } elseif (null !== $secondaryValue) {
251 2
            $result['item'][] = array(
252 2
                'text' => $this->secondaryText,
253 2
                'value' => (float) $secondaryValue,
254
            );
255 2
        }
256
257 8
        return $result;
258
    }
259
}
260