Completed
Push — master ( 2bf777...8cb5a2 )
by Michael
03:38
created

VoteHandler::transferVotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
use Michaelc\Voting\STV\{Election, Ballot, Candidate};
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Michaelc\Voting\STV\Election.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Michaelc\Voting\STV\Ballot.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Michaelc\Voting\STV\Candidate.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
class VoteHandler
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 1
	public function __construct(Election $election)
36
	{
37 1
		$this->election = $election;
38 1
		$this->ballots = $this->election->getBallots();
39 1
		$this->quota = $this->getQuota();
40 1
	}
41
42
	public function step($step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
	{
44
		foreach ($this->ballots as $i => $ballot)
45
		{
46
			$this->allocateVotes($ballot);
47
		}
48
49
		$candidates = $election->getActiveCandidates();
0 ignored issues
show
Bug introduced by
The variable $election does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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));
0 ignored issues
show
Bug introduced by
The variable $step does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
67
		return $ballot;
68
	}
69
70
	protected function transferVotes(float $votes, Candidate $candidate)
0 ignored issues
show
Unused Code introduced by
The parameter $votes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $candidate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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() - $quota;
0 ignored issues
show
Bug introduced by
The variable $quota does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
90
91
		$this->transferVotes($surplus, $candidate);
0 ignored issues
show
Unused Code introduced by
The call to the method Michaelc\Voting\STV\VoteHandler::transferVotes() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
92
93
		return;
94
	}
95
96
	/**
97
	 * Get the quota to win
98
	 *
99
	 * @return int
100
	 */
101 1
	public function getQuota(): int
102
	{
103 1
		return floor(
104 1
			($this->election->getNumBallots() /
105 1
				($this->election->getWinnersCount() + 1)
106
			)
107 1
			+ 1);
108
	}
109
}
110