Election   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 1
dl 0
loc 220
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCandidate() 0 4 1
A getCandidateCount() 0 4 1
A getActiveCandidates() 0 4 1
A getActiveCandidateIds() 0 12 2
A getElectedCandidates() 0 4 1
A getDefeatedCandidates() 0 4 1
A getStateCandidates() 0 12 3
A getActiveCandidateCount() 0 4 1
A getCandidateIds() 0 10 2
A getWinnersCount() 0 4 1
A getCandidates() 0 4 1
A getBallots() 0 4 1
A getNumBallots() 0 4 1
A getCandidatesStatus() 0 18 4
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
class Election
6
{
7
    /**
8
     * Count of candidates in election.
9
     *
10
     * @var int
11
     */
12
    protected $candidateCount;
13
14
    /**
15
     * Count of number of seats/winners.
16
     *
17
     * @var int
18
     */
19
    protected $winnersCount;
20
21
    /**
22
     * Array of candidates competing in election.
23
     *
24
     * @var array
25
     */
26
    protected $candidates;
27
28
    /**
29
     * Array of ballots cast in election.
30
     *
31
     * @var array
32
     */
33
    protected $ballots;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param array $candidates   Array of candidates competing
39
     * @param array $ballots      Array of all ballots cast in election
40
     * @param int   $winnersCount Number of winners to allocate
41
     */
42 6
    public function __construct(array $candidates, array $ballots, int $winnersCount = 2)
43
    {
44 6
        $this->winnersCount = $winnersCount;
45 6
        $this->candidates = $candidates;
46 6
        $this->ballots = $ballots;
47 6
        $this->candidateCount = count($candidates);
48 6
    }
49
50
    /**
51
     * Get a specific candidate object by their ID.
52
     *
53
     * @param int $id ID of the candidate to get
54
     *
55
     * @return \Michaelc\Voting\STV\Candidate
56
     */
57 2
    public function getCandidate(int $id): Candidate
58
    {
59 2
        return $this->candidates[$id];
60
    }
61
62
    /**
63
     * Get a count of candidates competing.
64
     *
65
     * @return int
66
     */
67 4
    public function getCandidateCount(): int
68
    {
69 4
        return $this->candidateCount;
70
    }
71
72
    /**
73
     * Get an array of candidates still running (not elected or defeated).
74
     *
75
     * @return \Michaelc\Voting\STV\Candidate[]
76
     */
77 3
    public function getActiveCandidates(): array
78
    {
79 3
        return $this->getStateCandidates(Candidate::RUNNING);
80
    }
81
82 2
    public function getActiveCandidateIds(): array
83
    {
84 2
        $ids = [];
85
86 2
        $candidates = $this->getActiveCandidates();
87
88 2
        foreach ($candidates as $candidate) {
89 2
            $ids[] = $candidate->getId();
90
        }
91
92 2
        return $ids;
93
    }
94
95
    /**
96
     * Get an array of candidates elected.
97
     *
98
     * @return \Michaelc\Voting\STV\Candidate[]
99
     */
100 2
    public function getElectedCandidates(): array
101
    {
102 2
        return $this->getStateCandidates(Candidate::ELECTED);
103
    }
104
105
    /**
106
     * Get an array of candidates defeated.
107
     *
108
     * @return \Michaelc\Voting\STV\Candidate[]
109
     */
110 2
    public function getDefeatedCandidates(): array
111
    {
112 2
        return $this->getStateCandidates(Candidate::DEFEATED);
113
    }
114
115
    /**
116
     * Get all candidates of a specific state.
117
     *
118
     * @param int $state A candidate state (See Candidate constants)
119
     *
120
     * @return \Michaelc\Voting\STV\Candidate[]
121
     */
122 3
    public function getStateCandidates(int $state): array
123
    {
124 3
        $candidates = [];
125
126 3
        foreach ($this->candidates as $candidateId => $candidate) {
127 3
            if ($candidate->getState() == $state) {
128 3
                $candidates[$candidateId] = $candidate;
129
            }
130
        }
131
132 3
        return $candidates;
133
    }
134
135
    /**
136
     * Get a count of candidates still running (not elected or defeated).
137
     *
138
     * @return int
139
     */
140 2
    public function getActiveCandidateCount(): int
141
    {
142 2
        return count($this->getActiveCandidates());
143
    }
144
145
    /**
146
     * Get an array of candidate ids.
147
     *
148
     * @return int[]
149
     */
150 3
    public function getCandidateIds(): array
151
    {
152 3
        $candidateIds = [];
153
154 3
        foreach ($this->candidates as $candidate) {
155 3
            $candidateIds[] = $candidate->getId();
156
        }
157
158 3
        return $candidateIds;
159
    }
160
161
    /**
162
     * Gets the value of winnersCount.
163
     *
164
     * @return int
165
     */
166 4
    public function getWinnersCount(): int
167
    {
168 4
        return $this->winnersCount;
169
    }
170
171
    /**
172
     * Gets the value of candidates.
173
     *
174
     * @return array
175
     */
176 3
    public function getCandidates(): array
177
    {
178 3
        return $this->candidates;
179
    }
180
181
    /**
182
     * Gets the value of ballots.
183
     *
184
     * @return array
185
     */
186 3
    public function getBallots(): array
187
    {
188 3
        return $this->ballots;
189
    }
190
191
    /**
192
     * Get the number of ballots.
193
     *
194
     * @return int
195
     */
196 3
    public function getNumBallots(): int
197
    {
198 3
        return count($this->ballots);
199
    }
200
201
    /**
202
     * Get status of all candidates.
203
     *
204
     * @return array
205
     */
206 1
    public function getCandidatesStatus(): array
207
    {
208 1
        $candidates = [];
209
210 1
        foreach ($this->getElectedCandidates() as $candidate) {
211 1
            $candidates['elected'][] = $candidate->getId();
212
        }
213
214 1
        foreach ($this->getActiveCandidates() as $candidate) {
215 1
            $candidates['active'][] = [$candidate->getId(), $candidate->getVotes()];
216
        }
217
218 1
        foreach ($this->getDefeatedCandidates() as $candidate) {
219 1
            $candidates['defeated'][] = $candidate->getId();
220
        }
221
222 1
        return $candidates;
223
    }
224
}
225