Executor::evaluate()   C
last analyzed

Complexity

Conditions 18
Paths 70

Size

Total Lines 62
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 62
rs 6.0989
cc 18
eloc 49
nc 70
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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