Passed
Push — master ( 88b497...e86a88 )
by Boudry
03:07
created

VoteUtil   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 0
loc 56
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C tagsConvert() 0 23 8
A getRankingAsString() 0 11 3
A convertVoteInput() 0 15 3
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 14 and the first side effect is on line 20.

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