Completed
Push — dev-1.6.x ( e86a88...4ab371 )
by Boudry
03:36
created

VotesManager::getDataContextObject()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
crap 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A VotesManager.php$0 ➔ dataCallBack() 0 8 1
A VotesManager.php$0 ➔ dataPrepareStoringAndFormat() 0 6 1
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\DataManager;
12
13
use Condorcet\DataManager\ArrayManager;
14
use Condorcet\DataManager\DataContextInterface;
15
use Condorcet\CondorcetException;
16
use Condorcet\Election;
17
use Condorcet\Vote;
18
use Condorcet\ElectionProcess\VoteUtil;
19
20
class VotesManager extends ArrayManager
21
{
22
23
/////////// Magic ///////////
24
25 112
    public function __construct (Election $election = null)
26
    {
27 112
        $this->setElection($election);
0 ignored issues
show
Bug introduced by
It seems like $election defined by parameter $election on line 25 can be null; however, Condorcet\DataManager\VotesManager::setElection() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
28
29 112
        parent::__construct();
30 112
    }
31
32 112
    public function setElection (Election $election)
33
    {
34 112
        $this->_link[0] = $election;
35 112
    }
36
37
/////////// Data CallBack for external drivers ///////////
38
39
    public function getDataContextObject () : DataContextInterface
40
    {
41
        $context = new Class implements DataContextInterface {
42
            public $election;
43
44 4
            public function dataCallBack ($data) : Vote
45
            {
46 4
                $vote = new Vote ($data);
47 4
                $this->election->checkVoteCandidate($vote);
48 4
                $vote->registerLink($this->election);
49
50 4
                return $vote;
51
            }
52
53 4
            public function dataPrepareStoringAndFormat ($data) : string
54
            {
55 4
                $data->destroyLink($this->election);
56
57 4
                return (string) $data;
58
            }
59
        };
60
61 5
        $context->election = $this->_link[0] ?? null;
62
63 5
        return $context;
64
    }
65
66 10
    protected function preDeletedTask ($object) : void
67
    {
68 10
        $object->destroyLink($this->_link[0]);
69 10
    }
70
71
/////////// Array Access - Specials improvements ///////////
72
73 101
    public function offsetSet($offset, $value) : void
74
    {
75 101
        if ($value instanceof Vote) :
76 101
            parent::offsetSet($offset,$value);
77 101
            $this->setStateToVote();
78
        else :
79 1
            throw new CondorcetException (0,'Value must be an instanceof Condorcet\\Vote');
80
        endif;
81 101
    }
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 101
    protected function setStateToVote () : void
92
    {
93 101
        foreach ($this->_link as $element) :
94 101
            $element->setStateToVote();
95
        endforeach;
96 101
    }
97
98
/////////// Public specific methods ///////////
99
100 6
    public function getVoteKey (Vote $vote) {
101 6
        ($r = array_search($vote, $this->_Container, true)) !== false
102 2
            OR ($r = array_search($vote, $this->_Cache, true));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
103
104 6
        return $r;
105
    }
106
107
    // Get the votes registered list
108 17
    public function getVotesList (?array $tag = null, bool $with = true) : array
109
    {
110 17
        if (($tag = VoteUtil::tagsConvert($tag)) === null) :
111 16
            return $this->getFullDataSet();
112
        else :
113 5
            $search = [];
114
115 5
            foreach ($this as $key => $value) :
116 5
                $noOne = true;
117 5
                foreach ($tag as $oneTag) :
118 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
119 5
                        if ($with) :
120 5
                            $search[$key] = $value;
121 5
                            break;
122
                        else :
123 5
                            $noOne = false;
124
                        endif;
125
                    endif;
126
                endforeach;
127
128 5
                if (!$with && $noOne) :
129 5
                    $search[$key] = $value;
130
                endif;
131
            endforeach;
132
133 5
            return $search;
134
        endif;
135
    }
136
137 5
    public function getVotesListAsString () : string
138
    {
139 5
        $simpleList = '';
140
141 5
        $weight = [];
142 5
        $nb = [];
143
144 5
        foreach($this->getVotesList() as $oneVote) :
145 5
            $oneVoteString = $oneVote->getSimpleRanking($this->_link[0]);
146
147 5
            if(!array_key_exists($oneVoteString, $weight)) :
148 5
                $weight[$oneVoteString] = 0;
149
            endif;
150 5
            if(!array_key_exists($oneVoteString, $nb)) :
151 5
                $nb[$oneVoteString] = 0;
152
            endif;
153
154 5
            if ($this->_link[0]->isVoteWeightIsAllowed()) :
155 1
                $weight[$oneVoteString] += $oneVote->getWeight();
156
            else :
157 4
                $weight[$oneVoteString]++;
158
            endif;
159
160 5
            $nb[$oneVoteString]++;
161
        endforeach;
162
163 5
        ksort($weight);
164 5
        arsort($weight);
165
166 5
        $isFirst = true;
167 5
        foreach ($weight as $key => $value) :
168 5
            if (!$isFirst) : $simpleList .= "\n"; endif;
169 5
            $simpleList .= $key.' * '.$nb[$key];
170 5
            $isFirst = false;
171
        endforeach;
172
173 5
        return $simpleList;
174
    }
175
176 10
    public function countVotes (?array $tag, bool $with) : int
177
    {
178 10
        if ($tag === null) :
179 7
            return count($this);
180
        else :
181 5
            $count = 0;
182
183 5
            foreach ($this as $key => $value) :
184 5
                $noOne = true;
185 5
                foreach ($tag as $oneTag) :
186 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
187 5
                        if ($with) :
188 5
                            $count++;
189 5
                            break;
190
                        else :
191 5
                            $noOne = false;
192
                        endif;
193
                    endif;
194
                endforeach;
195
196 5
                if (!$with && $noOne) :
197 5
                    $count++;
198
                endif;
199
            endforeach;
200
201 5
            return $count;
202
        endif;
203
    }
204
205
}
206