Completed
Pull Request — master (#173)
by Kevin
02:26
created

Executor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 83
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C evaluate() 0 62 18
1
<?php
2
3
namespace Magium\TestCase;
4
5
class Executor
6
{
7
8
    protected $reservedTrue = [
9
        'true'
10
    ];
11
12
    protected $reservedFalse = [
13
        'false', 'null', ''
14
    ];
15
16
    protected $operators = [
17
        T_IS_EQUAL, T_IS_NOT_EQUAL, T_IS_SMALLER_OR_EQUAL, T_IS_GREATER_OR_EQUAL
18
    ];
19
20
    protected $stringOperators = [
21
        '>', '<'
22
    ];
23
24
    public function evaluate($string)
25
    {
26
        $string = '<?php ' . $string;
27
        $comparison1 = $comparison2 = '';
28
        $operator = null;
29
        $tokens = token_get_all($string);
30
        array_shift($tokens);
31
        foreach ($tokens as $token) {
32
            if (is_array($token)) {
33
                if (in_array($token[0], $this->operators)) {
34
                    $operator = $token[1];
35
                } else if ($token[0] == T_STRING || $token[0] == T_WHITESPACE || $token[0] == T_LNUMBER) {
36
                    if ($operator === null) {
37
                        $comparison1 .= $token[1];
38
                    } else {
39
                        $comparison2 .= $token[1];
40
                    }
41
                }
42
            } else if (in_array($token, $this->stringOperators)) {
43
                $operator = $token;
44
            }
45
        }
46
47
        $comparison1 = trim($comparison1);
48
        $comparison2 = trim($comparison2);
49
50
        if ($operator === null) {
51
            $isTrue = in_array($comparison1, $this->reservedTrue);
52
            if ($isTrue) return true;
53
54
            $isFalse = in_array($comparison1, $this->reservedFalse);
55
            if ($isFalse) return false;
56
57
            return (boolean)$comparison1;
58
        } else {
59
            switch ($operator) {
60
                case '==':
61
                    return $comparison1 == $comparison2;
62
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
63
                case '!=':
64
                    return $comparison1 != $comparison2;
65
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
66
                case '>':
67
                    return $comparison1 > $comparison2;
68
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
                case '<':
70
                    return $comparison1 < $comparison2;
71
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
                case '>=':
73
                    return $comparison1 >= $comparison2;
74
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
75
                case '<=':
76
                    return $comparison1 <= $comparison2;
77
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
78
                default:
79
                    return false;
80
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
82
            }
83
        }
84
        return false;
85
    }
86
87
}
88