Completed
Push — master ( facd47...922b0c )
by Boudry
02:37
created

Result   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Test Coverage

Coverage 93.41%

Importance

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

27 Methods

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