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

VoteUtil::getRankingAsString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
crap 3
eloc 7
nc 3
nop 1
1
<?php
1 ignored issue
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 15 and the first side effect is on line 21.

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 - 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\ElectionProcess;
12
13
14
// Base Condorcet class
15
abstract class VoteUtil
16
{
17 111
    public static function tagsConvert ($tags) : ?array
18
    {
19 111
        if (empty($tags)) :
20 111
            return null;
21
        endif;
22
23
        // Make Array
24 13
        if (!is_array($tags)) :
25 13
            $tags = explode(',', $tags);
26
        endif;
27
28
        // Trim tags
29 13
        foreach ($tags as $key => &$oneTag) :
30 13
            if (empty($oneTag) || is_object($oneTag) || is_bool($oneTag)) :
31 1
                unset($tags[$key]);
32 1
                continue;
33
            endif;
34
35 13
            $oneTag = (!ctype_digit($oneTag)) ? trim($oneTag) : intval($oneTag);
36
        endforeach;
37
38 13
        return $tags;
39
    }
40
41 111
    public static function getRankingAsString (array $ranking) : string
42
    {
43 111
        foreach ($ranking as &$rank) :
44 108
            if (is_array($rank)) :
45 108
                sort($rank);
46 108
                $rank = implode(' = ',$rank);
47
            endif;
48
        endforeach;
49
50 111
        return implode(' > ', $ranking);
51
    }
52
53
    // From a string like 'A>B=C=H>G=T>Q'
54 95
    public static function convertVoteInput (string $formula) : array
55
    {
56 95
        $ranking = explode('>', $formula);
57
58 95
        foreach ($ranking as &$rank_vote) :
59 95
            $rank_vote = explode('=', $rank_vote);
60
61
            // Del space at start and end
62 95
            foreach ($rank_vote as &$value) :
63 95
                $value = trim($value);
64
            endforeach;
65
        endforeach;
66
67 95
        return $ranking;
68
    }
69
70
}
71