Passed
Branch master (73ca69)
by Boudry
03:33
created

DodgsonQuick::computeDodgson()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 45
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.0368

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 24
cts 26
cp 0.9231
rs 4.909
c 0
b 0
f 0
cc 9
eloc 34
nc 72
nop 0
crap 9.0368
1
<?php
2
/*
3
    Dodgson part of the Condorcet PHP Class
4
5
    By Julien Boudry - MIT LICENSE (Please read LICENSE.txt)
6
    https://github.com/julien-boudry/Condorcet
7
*/
8
declare(strict_types=1);
9
10
namespace Condorcet\Algo\Methods;
11
12
use Condorcet\Algo\Method;
13
use Condorcet\Algo\MethodInterface;
14
use Condorcet\Algo\Tools\PairwiseStats;
15
use Condorcet\Result;
16
17
// DODGSON Quick is an approximation for Dodgson method | https://www.maa.org/sites/default/files/pdf/cmj_ftp/CMJ/September%202010/3%20Articles/6%2009-229%20Ratliff/Dodgson_CMJ_Final.pdf
18
class DodgsonQuick extends Method implements MethodInterface
19
{
20
    // Method Name
21
    public const METHOD_NAME = ['Dodgson Quick','DodgsonQuick','Dodgson Quick Winner'];
22
23 1
    public function getResult () : Result
24
    {
25
        // Cache
26 1
        if ( $this->_Result !== null ) :
27 1
            return $this->_Result;
28
        endif;
29
30 1
        $this->computeDodgson();
31
32 1
        return $this->_Result;
33
    }
34
35 1
    protected function getStats () : array
36
    {
37 1
        $stats = [];
38
39 1
        foreach ($this->_Stats as $candidateKey => $dodgsonQuickValue) :
0 ignored issues
show
Bug introduced by
The property _Stats does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 1
             $stats[(string) $this->_selfElection->getCandidateId($candidateKey)] = $dodgsonQuickValue;
41
        endforeach;
42
43 1
        return $stats;
44
    }
45
46
47
/////////// COMPUTE ///////////
48
49
    //:: DODGSON ALGORITHM. :://
50
51 1
    protected function computeDodgson () : void
52
    {
53 1
        $pairwise = $this->_selfElection->getPairwise(false);
54 1
        $HeadToHead = [];
55
56 1
        foreach ($pairwise as $candidateId => $CandidateStats) :
57 1
            foreach ($CandidateStats['lose'] as $opponentId => $CandidateLose) :
58 1
                if (($diff = $CandidateLose - $CandidateStats['win'][$opponentId]) >= 0) :
59 1
                    $HeadToHead[$candidateId][$opponentId] = $diff;
60
                endif;
61
            endforeach;
62
        endforeach;
63
64 1
        $dodgsonQuick = [];
65
66 1
        foreach ($HeadToHead as $candidateId => $CandidateTidemanScores) :
67 1
            $dodgsonQuick[$candidateId] = 0;
68
69 1
            foreach ($CandidateTidemanScores as $opponentId => $oneTidemanScore) :
70 1
                $dodgsonQuick[$candidateId] += ceil($oneTidemanScore / 2);
71
            endforeach;
72
        endforeach;
73 1
        asort($dodgsonQuick);
74
75 1
        $rank = 0;
76 1
        $result = [];
77
78 1
        if($basicCondorcetWinner = $this->_selfElection->getWinner(null)) :
79
            $result[++$rank][] = $this->_selfElection->getCandidateKey($basicCondorcetWinner);
80
        endif;
81
82 1
        $lastDodgsonQuickValue = null;
83
84 1
        foreach ($dodgsonQuick as $CandidateId => $dodgsonQuickValue) :
85 1
            if($lastDodgsonQuickValue === $dodgsonQuickValue) :
86
                $result[$rank][] = $CandidateId;
87
            else:
88 1
                $result[++$rank][] = $CandidateId;
89 1
                $lastDodgsonQuickValue = $dodgsonQuickValue;
90
            endif;
91
        endforeach;
92
93 1
        $this->_Stats = $dodgsonQuick;
94 1
        $this->_Result = $this->createResult($result);
95 1
    }
96
97
}
98