Completed
Branch dev-1.7.x (6a7528)
by Boudry
05:58
created

BordaCount1::computeBorda()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.004

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 36
nop 0
dl 0
loc 41
ccs 24
cts 25
cp 0.96
crap 8.004
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/*
3
    Part of BORDA COUNT 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 BordaCount1 extends Method implements MethodInterface
21
{
22
    // Method Name
23
    public const METHOD_NAME = ['BordaCount','Borda Count','Borda','BordaCount1','Borda Count 1','Borda1'];
24
25
    public const STARTING = 1;
26
27
    protected $_Stats;
28
29 4
    public function getResult () : Result
30
    {
31
        // Cache
32 4
        if ( $this->_Result !== null ) :
33 4
            return $this->_Result;
34
        endif;
35
36 4
        $this->computeBorda();
37
38 4
        return $this->_Result;
39
    }
40
41 4
    protected function getStats () : array
42
    {
43 4
        $stats = [];
44
45 4
        foreach ($this->_Stats as $candidateKey => $oneScore) :
46 4
             $stats[(string) $this->_selfElection->getCandidateId($candidateKey)] = $oneScore;
47
        endforeach;
48
49 4
        return $stats;
50
    }
51
52
53
/////////// COMPUTE ///////////
54
55
    //:: BORDA ALGORITHM. :://
56
57 4
    protected function computeBorda () : void
58
    {
59 4
        $score = [];
60
61 4
        foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) :
62 4
            $score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0;
63
        endforeach;
64
65 4
        foreach ($this->_selfElection->getVotesManager() as $oneVote) :
66 4
            $CandidatesRanked = 0;
67 4
            $oneRanking = $oneVote->getContextualRanking($this->_selfElection);
68
69 4
            foreach ($oneRanking as $oneRank) :
70 4
                $rankScore = 0;
71 4
                foreach ($oneRank as $oneCandidateInRank) :
72 4
                    $rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++);
73
                endforeach;
74
75 4
                foreach ($oneRank as $oneCandidateInRank) :
76 4
                    $score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank);
77
                endforeach;
78
            endforeach;
79
        endforeach;
80
81 4
        arsort($score,SORT_NUMERIC);
82
83 4
        $rank = 0;
84 4
        $lastScore = null;
85 4
        $result = [];
86 4
        foreach ($score as $candidateKey => $candidateScore) :
87 4
            if ($candidateScore === $lastScore) :
88
                $result[$rank][] = $candidateKey;
89
            else :
90 4
                $result[++$rank] = [$candidateKey];
91 4
                $lastScore = $candidateScore;
92
            endif;
93
        endforeach;
94
95 4
        $this->_Stats = $score;
96 4
        $this->_Result = $this->createResult($result);
97 4
    }
98
99 4
    protected function getScoreByCandidateRanking (int $CandidatesRanked) : int
100
    {
101 4
        return $this->_selfElection->countCandidates() + static::STARTING - 1 - $CandidatesRanked;
102
    }
103
}
104