Completed
Push — master ( c89715...09054f )
by Michael
02:29
created

ElectionFactory::createCandidateCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
use Psr\Log\LoggerInterface as Logger;
6
use Michaelc\Voting\Exception\VotingLogicException as LogicException;
7
use Michaelc\Voting\Exception\VotingRuntimeException as RuntimeException;
8
use Michaelc\Voting\ElectionFactory as ElectionFactoryInterface;
9
use Michaelc\Voting\Election as ElectionInterface;
10
11
class ElectionFactory implements ElectionFactoryInterface
12
{
13 4
	public static function createBallotCollection(array $rankings): array
14
	{
15 4
		$ballotCollection = [];
16
17 4
		foreach ($rankings as $ranking) {
18 4
			$ballotCollection[] = new Ballot($ranking);
19
		}
20
21 4
		return $ballotCollection;
22
	}
23
24 1
	public static function createCandidateCollection(array $candidates): array
25
	{
26 1
		$candidateCollection = [];
27
28 1
		$candidates = array_values($candidates);
29
30 1
		foreach ($candidates as $i => $candidate) {
31 1
			$candidateCollection[] = new Candidate($i);
32
		}
33
34 1
		return $candidateCollection;
35
	}
36
37
	public static function createCandidateBallotCollection(array $candidates, array $rankings): array
38
	{
39
		$candidateMatchup = $candidateCollection = [];
40
41
		$candidates = array_values($candidates);
42
43
		foreach ($candidates as $i => $name) {
44
			$candidateCollection[] = new Candidate($i);
45
			$candidateMatchup[$name] = $i;
46
		}
47
48
		foreach ($rankings as $ranking) {
49
			array_walk($ranking, function(&$value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
50
			    $value = $candidateMatchup[$value];
0 ignored issues
show
Bug introduced by
The variable $candidateMatchup 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...
51
			});
52
		}
53
54
		$ballotCollection = $this->createBallotCollection($rankings);
0 ignored issues
show
Bug introduced by
The variable $this 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...
55
56
		return ['ballots' => $ballotCollection, 'candidates' => $candidateCollection];
57
	}
58
59
	public static function createElection(array $candidates, array $rankings, int $winnerCount, bool $ids = true): ElectionInterface
60
	{
61
		if ($ids) {
62
			$candidateCollection = $this->createCandidateSet($candidates);
0 ignored issues
show
Bug introduced by
The variable $this 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...
Bug introduced by
The method createCandidateSet() does not seem to exist on object<Michaelc\Voting\STV\ElectionFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
$candidateCollection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
			$ballotCollection = $this->createBallotCollection($rankings);
0 ignored issues
show
Unused Code introduced by
$ballotCollection is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
		} else {
65
			$collections = $this->createCandidateBallotCollection($candidates, $rankings);
66
			$candidates = $collections['candidates'];
67
			$ballots = $collections['ballots'];
68
		}
69
70
		return new Election($winnerCount, $candidates, $ballots);
0 ignored issues
show
Bug introduced by
The variable $ballots does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Documentation introduced by
$winnerCount is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
	}
72
}
73