Ballot   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 115
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRanking() 0 4 1
A getWeight() 0 4 1
A setWeight() 0 6 1
A getLevelUsed() 0 4 1
A incrementLevelUsed() 0 6 1
A getLastChoice() 0 10 2
A getNextChoice() 0 10 2
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
class Ballot
6
{
7
    /**
8
     * Ranking of candidates ids.
9
     *
10
     * @var int[]
11
     */
12
    protected $ranking;
13
14
    /**
15
     * The current weighting or value of this person's vote.
16
     *
17
     * @var float
18
     */
19
    protected $weight;
20
21
    /**
22
     * The current preference in use from this ballot.
23
     *
24
     * @var int
25
     */
26
    protected $levelUsed;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param int[] $ranking The ranking of candidates Key being ranking,
32
     *                       value being a candidate id. Zero-indexed (Key
33
     *                       0 for first choice)
34
     */
35 11
    public function __construct(array $ranking)
36
    {
37 11
        $this->weight = 1.0;
38 11
        $this->ranking = $ranking;
39 11
        $this->levelUsed = -1;
40 11
    }
41
42
    /**
43
     * Gets the Ranking of candidates ids.
44
     *
45
     * @return array
46
     */
47 4
    public function getRanking(): array
48
    {
49 4
        return $this->ranking;
50
    }
51
52
    /**
53
     * Gets the The current weighting or value of this person's vote.
54
     *
55
     * @return float
56
     */
57 3
    public function getWeight(): float
58
    {
59 3
        return $this->weight;
60
    }
61
62
    /**
63
     * Sets the The current weighting or value of this person's vote.
64
     *
65
     * @param float $weight The weight
66
     *
67
     * @return float $weight    The inputted weight
68
     */
69 2
    public function setWeight(float $weight): float
70
    {
71 2
        $this->weight = round($weight, 5);
72
73 2
        return $weight;
74
    }
75
76
    /**
77
     * Gets the the current preference in use from this ballot.
78
     *
79
     * @return int
80
     */
81 2
    public function getLevelUsed(): int
82
    {
83 2
        return $this->levelUsed;
84
    }
85
86
    /**
87
     * Sets the the current preference in use from this ballot.
88
     *
89
     * @return int
90
     */
91 3
    public function incrementLevelUsed(): int
92
    {
93 3
        ++$this->levelUsed;
94
95 3
        return $this->levelUsed;
96
    }
97
98 2
    public function getLastChoice()
99
    {
100 2
        $level = $this->levelUsed;
101
102 2
        if (empty($this->ranking[$level])) {
103 1
            return;
104
        }
105
106 2
        return $this->ranking[$level];
107
    }
108
109 2
    public function getNextChoice()
110
    {
111 2
        $level = $this->levelUsed + 1;
112
113 2
        if (empty($this->ranking[$level])) {
114 2
            return;
115
        }
116
117 2
        return $this->ranking[$level];
118
    }
119
}
120