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

BordaCount::compute()   D

Complexity

Conditions 10
Paths 78

Size

Total Lines 45
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 78
nop 0
dl 0
loc 45
ccs 27
cts 27
cp 1
crap 10
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 5
            $weight = ($this->_selfElection->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
55
56 5
            for ($i = 0 ; $i < $weight ; $i++) :
57 5
                $CandidatesRanked = 0;
58 5
                $oneRanking = $oneVote->getContextualRanking($this->_selfElection);
59
60 5
                foreach ($oneRanking as $oneRank) :
61 5
                    $rankScore = 0;
62 5
                    foreach ($oneRank as $oneCandidateInRank) :
63 5
                        $rankScore += $this->getScoreByCandidateRanking($CandidatesRanked++);
64
                    endforeach;
65
66 5
                    foreach ($oneRank as $oneCandidateInRank) :
67 5
                        $score[$this->_selfElection->getCandidateKey($oneCandidateInRank)] += $rankScore / count($oneRank);
68
                    endforeach;
69
                endforeach;
70
            endfor;
71
        endforeach;
72
73 5
        arsort($score,SORT_NUMERIC);
74
75 5
        $rank = 0;
76 5
        $lastScore = null;
77 5
        $result = [];
78 5
        foreach ($score as $candidateKey => $candidateScore) :
79 5
            if ($candidateScore === $lastScore) :
80 1
                $result[$rank][] = $candidateKey;
81
            else :
82 5
                $result[++$rank] = [$candidateKey];
83 5
                $lastScore = $candidateScore;
84
            endif;
85
        endforeach;
86
87 5
        $this->_Stats = $score;
88 5
        $this->_Result = $this->createResult($result);
89 5
    }
90
91 4
    protected function getScoreByCandidateRanking (int $CandidatesRanked) : float
92
    {
93 4
        return $this->_selfElection->countCandidates() + static::$starting - 1 - $CandidatesRanked;
94
    }
95
}
96