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

DodgsonQuick   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 80
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 11 2
A getStats() 0 10 2
D computeDodgson() 0 45 9
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