Completed
Push — master ( 9ee175...c01904 )
by Boudry
02:34
created

Condorcet::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
namespace Condorcet;
11
12
use Condorcet\CondorcetException;
13
use Condorcet\Result;
14
15
// Registering native Condorcet Methods implementation
16 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\Copeland');
17 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\DodgsonQuick');
18 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\DodgsonTidemanApproximation');
19 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\KemenyYoung');
20 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxWinning');
21 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxMargin');
22 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxOpposition');
23 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\RankedPairsMargin');
24 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\RankedPairsWinning');
25 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeWinning');
26 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeMargin');
27 10
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeRatio');
28
29
// Set the default Condorcet Class algorithm
30 10
Condorcet::setDefaultMethod('Schulze');
31
32
abstract class Condorcet
33
{
34
35
/////////// CONSTANTS ///////////
36
    public const VERSION = '1.5.0';
37
38
    public const CONDORCET_BASIC_CLASS = __NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic';
39
40
    protected static $_defaultMethod = null;
41
    protected static $_authMethods = [ self::CONDORCET_BASIC_CLASS => (__NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic')::METHOD_NAME ];
42
43
44
/////////// STATICS METHODS ///////////
45
46
    // Return library version numer
47 6
    public static function getVersion (string $option = 'FULL') : string
48
    {
49 6
        if ($option === 'MAJOR') :
50 3
            $version = explode('.', self::VERSION);
51 3
            return $version[0].'.'.$version[1];
52
        else :
53 5
            return self::VERSION;
54
        endif;
55
    }
56
57
    // Return an array with auth methods
58 1
    public static function getAuthMethods (bool $basic = false) : array
59
    {
60 1
        $auth = self::$_authMethods;
61
62
        // Don't show Natural Condorcet
63 1
        if (!$basic) :
64 1
            unset($auth[self::CONDORCET_BASIC_CLASS]);
65
        endif;
66
67 1
        return array_column($auth,0);
68
    }
69
70
71
    // Return the Class default method
72 14
    public static function getDefaultMethod () {
73 14
        return self::$_defaultMethod;
74
    }
75
76
77
    // Check if the method is supported
78 80
    public static function isAuthMethod (string $method)
79
    {
80 80
        $auth = self::$_authMethods;
81
82 80
        if (empty($method)) :
83 1
            throw new CondorcetException (8);
84
        endif;
85
86 79
        if ( isset($auth[$method]) ) :
87 12
            return $method;
88
        else : // Alias
89 77
            foreach ($auth as $class => &$alias) :
90 77
                foreach ($alias as &$entry) :
91 77
                    if ( strcasecmp($method,$entry) === 0 ) :
92 77
                        return $class;
93
                    endif;
94
                endforeach;
95
            endforeach;
96
        endif;
97
98 13
        return false;
99
    }
100
101
102
    // Add algos
103 12
    public static function addMethod (string $algos) : bool
104
    {
105
        // Check algos
106 12
        if ( self::isAuthMethod($algos) || !self::testMethod($algos) ) :
107 1
            return false;
108
        endif;
109
110
        // Adding algo
111 10
        self::$_authMethods[$algos] = $algos::METHOD_NAME;
112
113 10
        if (self::getDefaultMethod() === null) :
114 10
            self::setDefaultMethod($algos);
115
        endif;
116
117 10
        return true;
118
    }
119
120
121
        // Check if the class Algo. exist and ready to be used
122 11
        protected static function testMethod (string $method) : bool
123
        {
124 11
            if ( !class_exists($method) ) :             
125 1
                throw new CondorcetException(9);
126
            endif;
127
128 10
            if ( !is_subclass_of($method, __NAMESPACE__.'\\Algo\\MethodInterface') || !is_subclass_of($method,__NAMESPACE__.'\\Algo\\Method') ) :
129 1
                throw new CondorcetException(10);
130
            endif;
131
132 10
            foreach ($method::METHOD_NAME as $alias) :
133 10
                if (self::isAuthMethod($alias)) :
134 10
                    throw new CondorcetException(25);
135
                endif;
136
            endforeach;
137
138 10
            return true;
139
        }
140
141
142
    // Change default method for this class.
143 11
    public static function setDefaultMethod (string $method) : bool
144
    {       
145 11
        if ( ($method = self::isAuthMethod($method)) && $method !== self::CONDORCET_BASIC_CLASS ) :
146 10
            self::$_defaultMethod = $method;
147 10
            return true;
148
        else :
149 1
            return false;
150
        endif;
151
    }
152
}
153