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; |
|
|
|
|
63
|
|
|
case '!=': |
64
|
|
|
return $comparison1 != $comparison2; |
65
|
|
|
break; |
|
|
|
|
66
|
|
|
case '>': |
67
|
|
|
return $comparison1 > $comparison2; |
68
|
|
|
break; |
|
|
|
|
69
|
|
|
case '<': |
70
|
|
|
return $comparison1 < $comparison2; |
71
|
|
|
break; |
|
|
|
|
72
|
|
|
case '>=': |
73
|
|
|
return $comparison1 >= $comparison2; |
74
|
|
|
break; |
|
|
|
|
75
|
|
|
case '<=': |
76
|
|
|
return $comparison1 <= $comparison2; |
77
|
|
|
break; |
|
|
|
|
78
|
|
|
default: |
79
|
|
|
return false; |
80
|
|
|
break; |
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
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.