Passed
Push — master ( 291666...ef8eea )
by Boudry
03:25
created

BordaCount::compute()   C

Complexity

Conditions 11
Paths 84

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 11.0048

Importance

Changes 0
Metric Value
cc 11
nc 84
nop 0
dl 0
loc 51
rs 6.9224
c 0
b 0
f 0
ccs 28
cts 29
cp 0.9655
crap 11.0048

How to fix   Long Method    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
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