Car   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 69
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A changeState() 0 3 1
A brake() 0 5 1
A turnOn() 0 5 1
A isOn() 0 3 1
A turnOff() 0 5 1
A speedUp() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
namespace Tleckie\DesignPatterns\State;
4
5
/**
6
 * Class Car
7
 *
8
 * @package Tleckie\DesignPatterns\State
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class Car
12
{
13
    /** @var CarState */
14
    private CarState $state;
15
16
    /**
17
     * Car constructor.
18
     *
19
     * @param CarState $state
20
     */
21
    public function __construct(CarState $state)
22
    {
23
        $this->changeState($state);
24
    }
25
26
    /**
27
     * @param CarState $state
28
     */
29
    private function changeState(CarState $state): void
30
    {
31
        $this->state = $state;
32
    }
33
34
    /**
35
     * @return $this
36
     */
37
    public function brake(): Car
38
    {
39
        $this->changeState($this->state->brake());
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return $this
46
     */
47
    public function speedUp(): Car
48
    {
49
        $this->changeState($this->state->speedUp());
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return $this
56
     */
57
    public function turnOff(): Car
58
    {
59
        $this->changeState($this->state->turnOff());
60
61
        return $this;
62
    }
63
64
    /**
65
     * @return $this
66
     */
67
    public function turnOn(): Car
68
    {
69
        $this->changeState($this->state->turnOn());
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isOn(): bool
78
    {
79
        return !$this->state instanceof OffState;
80
    }
81
}
82