Passed
Push — TEST/ScrutinizerPHPAnalysisEng... ( c9e065...573acf )
by Boudry
09:25
created

ResultsProcess::getResult()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0033

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 21
cts 22
cp 0.9545
rs 8.439
c 0
b 0
f 0
cc 6
crap 6.0033
eloc 26
nc 7
nop 2
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\ElectionProcess;
12
13
use Condorcet\Condorcet;
14
use Condorcet\CondorcetException;
15
use Condorcet\Result;
16
use Condorcet\Algo\Pairwise;
17
use Condorcet\Timer\Chrono as Timer_Chrono;
18
19
// Manage Results for Election class
20
trait ResultsProcess
21
{
22
23
/////////// CONSTRUCTOR ///////////
24
25
    // Result
26
    protected $_Pairwise;
27
    protected $_Calculator;
28
29
30
/////////// GET RESULTS ///////////
31
32
    // Generic function for default result with ability to change default object method
33 75
    public function getResult ($method = true, array $options = []) : Result
34
    {
35 75
        $options = self::formatResultOptions($options);
36
37
        // Filter if tag is provided & return
38 75
        if ($options['%tagFilter']) :
39 2
            $chrono = new Timer_Chrono ($this->_timer, 'GetResult with filter');
40
41 2
            $filter = new self;
42
43 2
            foreach ($this->getCandidatesList() as $candidate) :
44 2
                $filter->addCandidate($candidate);
45
            endforeach;
46
47 2
            foreach ($this->getVotesList($options['tags'], $options['withTag']) as $vote) :
48 2
                $filter->addVote($vote);
49
            endforeach;
50
51 2
            unset($chrono);
52
53 2
            return $filter->getResult($method);
54
        endif;
55
56
            ////// Start //////
57
58
        // Prepare
59 75
        $this->prepareResult();
60
61
            //////
62
63 75
        $chrono = new Timer_Chrono ($this->_timer);
64
65 75
        if ($method === true) :
66 7
            $this->initResult(Condorcet::getDefaultMethod());
67 7
            $result = $this->_Calculator[Condorcet::getDefaultMethod()]->getResult();
68 71
        elseif ($method = Condorcet::isAuthMethod((string) $method)) :
69 71
            $this->initResult($method);
70 69
            $result = $this->_Calculator[$method]->getResult();
71
        else :
72
            throw new CondorcetException(8,$method);
73
        endif;
74
75 72
        $chrono->setRole('GetResult for '.$method);
76
77 72
        return $result;
78
    }
79
80
81 78
    public function getWinner (?string $substitution = null)
82
    {
83 78
        $algo = Condorcet::condorcetBasicSubstitution($substitution);
84
85
            //////
86
87 78
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
88 76
            new Timer_Chrono ($this->_timer, 'GetWinner for CondorcetBasic');
89 76
            $this->initResult($algo);
90 76
            $result = $this->_Calculator[$algo]->getWinner();
91
92 76
            return ($result === null) ? null : $this->getCandidateId($result);
93
        else :
94 50
            return $this->getResult($algo)->getWinner();
95
        endif;
96
    }
97
98
99 75
    public function getLoser (?string $substitution = null)
100
    {
101 75
        $algo = Condorcet::condorcetBasicSubstitution($substitution);
102
103
            //////
104
105 75
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
106 75
            new Timer_Chrono ($this->_timer, 'GetLoser for CondorcetBasic');
107 75
            $this->initResult($algo);
108 75
            $result = $this->_Calculator[$algo]->getLoser();
109
110 75
            return ($result === null) ? null : $this->getCandidateId($result);
111
        else :
112 2
            return $this->getResult($algo)->getLoser();
113
        endif;
114
    }
115
116 77
    public function getPairwise (bool $explicit = true)
117
    {
118 77
        $this->prepareResult();
119
120 77
        return (!$explicit) ? $this->_Pairwise : $this->_Pairwise->getExplicitPairwise();
121
    }
122
123
124
/////////// MAKE RESULTS ///////////
125
126 1
    public function computeResult ($method = true) : void
127
    {
128 1
        $this->getResult($method);
129 1
    }
130
131 80
    protected function makePairwise () : void
132
    {
133 80
        $this->_Pairwise = new Pairwise ($this);
134 80
    }
135
136 80
    protected function initResult (string $class) : void
137
    {
138 80
        if ( !isset($this->_Calculator[$class]) ) :
139 80
            $this->_Calculator[$class] = new $class($this);
140
        endif;
141 78
    }
142
143
    // Cleanup results to compute again with new votes
144 80
    protected function cleanupResult () : void
145
    {
146
        // Reset state
147 80
        if ($this->_State > 2) : 
148 8
            $this->_State = 2;
149
        endif;
150
151
            //////
152
153
        // Clean pairwise
154 80
        $this->_Pairwise = null;
155
156
        // Algos
157 80
        $this->_Calculator = null;
158 80
    }
159
160
161
/////////// UTILS ///////////
162
163 75
    protected static function formatResultOptions (array $arg) : array
164
    {
165
        // About tag filter
166 75
        if (isset($arg['tags'])):
167 2
            $arg['%tagFilter'] = true;
168
169 2
            if ( !isset($arg['withTag']) || !is_bool($arg['withTag']) ) :
170 2
                $arg['withTag'] = true;
171
            endif;
172
        else:
173 75
            $arg['%tagFilter'] = false;
174
        endif;
175
176 75
        return $arg;
177
    }
178
179
}
180