1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Hal\Component\Token; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Determines the type of a token (operand, operator...) |
14
|
|
|
* |
15
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
16
|
|
|
*/ |
17
|
|
|
class TokenType { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Available operands |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $operands = array( |
24
|
|
|
// IDENTIFIER |
25
|
|
|
T_VARIABLE |
26
|
|
|
,T_VAR |
27
|
|
|
,T_LNUMBER |
28
|
|
|
,T_DNUMBER |
29
|
|
|
,T_ARRAY |
30
|
|
|
,T_CONST |
31
|
|
|
,T_STRING |
32
|
|
|
,T_NUM_STRING |
33
|
|
|
|
34
|
|
|
// TYPENAME |
35
|
|
|
, T_INT_CAST |
36
|
|
|
, T_ARRAY_CAST |
37
|
|
|
, T_BOOL_CAST |
38
|
|
|
, T_DOUBLE_CAST |
39
|
|
|
, T_OBJECT_CAST |
40
|
|
|
, T_STRING_CAST |
41
|
|
|
, T_UNSET_CAST |
42
|
|
|
|
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Available operators |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $operators = array( |
51
|
|
|
// OPERATOR |
52
|
|
|
T_IS_EQUAL |
53
|
|
|
, T_AND_EQUAL |
54
|
|
|
, T_CONCAT_EQUAL |
55
|
|
|
, T_DIV_EQUAL |
56
|
|
|
, T_MINUS_EQUAL |
57
|
|
|
, T_MOD_EQUAL |
58
|
|
|
, T_MUL_EQUAL |
59
|
|
|
, T_OR_EQUAL |
60
|
|
|
, T_PLUS_EQUAL |
61
|
|
|
, T_SL_EQUAL |
62
|
|
|
, T_SR_EQUAL |
63
|
|
|
, T_XOR_EQUAL |
64
|
|
|
, T_IS_GREATER_OR_EQUAL |
65
|
|
|
, T_IS_SMALLER_OR_EQUAL |
66
|
|
|
, T_IS_NOT_EQUAL |
67
|
|
|
, T_IS_IDENTICAL |
68
|
|
|
, T_BOOLEAN_AND |
69
|
|
|
, T_BOOLEAN_AND |
70
|
|
|
, T_INC |
71
|
|
|
, T_OBJECT_OPERATOR |
72
|
|
|
, T_DOUBLE_COLON |
73
|
|
|
, T_PAAMAYIM_NEKUDOTAYIM |
74
|
|
|
|
75
|
|
|
// SCSPEC |
76
|
|
|
, T_STATIC |
77
|
|
|
, T_ABSTRACT |
78
|
|
|
|
79
|
|
|
// TYPE QUAL |
80
|
|
|
, T_FINAL |
81
|
|
|
|
82
|
|
|
// RESERVED |
83
|
|
|
, T_CONST |
84
|
|
|
, T_BREAK |
85
|
|
|
, T_CASE |
86
|
|
|
, T_CONTINUE |
87
|
|
|
, T_DEFAULT |
88
|
|
|
, T_DO |
89
|
|
|
, T_IF |
90
|
|
|
, T_ELSE |
91
|
|
|
, T_ELSEIF |
92
|
|
|
, T_FOR |
93
|
|
|
, T_FOREACH |
94
|
|
|
, T_GOTO |
95
|
|
|
, T_NEW |
96
|
|
|
, T_RETURN |
97
|
|
|
, T_SWITCH |
98
|
|
|
, T_WHILE |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Operators encapsuled int T_STRING |
103
|
|
|
* |
104
|
|
|
* @var array |
105
|
|
|
*/ |
106
|
|
|
private $operatorsStrings = array( |
107
|
|
|
';', '*', '/', '%', '-','+' |
108
|
|
|
, '!', '!=', '%', '%=', '&', '&&', '||', '&=', '(', ')' |
109
|
|
|
/*, '{', '}'*/, '[', ']', '*', '*=', '+', '++', '+=', ',' |
110
|
|
|
, '-', '--', '-=->', '.', '...', '/', '/=', ':', '::' |
111
|
|
|
, '<', '<<', '<<=', '<=', '=', '==', '>', '>=', '>>' |
112
|
|
|
, '>>=', '?', '^^=', '|', '|=', '~', ';', '=&', '“' |
113
|
|
|
, '“', '‘', '‘', '#', '##' |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* To bypass |
118
|
|
|
* |
119
|
|
|
* @var array |
120
|
|
|
*/ |
121
|
|
|
private $byPass = array( |
122
|
|
|
'{', '}' // in PHP, these case are counted with T_IF, '(', ... |
123
|
|
|
, ')' , '(' // we count only the first ( |
124
|
|
|
, ',' |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Constructor |
129
|
|
|
*/ |
130
|
|
|
public function __construct() { |
131
|
|
|
if(version_compare(PHP_VERSION, '5.4.0') >= 0) { |
132
|
|
|
$this->operators = array_merge($this->operators, array( |
133
|
|
|
T_INSTEADOF |
134
|
|
|
, T_TRAIT_C |
135
|
|
|
)); |
136
|
|
|
$this->operands = array_merge($this->operands, array( |
137
|
|
|
T_TRAIT |
138
|
|
|
, T_CALLABLE |
139
|
|
|
)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// performance : key will be equals to it value |
143
|
|
|
$this->operands = array_combine($this->operands, $this->operands); |
144
|
|
|
$this->operators= array_combine($this->operators, $this->operators); |
145
|
|
|
$this->operatorsStrings = array_combine($this->operatorsStrings, $this->operatorsStrings); |
146
|
|
|
$this->byPass= array_combine($this->byPass, $this->byPass); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check if the token is operand |
151
|
|
|
* |
152
|
|
|
* @param Token $token |
153
|
|
|
* @return boolean |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function isOperand(Token $token) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
if(T_STRING == $token->getType()) { |
158
|
|
|
return !(isset($this->byPass[$token->getValue()]) ||isset($this->operatorsStrings[$token->getValue()])); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return isset($this->operands[$token->getType()]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if the token is operator |
166
|
|
|
* |
167
|
|
|
* @param Token $token |
168
|
|
|
* @return boolean |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function isOperator(Token $token) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
if(T_STRING == $token->getType()) { |
173
|
|
|
if(isset($this->byPass[$token->getValue()])) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
return isset($this->operatorsStrings[$token->getValue()]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return isset($this->operators[$token->getType()]); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.