Completed
Branch master (fa2fc5)
by Boudry
05:50 queued 02:29
created

VotesManager   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 219
ccs 107
cts 107
cp 1
rs 8.3673
c 0
b 0
f 0
wmc 45

17 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ dataCallBack() 0 8 1
A hp$0 ➔ dataPrepareStoringAndFormat() 0 6 1
A __construct() 0 6 1
A setElection() 0 4 1
A preDeletedTask() 0 4 1
A offsetSet() 0 9 2
A offsetUnset() 0 5 1
A setStateToVote() 0 6 2
A getVoteKey() 0 5 2
A getFulllVotesListGenerator() 0 6 2
B getPartialVotesListGenerator() 0 20 8
A getVotesList() 0 14 3
A getVotesListGenerator() 0 8 2
C getVotesListAsString() 0 38 7
D countVotes() 0 28 9
B getDataContextObject() 0 26 1
A sumVotesWeight() 0 10 3

How to fix   Complexity   

Complex Class

Complex classes like VotesManager 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.

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 VotesManager, 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
12
namespace Condorcet\DataManager;
13
14
15
use Condorcet\CondorcetException;
16
use Condorcet\Election;
17
use Condorcet\Vote;
18
19
20
class VotesManager extends ArrayManager
21
{
22
23
/////////// Magic ///////////
24
25 130
    public function __construct (Election $election)
26
    {
27 130
        $this->setElection($election);
28
29 130
        parent::__construct();
30 130
    }
31
32 130
    public function setElection (Election $election)
33
    {
34 130
        $this->_link[0] = $election;
35 130
    }
36
37
/////////// Data CallBack for external drivers ///////////
38
39 7
    public function getDataContextObject () : DataContextInterface
40
    {
41
        $context = new Class implements DataContextInterface {
42
            public $election;
43
44 5
            public function dataCallBack ($data) : Vote
45
            {
46 5
                $vote = new Vote ($data);
47 5
                $this->election->checkVoteCandidate($vote);
48 5
                $vote->registerLink($this->election);
49
50 5
                return $vote;
51
            }
52
53 6
            public function dataPrepareStoringAndFormat ($data) : string
54
            {
55 6
                $data->destroyLink($this->election);
56
57 6
                return (string) $data;
58
            }
59
        };
60
61 7
        $context->election = $this->_link[0] ?? null;
62
63 7
        return $context;
64
    }
65
66 11
    protected function preDeletedTask ($object) : void
67
    {
68 11
        $object->destroyLink($this->_link[0]);
69 11
    }
70
71
/////////// Array Access - Specials improvements ///////////
72
73 116
    public function offsetSet($offset, $value) : void
74
    {
75 116
        if ($value instanceof Vote) :
76 116
            parent::offsetSet($offset,$value);
77 116
            $this->setStateToVote();
78
        else :
79 1
            throw new CondorcetException (0,'Value must be an instanceof Condorcet\\Vote');
80
        endif;
81 116
    }
82
83 7
    public function offsetUnset($offset) : void
84
    {
85 7
        parent::offsetUnset($offset);
86 7
        $this->setStateToVote();
87 7
    }
88
89
/////////// Internal Election related methods ///////////
90
91 116
    protected function setStateToVote () : void
92
    {
93 116
        foreach ($this->_link as $element) :
94 116
            $element->setStateToVote();
95
        endforeach;
96 116
    }
97
98
/////////// Get Votes Methods ///////////
99
100 6
    public function getVoteKey (Vote $vote) {
101 6
        ($r = array_search($vote, $this->_Container, true)) !== false || ($r = array_search($vote, $this->_Cache, true));
102
103 6
        return $r;
104
    }
105
106 2
    protected function getFulllVotesListGenerator () : \Generator
107
    {
108 2
        foreach ($this as $voteKey => $vote) :
109 2
            yield $voteKey => $vote;
110
        endforeach;
111 2
    }
112
113 7
    protected function getPartialVotesListGenerator (array $tag, bool $with) : \Generator
114
    {
115 7
        foreach ($this as $voteKey => $vote) :
116 7
            $noOne = true;
117 7
            foreach ($tag as $oneTag) :
118 7
                if ( ( $oneTag === $voteKey ) || in_array($oneTag, $vote->getTags(),true) ) :
119 7
                    if ($with) :
120 7
                        yield $voteKey => $vote;
121 7
                        break;
122
                    else :
123 7
                        $noOne = false;
124
                    endif;
125
                endif;
126
            endforeach;
127
128 7
            if (!$with && $noOne) :
129 7
                yield $voteKey => $vote;
130
            endif;
131
        endforeach;
132 7
    }
133
134
    // Get the votes list
135 14
    public function getVotesList ($tag = null, bool $with = true) : array
136
    {
137 14
        if ($tag === null) :
138 12
            return $this->getFullDataSet();
139
        else :
140 6
            $search = [];
141
142 6
            foreach ($this->getPartialVotesListGenerator($tag,$with) as $voteKey => $vote) :
143 6
                $search[$voteKey] = $vote;
144
            endforeach;
145
146 6
            return $search;
147
        endif;
148
    }
149
150
    // Get the votes list as a generator object
151 2
    public function getVotesListGenerator ($tag = null, bool $with = true) : \Generator
152
    {
153 2
        if ($tag === null) :
154 2
            return $this->getFulllVotesListGenerator();
155
        else :
156 2
            return $this->getPartialVotesListGenerator($tag,$with);
157
        endif;
158
    }
159
160 5
    public function getVotesListAsString () : string
161
    {
162 5
        $simpleList = '';
163
164 5
        $weight = [];
165 5
        $nb = [];
166
167 5
        foreach ($this as $oneVote) :
168 5
            $oneVoteString = $oneVote->getSimpleRanking($this->_link[0]);
169
170 5
            if(!array_key_exists($oneVoteString, $weight)) :
171 5
                $weight[$oneVoteString] = 0;
172
            endif;
173 5
            if(!array_key_exists($oneVoteString, $nb)) :
174 5
                $nb[$oneVoteString] = 0;
175
            endif;
176
177 5
            if ($this->_link[0]->isVoteWeightIsAllowed()) :
178 1
                $weight[$oneVoteString] += $oneVote->getWeight();
179
            else :
180 4
                $weight[$oneVoteString]++;
181
            endif;
182
183 5
            $nb[$oneVoteString]++;
184
        endforeach;
185
186 5
        ksort($weight);
187 5
        arsort($weight);
188
189 5
        $isFirst = true;
190 5
        foreach ($weight as $key => $value) :
191 5
            if (!$isFirst) : $simpleList .= "\n"; endif;
192 5
            $simpleList .= $key.' * '.$nb[$key];
193 5
            $isFirst = false;
194
        endforeach;
195
196 5
        return $simpleList;
197
    }
198
199 11
    public function countVotes (?array $tag, bool $with) : int
200
    {
201 11
        if ($tag === null) :
202 8
            return count($this);
203
        else :
204 5
            $count = 0;
205
206 5
            foreach ($this as $key => $value) :
207 5
                $noOne = true;
208 5
                foreach ($tag as $oneTag) :
209 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
210 5
                        if ($with) :
211 5
                            $count++;
212 5
                            break;
213
                        else :
214 5
                            $noOne = false;
215
                        endif;
216
                    endif;
217
                endforeach;
218
219 5
                if (!$with && $noOne) :
220 5
                    $count++;
221
                endif;
222
            endforeach;
223
224 5
            return $count;
225
        endif;
226
    }
227
228 6
    public function sumVotesWeight () : int
229
    {
230 6
        $sum = 0;
231
232 6
        foreach ($this as $oneVote) :
233 6
            $sum += ($this->_link[0]->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
234
        endforeach;
235
236 6
        return $sum;
237
    }
238
}
239