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

Pairwise   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 3
dl 0
loc 160
ccs 64
cts 66
cp 0.9697
rs 9.8
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 1 1
A offsetExists() 0 3 1
A offsetUnset() 0 1 1
A offsetGet() 0 3 1
A rewind() 0 4 1
A current() 0 3 1
A key() 0 3 1
A next() 0 5 2
A valid() 0 3 1
A __construct() 0 5 1
A getExplicitPairwise() 0 20 4
D doPairwise() 0 81 16
1
<?php
2
/*
3
    Condorcet PHP Class, with Schulze Methods and others !
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
11
namespace Condorcet\Algo;
12
13
use Condorcet\CondorcetVersion;
14
use Condorcet\Election;
15
use Condorcet\Timer\Chrono as Timer_Chrono;
16
17
class Pairwise implements \ArrayAccess,\Iterator
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "Iterator"; 0 found
Loading history...
18
{
19
    use CondorcetVersion;
20
21
    // Implement ArrayAccess
22
    public function offsetSet($offset, $value) : void {}
23
24
    public function offsetExists($offset) : bool {
25
        return isset($this->_Pairwise[$offset]);
26
    }
27
28
    public function offsetUnset($offset) : void {}
29
30 4
    public function offsetGet($offset) : ?array {
31 4
        return $this->_Pairwise[$offset] ?? null;
32
    }
33
34
35
    // Implement Iterator
36
    private $valid = true;
37
38 4
    public function rewind() : void {
39 4
        reset($this->_Pairwise);
40 4
        $this->valid = true;
41 4
    }
42
43 4
    public function current() : array {
44 4
        return $this->_Pairwise[$this->key()];
45
    }
46
47 4
    public function key() : ?int {
48 4
        return key($this->_Pairwise);
49
    }
50
51 4
    public function next() : void {
52 4
        if (next($this->_Pairwise) === false) :
53 4
            $this->valid = false;
54
        endif;
55 4
    }
56
57 4
    public function valid() : bool {
58 4
        return $this->valid;
59
    }   
60
61
62
    // Pairwise
63
64
    protected $_Election;
65
    protected $_Pairwise = [];
66
67 4
    public function __construct (Election &$link)
68
    {
69 4
        $this->_Election = $link;
70 4
        $this->doPairwise();
71 4
    }
72
73 2
    public function getExplicitPairwise () : array
74
    {
75 2
        $explicit_pairwise = [];
76
77 2
        foreach ($this->_Pairwise as $candidate_key => $candidate_value) :
78
79 2
            $candidate_name = $this->_Election->getCandidateId($candidate_key, true);
80
            
81 2
            foreach ($candidate_value as $mode => $mode_value) :
82
83 2
                foreach ($mode_value as $candidate_list_key => $candidate_list_value) :
84 2
                    $explicit_pairwise[$candidate_name][$mode][$this->_Election->getCandidateId($candidate_list_key, true)] = $candidate_list_value;
85
                endforeach;
86
87
            endforeach;
88
89
        endforeach;
90
91 2
        return $explicit_pairwise;
92
    }
93
94 4
    protected function doPairwise () : void
95
    {
96
        // Chrono
97 4
        $chrono = new Timer_Chrono ( $this->_Election->getTimerManager(), 'Do Pairwise' );
0 ignored issues
show
Unused Code introduced by
$chrono is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
99 4
        foreach ( $this->_Election->getCandidatesList(false) as $candidate_key => $candidate_id ) :
100
101 4
            $this->_Pairwise[$candidate_key] = [ 'win' => [], 'null' => [], 'lose' => [] ];
102
103 4
            foreach ( $this->_Election->getCandidatesList(false) as $candidate_key_r => $candidate_id_r ) :
104
105 4
                if ($candidate_key_r !== $candidate_key) :
106 4
                    $this->_Pairwise[$candidate_key]['win'][$candidate_key_r]   = 0;
107 4
                    $this->_Pairwise[$candidate_key]['null'][$candidate_key_r]  = 0;
108 4
                    $this->_Pairwise[$candidate_key]['lose'][$candidate_key_r]  = 0;
109
                endif;
110
111
            endforeach;
112
113
        endforeach;
114
115
        // Win && Null
116 4
        foreach ( $this->_Election->getVotesManager() as $vote_id => $oneVote ) :
117 4
            $vote_ranking = $oneVote->getContextualRanking($this->_Election);
118
119 4
            $voteWeight = ($this->_Election->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
120
121 4
            $vote_candidate_list = (function (array $r) : array { $list = [];
122 4
                    foreach ($r as $rank) :
123 4
                        foreach ($rank as $oneCandidate) :
124 4
                            $list[] = $oneCandidate;
125
                        endforeach;
126
                    endforeach;
127
128 4
                    return $list;})($vote_ranking);
129
130 4
            $done_Candidates = [];
131
132 4
            foreach ($vote_ranking as $candidates_in_rank) :
133
134 4
                $candidates_in_rank_keys = [];
135
136 4
                foreach ($candidates_in_rank as $candidate) :
137 4
                    $candidates_in_rank_keys[] = $this->_Election->getCandidateKey($candidate);
138
                endforeach;
139
140 4
                foreach ($candidates_in_rank as $candidate) :
141
142 4
                    $candidate_key = $this->_Election->getCandidateKey($candidate);
143
144
                    // Process
145 4
                    foreach ( $vote_candidate_list as $g_Candidate ) :
146
147 4
                        $g_candidate_key = $this->_Election->getCandidateKey($g_Candidate);
148
149 4
                        if ($candidate_key === $g_candidate_key) :
150 4
                            continue;
151
                        endif;
152
153
                        // Win & Lose
154 4
                        if (    !in_array($g_candidate_key, $done_Candidates, true) && 
155 4
                                !in_array($g_candidate_key, $candidates_in_rank_keys, true) ) :
156
157 4
                            $this->_Pairwise[$candidate_key]['win'][$g_candidate_key] += $voteWeight;
158 4
                            $this->_Pairwise[$g_candidate_key]['lose'][$candidate_key] += $voteWeight;
159
160 4
                            $done_Candidates[] = $candidate_key;
161
162
                        // Null
163 4
                        elseif (in_array($g_candidate_key, $candidates_in_rank_keys, true)) :
164 4
                            $this->_Pairwise[$candidate_key]['null'][$g_candidate_key] += $voteWeight;
165
                        endif;
166
167
                    endforeach;
168
169
                endforeach;
170
171
            endforeach;
172
173
        endforeach;
174 4
    }
175
176
}