Completed
Push — master ( 23a3a0...f1929a )
by Michael
04:24 queued 52s
created
src/Michaelc/Voting/STV/VoteHandler.php 1 patch
Indentation   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -6,104 +6,104 @@
 block discarded – undo
6 6
 
7 7
 class VoteHandler
8 8
 {
9
-	/**
10
-	 * Election object
11
-	 *
12
-	 * @var \Michaelc\Voting\STV\Election;
13
-	 */
14
-	protected $election;
15
-
16
-	/**
17
-	 * Array of all ballots in election
18
-	 *
19
-	 * @var \MichaelC\Voting\STV\Ballot[]
20
-	 */
21
-	protected $ballots;
22
-
23
-	/**
24
-	 * Quota of votes needed for a candidate to be elected
25
-	 *
26
-	 * @var int
27
-	 */
28
-	protected $quota;
29
-
30
-	/**
31
-	 * Constructor
32
-	 *
33
-	 * @param Election $election
34
-	 */
35
-	public function __construct(Election $election)
36
-	{
37
-		$this->election = $election;
38
-		$this->ballots = $this->election->getBallots();
39
-		$this->quota = $this->getQuota();
40
-	}
41
-
42
-	public function step($step)
43
-	{
44
-		foreach ($this->ballots as $i => $ballot)
45
-		{
46
-			$this->allocateVotes($ballot);
47
-		}
48
-
49
-		$candidates = $election->getActiveCandidates();
50
-
51
-		foreach ($candidates as $i => $candidate)
52
-		{
53
-			if ($candidate->getVotes() >= $this->quota)
54
-			{
55
-				$this->electCandidate($candidate);
56
-        	}
57
-		}
58
-	}
59
-
60
-	protected function allocateVotes(&$ballot): Ballot
61
-	{
62
-		$weight = $ballot->getWeight();
63
-		$candidate = $ballot->getNextPreference();
64
-		$this->election->getCandidate($candidate->getId())->addVotes($weight);
65
-		$ballot->setLastUsedLevel(($step - 1));
66
-
67
-		return $ballot;
68
-	}
69
-
70
-	protected function transferVotes(float $votes, Candidate $candidate)
71
-	{
72
-		return;
73
-	}
74
-
75
-	/**
76
-	 * Elect a candidate after they've passed the threshold
77
-	 *
78
-	 * @param  \Michaelc\Voting\STV\Candidate $candidate
79
-	 * @return null
80
-	 */
81
-	protected function electCandidate(Candidate $candidate)
82
-	{
83
-		if ($candidate->getVotes() < $this->quota)
84
-		{
85
-			throw new Exception("We shouldn't be electing someone who hasn't met the quota");
86
-    	}
87
-
88
-		$candidate->setState(Candidate::ELECTED);
89
-		$surplus = $candidate->getVotes() - $this->quota;
90
-
91
-		$this->transferVotes($surplus, $candidate);
92
-
93
-		return;
94
-	}
95
-
96
-	/**
97
-	 * Get the quota to win
98
-	 *
99
-	 * @return int
100
-	 */
101
-	public function getQuota(): int
102
-	{
103
-		return floor(
104
-			($this->election->getNumBallots() /
105
-				($this->election->getWinnersCount() + 1)
106
-			)
107
-			+ 1);
108
-	}
9
+    /**
10
+     * Election object
11
+     *
12
+     * @var \Michaelc\Voting\STV\Election;
13
+     */
14
+    protected $election;
15
+
16
+    /**
17
+     * Array of all ballots in election
18
+     *
19
+     * @var \MichaelC\Voting\STV\Ballot[]
20
+     */
21
+    protected $ballots;
22
+
23
+    /**
24
+     * Quota of votes needed for a candidate to be elected
25
+     *
26
+     * @var int
27
+     */
28
+    protected $quota;
29
+
30
+    /**
31
+     * Constructor
32
+     *
33
+     * @param Election $election
34
+     */
35
+    public function __construct(Election $election)
36
+    {
37
+        $this->election = $election;
38
+        $this->ballots = $this->election->getBallots();
39
+        $this->quota = $this->getQuota();
40
+    }
41
+
42
+    public function step($step)
43
+    {
44
+        foreach ($this->ballots as $i => $ballot)
45
+        {
46
+            $this->allocateVotes($ballot);
47
+        }
48
+
49
+        $candidates = $election->getActiveCandidates();
50
+
51
+        foreach ($candidates as $i => $candidate)
52
+        {
53
+            if ($candidate->getVotes() >= $this->quota)
54
+            {
55
+                $this->electCandidate($candidate);
56
+            }
57
+        }
58
+    }
59
+
60
+    protected function allocateVotes(&$ballot): Ballot
61
+    {
62
+        $weight = $ballot->getWeight();
63
+        $candidate = $ballot->getNextPreference();
64
+        $this->election->getCandidate($candidate->getId())->addVotes($weight);
65
+        $ballot->setLastUsedLevel(($step - 1));
66
+
67
+        return $ballot;
68
+    }
69
+
70
+    protected function transferVotes(float $votes, Candidate $candidate)
71
+    {
72
+        return;
73
+    }
74
+
75
+    /**
76
+     * Elect a candidate after they've passed the threshold
77
+     *
78
+     * @param  \Michaelc\Voting\STV\Candidate $candidate
79
+     * @return null
80
+     */
81
+    protected function electCandidate(Candidate $candidate)
82
+    {
83
+        if ($candidate->getVotes() < $this->quota)
84
+        {
85
+            throw new Exception("We shouldn't be electing someone who hasn't met the quota");
86
+        }
87
+
88
+        $candidate->setState(Candidate::ELECTED);
89
+        $surplus = $candidate->getVotes() - $this->quota;
90
+
91
+        $this->transferVotes($surplus, $candidate);
92
+
93
+        return;
94
+    }
95
+
96
+    /**
97
+     * Get the quota to win
98
+     *
99
+     * @return int
100
+     */
101
+    public function getQuota(): int
102
+    {
103
+        return floor(
104
+            ($this->election->getNumBallots() /
105
+                ($this->election->getWinnersCount() + 1)
106
+            )
107
+            + 1);
108
+    }
109 109
 }
Please login to merge, or discard this patch.