Completed
Push — master ( e86a88...9f06ff )
by Boudry
04:59 queued 02:04
created

Result   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Test Coverage

Coverage 95.6%

Importance

Changes 0
Metric Value
wmc 42
lcom 4
cbo 5
dl 0
loc 207
ccs 87
cts 91
cp 0.956
rs 8.295
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A rewind() 0 3 1
A current() 0 3 1
A key() 0 3 1
A next() 0 3 1
A valid() 0 3 2
A offsetSet() 0 3 1
A offsetExists() 0 3 1
A offsetUnset() 0 3 1
A offsetGet() 0 3 1
A count() 0 3 1
A __construct() 0 13 1
B getResultAsArray() 0 16 6
A getResultAsString() 0 4 1
A getOriginalArrayWithString() 0 4 1
A getResultAsInternalKey() 0 4 1
A getStats() 0 3 1
A getWinner() 0 3 1
A getLoser() 0 3 1
A getCondorcetWinner() 0 3 1
A getCondorcetLoser() 0 3 1
C makeUserResult() 0 24 7
A addWarning() 0 6 1
A getWarning() 0 16 4
A getClassGenerator() 0 3 1
A getMethod() 0 3 1
A getBuildTimeStamp() 0 3 1
A getCondorcetElectionGeneratorVersion() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Result often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Result, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
namespace Condorcet;
12
13
use Condorcet\CondorcetException;
14
use Condorcet\CondorcetVersion;
15
use Condorcet\Election;
16
use Condorcet\CondorcetUtil;
17
use Condorcet\Vote;
18
use Condorcet\ElectionProcess\VoteUtil;
19
20
21
class Result implements \ArrayAccess, \Countable, \Iterator 
22
{
23
    use CondorcetVersion;
24
25
    // Implement Iterator
26
27 1
    public function rewind() :void {
28 1
        reset($this->_UserResult);
29 1
    }
30
31 1
    public function current () {
32 1
        return current($this->_UserResult);
33
    }
34
35 1
    public function key () : int {
36 1
        return key($this->_UserResult);
37
    }
38
39 1
    public function next () : void {
40 1
        next($this->_UserResult);
41 1
    }
42
43 1
    public function valid () : bool {
44 1
        return (key($this->_UserResult) === null) ? false : true;
45
    }
46
47
    // Implement ArrayAccess
48
49 1
    public function offsetSet ($offset, $value) : void {
50 1
        throw new CondorcetException (0,"Can't change a result");
51
    }
52
53 1
    public function offsetExists ($offset) : bool {
54 1
        return isset($this->_UserResult[$offset]);
55
    }
56
57 1
    public function offsetUnset ($offset) : void {
58 1
        throw new CondorcetException (0,"Can't change a result");
59
    }
60
61 50
    public function offsetGet ($offset) {
62 50
        return $this->_UserResult[$offset] ?? null;
63
    }
64
65
    // Implement Countable
66
67 3
    public function count () : int {
68 3
        return count($this->_UserResult);
69
    }
70
71
72
/////////// CONSTRUCTOR ///////////
73
74
    protected $_Result;
75
    protected $_UserResult;
76
    protected $_stringResult;
77
    protected $_CondorcetWinner;
78
    protected $_CondorcetLoser;
79
80
    protected $_Stats;
81
82
    protected $_warning = [];
83
84
    protected $_BuildTimeStamp;
85
    protected $_fromMethod;
86
    protected $_byClass;
87
    protected $_ElectionCondorcetVersion;
88
89
90 72
    public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats)
91
    {
92 72
        $this->_Result = $result;
93 72
        $this->_UserResult = $this->makeUserResult($election);
94 72
        $this->_stringResult = $this->getResultAsArray(true);
95 72
        $this->_Stats = $stats;
96 72
        $this->_fromMethod = $fromMethod;
97 72
        $this->_byClass = $byClass;
98 72
        $this->_ElectionCondorcetVersion = $election->getObjectVersion();
99 72
        $this->_CondorcetWinner = $election->getWinner();
100 72
        $this->_CondorcetLoser = $election->getLoser();
101 72
        $this->_BuildTimeStamp = microtime(true);
102 72
    }
103
104
105
/////////// Get Result ///////////
106
107 72
    public function getResultAsArray (bool $convertToString = false) : array
108
    {
109 72
        $r = $this->_UserResult;
110
111 72
        foreach ($r as &$rank) :
112 72
            if (count($rank) === 1) :
113 69
                $rank = ($convertToString) ? (string) $rank[0] : $rank[0];
114 25
            elseif ($convertToString) :
115 25
                foreach ($rank as &$subRank) :
116 72
                    $subRank = (string) $subRank;
117
                endforeach;
118
            endif;
119
        endforeach;
120
121 72
        return $r;
122
    }
123
124 3
    public function getResultAsString ()
125
    {
126 3
        return VoteUtil::getRankingAsString($this->getResultAsArray(true));
127
    }
128
129 1
    public function getOriginalArrayWithString () : array
130
    {
131 1
        return $this->_stringResult;
132
    }
133
134
    public function getResultAsInternalKey () : array
135
    {
136
        return $this->_Result;
137
    }
138
139 11
    public function getStats () {
140 11
        return $this->_Stats;
141
    }
142
143 49
    public function getWinner () {
144 49
        return CondorcetUtil::format($this[1],false);
145
    }
146
147 3
    public function getLoser () {
148 3
        return CondorcetUtil::format($this[count($this)],false);
149
    }
150
151 1
    public function getCondorcetWinner () {
152 1
        return $this->_CondorcetWinner;
153
    }
154
155 1
    public function getCondorcetLoser () {
156 1
        return $this->_CondorcetLoser;
157
    }
158
159 72
    protected function makeUserResult (Election $election) : array
160
    {
161 72
        $userResult = [];
162
163 72
        foreach ( $this->_Result as $key => $value ) :
164 72
            if (is_array($value)) :
165 69
                foreach ($value as $candidate_key) :
166 69
                    $userResult[$key][] = $election->getCandidateId($candidate_key);
167
                endforeach;
168 5
            elseif (is_null($value)) :
169
                $userResult[$key] = null;
170
            else :
171 72
                $userResult[$key][] = $election->getCandidateId($value);
172
            endif;
173
        endforeach;
174
175 72
        foreach ( $userResult as $key => $value ) :
176 72
            if (is_null($value)) :
177 72
                $userResult[$key] = null;
178
            endif;
179
        endforeach;
180
181 72
        return $userResult;
182
    }
183
184
185
/////////// Get & Set MetaData ///////////
186
187 1
    public function addWarning (int $type, string $msg = null) : bool
188
    {
189 1
        $this->_warning[] = ['type' => $type, 'msg' => $msg];
190
191 1
        return true;
192
    }
193
194 2
    public function getWarning ($type = null) : array
195
    {
196 2
        if ($type === null) :
197
            return $this->_warning;
198
        else :
199 2
            $r = [];
200
201 2
            foreach ($this->_warning as $oneWarning) :
202 1
                if ($oneWarning['type'] === (int) $type) :
203 1
                    $r[] = $oneWarning;
204
                endif;
205
            endforeach;
206
207 2
            return $r;
208
        endif;
209
    }
210
211 1
    public function getClassGenerator () : string {
212 1
        return $this->_byClass;
213
    }
214
215 1
    public function getMethod () : string {
216 1
        return $this->_fromMethod;
217
    }
218
219 1
    public function getBuildTimeStamp () : float {
220 1
        return (float) $this->_BuildTimeStamp;
221
    }
222
223 1
    public function getCondorcetElectionGeneratorVersion () : string {
224 1
        return $this->_ElectionCondorcetVersion;
225
    }
226
227
}
228