Passed
Branch master (73ca69)
by Boudry
03:33
created

CondorcetException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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
namespace Condorcet;
11
12
use Condorcet\Condorcet;
13
use Condorcet\CondorcetVersion;
14
15
// Custom Exeption
16
class CondorcetException extends \Exception
17
{
18
    use CondorcetVersion;
19
20
    protected $_infos;
21
22
    public function __construct (int $code = 0, $infos = '')
23
    {
24
        $this->_infos = $infos;
25
26
        parent::__construct($this->correspondence($code), $code);
27
    }
28
29
    public function __toString () : string
30
    {
31
           return __CLASS__ . ": [{$this->code}]: {$this->message} (line: {$this->file}:{$this->line})\n";
32
    }
33
34
    protected function correspondence (int $code) : string
35
    {
36
        // Common
37
        $error[1] = 'Bad candidate format';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$error was never initialized. Although not strictly required by PHP, it is generally a good practice to add $error = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
        $error[2] = 'The voting process has already started';
39
        $error[3] = 'This candidate ID is already registered';
40
        $error[4] = 'This candidate ID do not exist';
41
        $error[5] = 'Bad vote format | '.$this->_infos;
42
        $error[6] = 'You need to specify votes before results';
43
        $error[7] = 'Your Candidate ID is too long > ' . Election::MAX_LENGTH_CANDIDATE_ID;
44
        $error[8] = 'This method do not exist';
45
        $error[9] = 'The algo class you want has not been defined';
46
        $error[10] = 'The algo class you want is not correct';
47
        $error[11] = 'You try to unserialize an object version older than your actual Class version. This is a problematic thing';
48
        $error[12] = 'You have exceeded the number of votes allowed for this method.';
49
        $error[13] = 'Formatting error: You must specify an integer';
50
        $error[14] = 'parseVote() must take a string (raw or path) as argument';
51
        $error[15] = 'Input must be valid Json format';
52
        $error[16] = 'You have exceeded the maximum number of votes allowed per election ('.$this->_infos.').';
53
        $error[17] = 'Bad tags input format';
54
        $error[18] = 'New vote can\'t match Candidate of his elections';
55
        $error[19] = 'This name is not allowed in because of a namesake in the election in which the object participates.';
56
        $error[20] = 'You need to specify one or more candidates before voting';
57
        $error[21] = 'Bad vote timestamp format';
58
        $error[22] = 'This context is not valid';
59
        $error[23] = 'No Data Handler in use';
60
        $error[24] = 'A Data Handler is already in use';
61
        $error[25] = 'Algo class try to use existing alias';
62
        $error[26] = 'Weight can not be < 1';
63
64
        $error[30] = 'Candidate not in Ranking';
65
66
67
        // DataManager
68
        $error[30] = 'This entity does not exist.';
69
70
        // Algorithms
71
        if ($code === 101) :
72
            $error[101] = $this->_infos;
73
        endif;
74
        $error[102] = 'Marquis of Condorcet algortihm can\'t provide a full ranking. But only Winner and Loser.';
75
76
77
        if ( array_key_exists($code, $error) ) :
78
            return $error[$code];
79
        else :
80
            return (!is_null($this->_infos)) ? $this->_infos : 'Mysterious Error';
81
        endif;
82
    }
83
}
84