Completed
Branch dev-1.8.x (7905e8)
by Boudry
03:47
created

BordaCount   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 35
cts 36
cp 0.9722
wmc 14
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStats() 0 10 2
C compute() 0 51 11
A getScoreByCandidateRanking() 0 4 1
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 BordaCount extends Method implements MethodInterface
21
{
22
    // Method Name
23
    public const METHOD_NAME = ['BordaCount','Borda Count','Borda','Méthode Borda'];
24
25
    public static $starting = 1;
26
27
    protected $_Stats;
28
29 5
    protected function getStats () : array
30
    {
31 5
        $stats = [];
32
33 5
        foreach ($this->_Stats as $candidateKey => $oneScore) :
34 5
             $stats[(string) $this->_selfElection->getCandidateId($candidateKey)] = $oneScore;
35
        endforeach;
36
37 5
        return $stats;
38
    }
39
40
41
/////////// COMPUTE ///////////
42
43
    //:: BORDA ALGORITHM. :://
44
45 5
    protected function compute () : void
46
    {
47 5
        $score = [];
48
49 5
        foreach ($this->_selfElection->getCandidatesList() as $oneCandidate) :
50 5
            $score[$this->_selfElection->getCandidateKey($oneCandidate)] = 0;
51
        endforeach;
52
53 5
        foreach ($this->_selfElection->getVotesManager() as $oneVote) :
54
55
            // Ignore vote who don't respect election constraints
56 5
            if(!$this->_selfElection->testIfVoteIsValidUnderElectionConstraints($oneVote)) :
57
                continue ;
58
            endif;
59
60 5
            $weight = ($this->_selfElection->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
61
62 5
            for ($i = 0 ; $i < $weight ; $i++) :
63 5
                $CandidatesRanked = 0;
64 5
                $oneRanking = $oneVote->getContextualRanking($this->_selfElection);
65
66 5
                foreach ($oneRanking as $oneRank) :
67 5
                    $rankScore = 0;
68 5
                    foreach ($oneRank as $oneCandidateInRank) :
69 5
                        $rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++);
70
                    endforeach;
71
72 5
                    foreach ($oneRank as $oneCandidateInRank) :
73 5
                        $score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank);
74
                    endforeach;
75
                endforeach;
76
            endfor;
77
        endforeach;
78
79 5
        arsort($score,SORT_NUMERIC);
80
81 5
        $rank = 0;
82 5
        $lastScore = null;
83 5
        $result = [];
84 5
        foreach ($score as $candidateKey => $candidateScore) :
85 5
            if ($candidateScore === $lastScore) :
86 1
                $result[$rank][] = $candidateKey;
87
            else :
88 5
                $result[++$rank] = [$candidateKey];
89 5
                $lastScore = $candidateScore;
90
            endif;
91
        endforeach;
92
93 5
        $this->_Stats = $score;
94 5
        $this->_Result = $this->createResult($result);
95 5
    }
96
97 4
    protected function getScoreByCandidateRanking (int $CandidatesRanked) : float
98
    {
99 4
        return $this->_selfElection->countCandidates() + static::$starting - 1 - $CandidatesRanked;
100
    }
101
}
102