Candidate::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
class Candidate
6
{
7
    const ELECTED = 1;
8
    const RUNNING = 2;
9
    const DEFEATED = 3;
10
11
    /**
12
     * Identifier for the candidate.
13
     *
14
     * @var int
15
     */
16
    protected $id;
17
18
    /**
19
     * Number of votes the candidate currently has.
20
     *
21
     * @var float
22
     */
23
    protected $votes;
24
25
    /**
26
     * State of the candidate (use class constants).
27
     *
28
     * @var int
29
     */
30
    protected $state;
31
32
    /**
33
     * Constructor.
34
     */
35 11
    public function __construct(int $id)
36
    {
37 11
        $this->id = $id;
38 11
        $this->votes = 0.0;
39 11
        $this->state = self::RUNNING;
40 11
    }
41
42
    /**
43
     * String representation of candidates.
44
     *
45
     * @return string
46
     */
47 2
    public function __toString()
48
    {
49 2
        return (string) $this->id;
50
    }
51
52
    /**
53
     * Gets the Identifier for the candidate.
54
     *
55
     * @return int
56
     */
57 5
    public function getId(): int
58
    {
59 5
        return $this->id;
60
    }
61
62
    /**
63
     * Gets the Number of votes the candidate currently has.
64
     *
65
     * @return float
66
     */
67 3
    public function getVotes(): float
68
    {
69 3
        return $this->votes;
70
    }
71
72
    /**
73
     * Adds votes to a candidate.
74
     *
75
     * @param float $votes Number of votes to add
76
     *
77
     * @return self
78
     */
79 2
    public function addVotes(float $votes)
80
    {
81 2
        $this->votes += $votes;
82
83 2
        return $this;
84
    }
85
86
    /**
87
     * Gets the State of the candidate (use class constants).
88
     *
89
     * @return int
90
     */
91 6
    public function getState(): int
92
    {
93 6
        return $this->state;
94
    }
95
96
    /**
97
     * Sets the State of the candidate (use class constants).
98
     *
99
     * @param int $state the state
100
     *
101
     * @return self
102
     */
103 4
    public function setState(int $state)
104
    {
105 4
        $this->state = $state;
106
107 4
        return $this;
108
    }
109
}
110