ResultsProcess::getWinner()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 3
rs 9.7333
c 0
b 0
f 0
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 CondorcetPHP\Condorcet\ElectionProcess;
12
13
use CondorcetPHP\Condorcet\{Condorcet, Result};
14
use CondorcetPHP\Condorcet\Algo\Pairwise;
15
use CondorcetPHP\Condorcet\Throwable\CondorcetException;
16
use CondorcetPHP\Condorcet\Timer\Chrono as Timer_Chrono;
17
18
// Manage Results for Election class
19
trait ResultsProcess
20
{
21
22
/////////// CONSTRUCTOR ///////////
23
24
    // Result
25
    protected $_Pairwise;
26
    protected $_Calculator;
27
28
29
/////////// GET RESULTS ///////////
30
31
    // Generic function for default result with ability to change default object method
32 91
    public function getResult (?string $method = null, array $options = []) : Result
33
    {
34 91
        $options = self::formatResultOptions($options);
35
36
        // Filter if tag is provided & return
37 91
        if ($options['%tagFilter']) :
38 2
            $chrono = new Timer_Chrono ($this->_timer, 'GetResult with filter');
39
40 2
            $filter = new self;
41
42 2
            foreach ($this->getCandidatesList() as $candidate) :
43 2
                $filter->addCandidate($candidate);
44
            endforeach;
45
46 2
            foreach ($this->getVotesList($options['tags'], $options['withTag']) as $vote) :
47 2
                $filter->addVote($vote);
48
            endforeach;
49
50 2
            unset($chrono);
51
52 2
            return $filter->getResult($method);
53
        endif;
54
55
            ////// Start //////
56
57
        // Prepare
58 91
        $this->prepareResult();
59
60
            //////
61
62 90
        $chrono = new Timer_Chrono ($this->_timer);
63
64 90
        if ($method === null) :
65 8
            $this->initResult(Condorcet::getDefaultMethod());
66 8
            $result = $this->_Calculator[Condorcet::getDefaultMethod()]->getResult();
67 85
        elseif ($method = Condorcet::isAuthMethod((string) $method)) :
68 85
            $this->initResult($method);
69 83
            $result = $this->_Calculator[$method]->getResult();
70
        else :
71
            throw new CondorcetException(8,$method);
72
        endif;
73
74 87
        $chrono->setRole('GetResult for '.$method);
75
76 87
        return $result;
77
    }
78
79
80 93
    public function getWinner (?string $method = null)
81
    {
82 93
        $algo = Condorcet::condorcetBasicSubstitution($method);
83
84
            //////
85
86 93
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
87 91
            new Timer_Chrono ($this->_timer, 'GetWinner for CondorcetBasic');
88 91
            $this->initResult($algo);
89 91
            $result = $this->_Calculator[$algo]->getWinner();
90
91 91
            return ($result === null) ? null : $this->getCandidateObjectFromKey($result);
0 ignored issues
show
Bug introduced by
It seems like getCandidateObjectFromKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
92
        else :
93 51
            return $this->getResult($algo)->getWinner();
94
        endif;
95
    }
96
97
98 90
    public function getLoser (?string $method = null)
99
    {
100 90
        $algo = Condorcet::condorcetBasicSubstitution($method);
101
102
            //////
103
104 90
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
105 90
            new Timer_Chrono ($this->_timer, 'GetLoser for CondorcetBasic');
106 90
            $this->initResult($algo);
107 90
            $result = $this->_Calculator[$algo]->getLoser();
108
109 90
            return ($result === null) ? null : $this->getCandidateObjectFromKey($result);
0 ignored issues
show
Bug introduced by
It seems like getCandidateObjectFromKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
110
        else :
111 2
            return $this->getResult($algo)->getLoser();
112
        endif;
113
    }
114
115 92
    public function getPairwise () : Pairwise
116
    {
117 92
        $this->prepareResult();
0 ignored issues
show
Bug introduced by
It seems like prepareResult() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
118 92
        return $this->_Pairwise;
119
    }
120
121 1
    public function getExplicitPairwise () : array
122
    {
123 1
        return $this->getPairwise()->getExplicitPairwise();
124
    }
125
126
127
128
/////////// MAKE RESULTS ///////////
129
130 1
    public function computeResult (?string $method = null) : void
131
    {
132 1
        $this->getResult($method);
133 1
    }
134
135 95
    protected function makePairwise () : void
136
    {
137 95
        $this->_Pairwise = new Pairwise ($this);
138 95
    }
139
140 95
    protected function initResult (string $class) : void
141
    {
142 95
        if ( !isset($this->_Calculator[$class]) ) :
143 95
            $this->_Calculator[$class] = new $class($this);
144
        endif;
145 93
    }
146
147
    // Cleanup results to compute again with new votes
148 96
    protected function cleanupCompute () : void
149
    {
150
        // Clean pairwise
151 96
        $this->cleanupPairwise();
152
153
        // Algos
154 96
        $this->cleanupCalculator();
155 96
    }
156
157 96
    public function cleanupPairwise () : void
158
    {
159
        // Reset state
160 96
        if ($this->_State > 2) : 
161 5
            $this->_State = 2;
162
        endif;
163
164 96
        $this->_Pairwise = null;
165 96
        $this->cleanupCalculator();
166 96
    }
167
168 96
    public function cleanupCalculator () : void
169
    {
170 96
        $this->_Calculator = null;
171 96
    }
172
173
174
/////////// UTILS ///////////
175
176 91
    protected static function formatResultOptions (array $arg) : array
177
    {
178
        // About tag filter
179 91
        if (isset($arg['tags'])):
180 2
            $arg['%tagFilter'] = true;
181
182 2
            if ( !isset($arg['withTag']) || !is_bool($arg['withTag']) ) :
183 2
                $arg['withTag'] = true;
184
            endif;
185
        else:
186 91
            $arg['%tagFilter'] = false;
187
        endif;
188
189 91
        return $arg;
190
    }
191
}
192