Test Failed
Push — master ( 2b1c87...f9c14b )
by Laurens
02:16
created

Thermostat::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 28
dl 0
loc 58
ccs 29
cts 29
cp 1
crap 1
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\Device;
6
7
use DateTimeImmutable;
8
use LauLamanApps\NestApi\Client\Device\Thermostat\HvacMode;
9
use LauLamanApps\NestApi\Client\Device\Thermostat\HvacState;
10
use LauLamanApps\NestApi\Client\Device\Thermostat\Temperature;
11
use LauLamanApps\NestApi\Client\Device\Thermostat\Temperature\Scale;
12
use LauLamanApps\NestApi\Http\Command\ThermostatCommand;
13
use LauLamanApps\NestApi\NestClientInterface;
14
15
final class Thermostat
16
{
17
    /**
18
     * @var NestClientInterface
19
     */
20
    private $client;
21
22
    /**
23
     * @var string
24
     */
25
    private $deviceId;
26
27
    /**
28
     * @var string
29
     */
30
    private $whereId;
31
32
    /**
33
     * @var string
34
     */
35
    private $structureId;
36
37
    /**
38
     * @var string
39
     */
40
    private $name;
41
42
    /**
43
     * @var string
44
     */
45
    private $nameLong;
46
47
    /**
48
     * @var Scale
49
     */
50
    private $scale;
51
52
    /**
53
     * @var string
54
     */
55
    private $locale;
56
57
    /**
58
     * @var string
59
     */
60
    private $softwareVersion;
61
62
    /**
63
     * @var bool
64
     */
65
    private $canHeat;
66
67
    /**
68
     * @var bool
69
     */
70
    private $canCool;
71
72
    /**
73
     * @var bool
74
     */
75
    private $hasFan;
76
77
    /**
78
     * @var Temperature
79
     */
80
    private $ambientTemperature;
81
82
    /**
83
     * @var Temperature
84
     */
85
    private $targetTemperature;
86
87
    /**
88
     * @var Temperature
89
     */
90
    private $targetTemperatureHigh;
91
92
    /**
93
     * @var Temperature
94
     */
95
    private $targetTemperatureLow;
96
97
    /**
98
     * @var Temperature
99
     */
100
    private $lockedTempMin;
101
102
    /**
103
     * @var Temperature
104
     */
105
    private $lockedTempMax;
106
107
    /**
108
     * @var bool
109
     */
110
    private $has_leaf;
111
112
    /**
113
     * @var int
114
     */
115
    private $humidity;
116
117
    /**
118
     * @var HvacMode
119
     */
120
    private $hvacMode;
121
122
    /**
123
     * @var HvacState
124
     */
125
    private $hvacState;
126
127
    /**
128
     * @var bool
129
     */
130
    private $isUsingEmergencyHeat;
131
132
    /**
133
     * @var bool
134
     */
135
    private $locked;
136
137
    /**
138
     * @var bool
139
     */
140
    private $online;
141
142
    /**
143
     * @var null|DateTimeImmutable
144
     */
145
    private $lastConnection;
146
147
    /**
148
     * @var bool
149
     */
150
    private $fanTimerActive;
151
152
    /**
153
     * @var DateTimeImmutable
154
     */
155
    private $fanTimerTimeout;
156
157
158 2
    public function __construct(
159
        NestClientInterface $nestClient,
160
        string $deviceId,
161
        string $whereId,
162
        string $structureId,
163
        string $name,
164
        string $nameLong,
165
        Scale $scale,
166
        string $locale,
167
        string $softwareVersion,
168
        bool $canHeat,
169
        bool $canCool,
170
        bool $hasFan,
171
        Temperature $ambientTemperature,
172
        Temperature $targetTemperature,
173
        Temperature $targetTemperatureHigh,
174
        Temperature $targetTemperatureLow,
175
        Temperature $lockedTempMin,
176
        Temperature $lockedTempMax,
177
        bool $has_leaf,
178
        int $humidity,
179
        HvacMode $hvacMode,
180
        HvacState $hvacState,
181
        bool $isUsingEmergencyHeat,
182
        bool $locked,
183
        bool $online,
184
        ?DateTimeImmutable $lastConnection = null,
185
        bool $fanTimerActive,
186
        DateTimeImmutable $fanTimerTimeout
187
    ) {
188 2
        $this->deviceId = $deviceId;
189 2
        $this->whereId = $whereId;
190 2
        $this->structureId = $structureId;
191 2
        $this->name = $name;
192 2
        $this->nameLong = $nameLong;
193 2
        $this->scale = $scale;
194 2
        $this->locale = $locale;
195 2
        $this->softwareVersion = $softwareVersion;
196 2
        $this->canHeat = $canHeat;
197 2
        $this->canCool = $canCool;
198 2
        $this->hasFan = $hasFan;
199 2
        $this->ambientTemperature = $ambientTemperature;
200 2
        $this->targetTemperature = $targetTemperature;
201 2
        $this->targetTemperatureHigh = $targetTemperatureHigh;
202 2
        $this->targetTemperatureLow = $targetTemperatureLow;
203 2
        $this->lockedTempMin = $lockedTempMin;
204 2
        $this->lockedTempMax = $lockedTempMax;
205 2
        $this->has_leaf = $has_leaf;
206 2
        $this->humidity = $humidity;
207 2
        $this->hvacMode = $hvacMode;
208 2
        $this->hvacState = $hvacState;
209 2
        $this->isUsingEmergencyHeat = $isUsingEmergencyHeat;
210 2
        $this->locked = $locked;
211 2
        $this->online = $online;
212 2
        $this->lastConnection = $lastConnection;
213 2
        $this->fanTimerActive = $fanTimerActive;
214 2
        $this->fanTimerTimeout = $fanTimerTimeout;
215 2
        $this->client = $nestClient;
216 2
    }
217
218
    public function getDeviceId(): string
219
    {
220
        return $this->deviceId;
221
    }
222
223
    public function getWhereId(): string
224
    {
225
        return $this->whereId;
226
    }
227
228
    public function getStructureId(): string
229
    {
230
        return $this->structureId;
231
    }
232
233 2
    public function getName(): string
234
    {
235 2
        return $this->name;
236
    }
237
238
    public function getNameLong(): string
239
    {
240
        return $this->nameLong;
241
    }
242
243
    public function getScale(): Scale
244
    {
245
        return $this->scale;
246
    }
247
248
    public function getLocale(): string
249
    {
250
        return $this->locale;
251
    }
252
253
    public function getSoftwareVersion(): string
254
    {
255
        return $this->softwareVersion;
256
    }
257
258
    public function canHeat(): bool
259
    {
260
        return $this->canHeat;
261
    }
262
263
    public function canCool(): bool
264
    {
265
        return $this->canCool;
266
    }
267
268
    public function hasFan(): bool
269
    {
270
        return $this->hasFan;
271
    }
272
273
    public function getAmbientTemperature(): Temperature
274
    {
275
        return $this->ambientTemperature;
276
    }
277
278
    public function getTargetTemperature(): Temperature
279
    {
280
        return $this->targetTemperature;
281
    }
282
283
    public function getTargetTemperatureHigh(): Temperature
284
    {
285
        return $this->targetTemperatureHigh;
286
    }
287
288
    public function getTargetTemperatureLow(): Temperature
289
    {
290
        return $this->targetTemperatureLow;
291
    }
292
293
    public function getLockedTempMin(): Temperature
294
    {
295
        return $this->lockedTempMin;
296
    }
297
298
    public function getLockedTempMax(): Temperature
299
    {
300
        return $this->lockedTempMax;
301
    }
302
303
    public function hasLeaf(): bool
304
    {
305
        return $this->has_leaf;
306
    }
307
308
    public function getHumidity(): int
309
    {
310
        return $this->humidity;
311
    }
312
313
    public function getHvacMode(): HvacMode
314
    {
315
        return $this->hvacMode;
316
    }
317
318
    public function getHvacState(): HvacState
319
    {
320
        return $this->hvacState;
321
    }
322
323
    public function isUsingEmergencyHeat(): bool
324
    {
325
        return $this->isUsingEmergencyHeat;
326
    }
327
328
    public function isLocked(): bool
329
    {
330
        return $this->locked;
331
    }
332
333
    public function isOnline(): bool
334
    {
335
        return $this->online;
336
    }
337
338
    public function getLastConnection(): ?DateTimeImmutable
339
    {
340
        return $this->lastConnection;
341
    }
342
343
    public function isFanTimerActive(): bool
344
    {
345
        return $this->fanTimerActive;
346
    }
347
348
    public function getFanTimerTimeout(): DateTimeImmutable
349
    {
350
        return $this->fanTimerTimeout;
351
    }
352
353
    public function setScale(Scale $scale): void
354
    {
355
        $command = new ThermostatCommand($this->getDeviceId());
356
        $command->setScale($scale);
357
358
        $this->client->sendCommand($command);
359
    }
360
361
    public function setTargetTemperature(Temperature $temperature): void
362
    {
363
        $command = new ThermostatCommand($this->getDeviceId());
364
        $command->setTargetTemperature($temperature);
365
366
        $this->client->sendCommand($command);
367
    }
368
369
    public function setTargetTemperatureHigh(Temperature $temperature): void
370
    {
371
        $command = new ThermostatCommand($this->getDeviceId());
372
        $command->setTargetTemperatureHigh($temperature);
373
374
        $this->client->sendCommand($command);
375
    }
376
377
    public function setTargetTemperatureLow(Temperature $temperature): void
378
    {
379
        $command = new ThermostatCommand($this->getDeviceId());
380
        $command->setTargetTemperatureLow($temperature);
381
382
        $this->client->sendCommand($command);
383
    }
384
385
    public function setEcoTemperatureHigh(Temperature $temperature): void
386
    {
387
        $command = new ThermostatCommand($this->getDeviceId());
388
        $command->setEcoTemperatureHigh($temperature);
389
390
        $this->client->sendCommand($command);
391
    }
392
393
    public function setEcoTemperatureLow(Temperature $temperature): void
394
    {
395
        $command = new ThermostatCommand($this->getDeviceId());
396
        $command->setEcoTemperatureLow($temperature);
397
398
        $this->client->sendCommand($command);
399
    }
400
401
    public function setHvacMode(HvacMode $mode): void
402
    {
403
        $command = new ThermostatCommand($this->getDeviceId());
404
        $command->setHvacMode($mode);
405
406
        $this->client->sendCommand($command);
407
    }
408
409
    public function setLabel(string $label): void
410
    {
411
        $command = new ThermostatCommand($this->getDeviceId());
412
        $command->setLabel($label);
413
414
        $this->client->sendCommand($command);
415
    }
416
417
    public function setFanTimerDuration(int $duration): void
418
    {
419
        $command = new ThermostatCommand($this->getDeviceId());
420
        $command->setFanTimerDuration($duration);
421
422
        $this->client->sendCommand($command);
423
    }
424
}
425