Completed
Push — master ( c698df...2bf777 )
by Michael
03:54
created

VoteHandler::getQuota()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
use Michaelc\Voting\STV\{Election, 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\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
	protected $election;
10
	protected $ballots;
11
12
	/**
13
	 * Constructor
14
	 *
15
	 * @param Election $election
16
	 */
17
	public function __construct(Election $election)
18
	{
19
		$this->election = $election;
20
		$this->ballots = $this->election->getBallots();
21
	}
22
23
	public function step($step)
24
	{
25
		foreach ($this->ballots as $i => $ballot)
26
		{
27
			$weight = $ballot->getWeight();
28
			$candidate = $ballot->getNextPreference();
29
			$election->getCandidate($candidate->getId())->addVotes($weight);
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...
30
			$ballot->setLastUsedLevel(($step - 1));
31
		}
32
33
		$candidates = $election->getActiveCandidates();
34
35
		foreach ($candidates as $i => $candidate) {
36
			if ($candidate->getVotes() >= $this->getQuota())
37
			{
38
        		$candidate->setState(Candidate::ELECTED);
39
        		$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...
40
41
        		$this->transferVotes($surplus, $candidate);
42
        	}
43
		}
44
	}
45
46
	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...
47
	{
48
49
	}
50
51
	public function getQuota()
52
	{
53
		return floor(($election->getNumBallots() / ($election->getNumSeats() + 1)) + 1);
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...
54
	}
55
}
56