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

CondorcetUtil   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 25
dl 0
loc 84
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C format() 0 31 12
B isJson() 0 11 6
B prepareParse() 0 24 5
A prepareJson() 0 7 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;
12
13
use Condorcet\CondorcetException;
14
use Condorcet\Candidate;
15
use Condorcet\Election;
16
use Condorcet\Result;
17
use Condorcet\Vote;
18
19
abstract class CondorcetUtil
20
{
21
    // Check JSON format
22 3
    public static function isJson (string $string) : bool
23
    {
24 3
        if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) :
25 1
            return false;
26
        endif;
27
28
        // try to decode string
29 2
        json_decode($string);
30
31
        // check if error occured
32 2
        return json_last_error() === JSON_ERROR_NONE;
33
    }
34
35 3
    public static function prepareJson (string $input)
36
    {
37 3
        if (!self::isJson($input)) :
38 1
            throw new CondorcetException(15);
39
        endif;
40
41 2
        return json_decode($input, true);
42
    }
43
44
    // Generic action before parsing data from string input
45 79
    public static function prepareParse (string $input, bool $allowFile) : array
46
    {
47
        // Is string or is file ?
48 79
        if ($allowFile === true && is_file($input)) :
49
            $input = file_get_contents($input);
50
        endif;
51
52
        // Line
53 79
        $input = preg_replace("(\r\n|\n|\r)",';',$input);
54 79
        $input = explode(';', $input);
55
56
        // Delete comments
57 79
        foreach ($input as &$line) :
58
            // Delete comments
59 79
            $is_comment = mb_strpos($line, '#');
60 79
            if ($is_comment !== false) :
61 2
                $line = substr($line, 0, $is_comment);
62
            endif;
63
64
            // Trim
65 79
            $line = trim($line);
66
        endforeach;
67
68 79
        return $input;
69
    }
70
71
    // Simplify Condorcet Var_Dump. Transform object to String.
72 53
    public static function format ($input, bool $convertObject = true)
73
    {
74 53
        if (is_object($input)) :
75
            
76 53
            $r = $input;
77
78 53
            if ($convertObject) :
79 3
                if ($input instanceof Candidate) :
80 1
                    $r = (string) $input;
81 3
                elseif ($input instanceof Vote) :
82
                    $r = $input->getRanking();
83 3
                elseif ($input instanceof Result) :
84 53
                    $r = $input->getResultAsArray(true);
85
                endif;
86
            endif;
87
88 52
        elseif (!is_array($input)) :
89 2
            $r = $input;
90
        else :
91 52
            foreach ($input as $key => $line) :
92 52
                $input[$key] = self::format($line,$convertObject);
93
            endforeach;
94
95 52
            if (count($input) === 1 && is_int(key($input)) && (!is_array(reset($input)) || count(reset($input)) === 1)):
96 45
                $r = reset($input);
97
            else:
98 7
                $r = $input;
99
            endif;
100
        endif;
101
        
102 53
        return $r;
103
    }
104
}
105