Completed
Branch master (fa2fc5)
by Boudry
05:50 queued 02:29
created

Ftpt::compute()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 42
nop 0
dl 0
loc 37
ccs 22
cts 22
cp 1
crap 8
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/*
3
    Part of FTPT method Module - From the original Condorcet PHP
4
5
    Condorcet PHP - Election manager and results calculator.
6
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
7
8
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
9
    https://github.com/julien-boudry/Condorcet
10
*/
11
declare(strict_types=1);
12
13
namespace Condorcet\Algo\Methods;
14
15
use Condorcet\Algo\Method;
16
use Condorcet\Algo\MethodInterface;
17
18
use Condorcet\Result;
19
20
class Ftpt extends Method implements MethodInterface
21
{
22
    // Method Name
23
    public const METHOD_NAME = ['First-past-the-post voting', 'First-past-the-post', 'First Choice', 'FirstChoice', 'FTPT'];
24
25
    protected $_Stats;
26
27 4
    protected function getStats(): array
28
    {
29 4
        $stats = [];
30
31 4
        foreach ($this->_Stats as $candidateKey => $oneScore) :
32 4
            $stats[(string)$this->_selfElection->getCandidateId($candidateKey)] = $oneScore;
33
        endforeach;
34
35 4
        return $stats;
36
    }
37
38
39
/////////// COMPUTE ///////////
40
41
    //:: FTPT Count :://
42
43 4
    protected function compute(): void
44
    {
45 4
        $score = [];
46
47 4
        foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) :
48 4
            $score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0;
49
        endforeach;
50
51 4
        foreach ($this->_selfElection->getVotesManager() as $oneVote) :
52 4
            $weight = ($this->_selfElection->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
53
54 4
            for ($i = 0; $i < $weight; $i++) :
55 4
                $oneRanking = $oneVote->getContextualRanking($this->_selfElection);
56
57 4
                foreach ($oneRanking[1] as $oneCandidateInRank) :
58 4
                    $score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += 1 / count($oneRanking[1]);
59
                endforeach;
60
            endfor;
61
        endforeach;
62
63 4
        arsort($score, SORT_NUMERIC);
64
65 4
        $rank = 0;
66 4
        $lastScore = null;
67 4
        $result = [];
68 4
        foreach ($score as $candidateKey => $candidateScore) :
69 4
            if ($candidateScore === $lastScore) :
70 2
                $result[$rank][] = $candidateKey;
71
            else :
72 4
                $result[++$rank] = [$candidateKey];
73 4
                $lastScore = $candidateScore;
74
            endif;
75
        endforeach;
76
77 4
        $this->_Stats = $score;
78 4
        $this->_Result = $this->createResult($result);
79 4
    }
80
}
81