Completed
Push — master ( 09054f...2cb152 )
by Michael
02:20
created

ElectionFactory::createCandidateBallotCollection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 2
crap 3
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
9
class ElectionFactory
10
{
11 5
	public static function createBallotCollection(array $rankings): array
12
	{
13 5
		$ballotCollection = [];
14
15 5
		foreach ($rankings as $ranking) {
16 5
			$ballotCollection[] = new Ballot($ranking);
17
		}
18
19 5
		return $ballotCollection;
20
	}
21
22 1
	public static function createCandidateCollection(array $candidates): array
23
	{
24 1
		$candidateCollection = [];
25
26 1
		$candidates = array_values($candidates);
27
28 1
		foreach ($candidates as $i => $candidate) {
29 1
			$candidateCollection[] = new Candidate($i);
30
		}
31
32 1
		return $candidateCollection;
33
	}
34
35 1
	public static function createCandidateBallotCollection(array $candidates, array $rankings): array
36
	{
37 1
		$candidateMatchup = $candidateCollection = [];
38
39 1
		$candidates = array_values($candidates);
40
41 1
		foreach ($candidates as $i => $name) {
42 1
			$candidateCollection[] = new Candidate($i);
43 1
			$candidateMatchup[$name] = $i;
44
		}
45
46 1
		foreach ($rankings as &$ranking) {
47 1
			array_walk($ranking, function (&$value, $key, $candidateMatchup) {
48 1
			    $value = $candidateMatchup[$value];
49 1
			}, $candidateMatchup);
50
		}
51
52 1
		$ballotCollection = self::createBallotCollection($rankings);
53
54 1
		return ['ballots' => $ballotCollection, 'candidates' => $candidateCollection];
55
	}
56
57
	public static function createElection(array $candidates, array $rankings, int $winnerCount, bool $ids = true): Election
58
	{
59
		if ($ids) {
60
			$candidateCollection = self::createCandidateSet($candidates);
0 ignored issues
show
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...
61
			$ballotCollection = self::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...
62
		} else {
63
			$collections = self::createCandidateBallotCollection($candidates, $rankings);
64
			$candidates = $collections['candidates'];
65
			$ballots = $collections['ballots'];
66
		}
67
68
		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...
69
	}
70
}
71