Passed
Branch dev-2.0 (faa14c)
by Boudry
02:42
created

CondorcetBasic   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 3 1
A getStats() 0 1 1
B getWinner() 0 28 6
B getLoser() 0 28 6
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\Algo\Methods;
12
13
use CondorcetPHP\Condorcet\Algo\Method;
14
use CondorcetPHP\Condorcet\Algo\MethodInterface;
15
use CondorcetPHP\Condorcet\CondorcetException;
16
use CondorcetPHP\Condorcet\Result;
17
18
// Condorcet Basic Class, provide natural Condorcet winner or looser
19
class CondorcetBasic extends Method implements MethodInterface
20
{
21
    // Method Name
22
    public const METHOD_NAME = ['CondorcetBasic'];
23
24
    // Basic Condorcet
25
    protected $_CondorcetWinner;
26
    protected $_CondorcetLoser;
27
28
29
/////////// PUBLIC ///////////
30
31
32 1
    public function getResult () : Result {
33 1
        throw new CondorcetException (102);
34
    }
35
36
37
    protected function getStats () : array { return []; }
38
39
40
    // Get a Condorcet certified winner. If there is none = null. You can force a winner choice with alternative supported methods ($substitution)
41 90
    public function getWinner () : ?int
42
    {
43
        // Cache
44 90
        if ( $this->_CondorcetWinner !== null ) :
45 11
            return $this->_CondorcetWinner;
46
        endif;
47
48
            //////
49
50
        // Basic Condorcet calculation
51 90
        foreach ( $this->_selfElection->getPairwise() as $candidate_key => $candidat_detail ) :
52 90
            $winner = true;
53
54 90
            foreach ($candidat_detail['win'] as $challenger_key => $win_count ) :
55 90
                if  ( $win_count <= $candidat_detail['lose'][$challenger_key] ) :
56 79
                    $winner = false;
57 90
                    break;
58
                endif;
59
            endforeach;
60
61 90
            if ($winner) :
62 41
                $this->_CondorcetWinner = $candidate_key;
63 90
                return $this->_CondorcetWinner;
64
            endif;
65
        endforeach;
66
67 53
            return null;
68
    }
69
70
    // Get a Condorcet certified loser. If there is none = null. You can force a winner choice with alternative supported methods ($substitution)
71 89
    public function getLoser () : ?int
72
    {
73
        // Cache
74 89
        if ( $this->_CondorcetLoser !== null ) :
75 10
            return $this->_CondorcetLoser;
76
        endif;
77
78
            //////
79
80
        // Basic Condorcet calculation
81 89
        foreach ( $this->_selfElection->getPairwise() as $candidate_key => $candidat_detail ) :
82 89
            $loser = true;
83
84 89
            foreach ( $candidat_detail['lose'] as $challenger_key => $lose_count ) :
85 89
                if  ( $lose_count <= $candidat_detail['win'][$challenger_key] ) :
86 75
                    $loser = false;
87 89
                    break;
88
                endif;
89
            endforeach;
90
91 89
            if ($loser) :
92 48
                $this->_CondorcetLoser = $candidate_key;
93 89
                return $this->_CondorcetLoser;
94
            endif;
95
        endforeach;
96
97 42
        return null;
98
    }
99
}
100