@@ -16,7 +16,13 @@ class Lexer
16
16
protected $ tokenMap ;
17
17
protected $ dropTokens ;
18
18
19
- protected $ usedAttributes ;
19
+ private $ attributeStartLineUsed ;
20
+ private $ attributeEndLineUsed ;
21
+ private $ attributeStartTokenPosUsed ;
22
+ private $ attributeEndTokenPosUsed ;
23
+ private $ attributeStartFilePosUsed ;
24
+ private $ attributeEndFilePosUsed ;
25
+ private $ attributeCommentsUsed ;
20
26
21
27
/**
22
28
* Creates a Lexer.
@@ -37,12 +43,17 @@ public function __construct(array $options = []) {
37
43
[\T_WHITESPACE , \T_OPEN_TAG , \T_COMMENT , \T_DOC_COMMENT ], 1
38
44
);
39
45
40
- // the usedAttributes member is a map of the used attribute names to a dummy
41
- // value (here "true")
42
- $ options += [
43
- 'usedAttributes ' => ['comments ' , 'startLine ' , 'endLine ' ],
44
- ];
45
- $ this ->usedAttributes = array_fill_keys ($ options ['usedAttributes ' ], true );
46
+ $ defaultAttributes = ['comments ' , 'startLine ' , 'endLine ' ];
47
+ $ usedAttributes = array_fill_keys ($ options ['usedAttributes ' ] ?? $ defaultAttributes , true );
48
+
49
+ // Create individual boolean properties to make these checks faster.
50
+ $ this ->attributeStartLineUsed = isset ($ usedAttributes ['startLine ' ]);
51
+ $ this ->attributeEndLineUsed = isset ($ usedAttributes ['endLine ' ]);
52
+ $ this ->attributeStartTokenPosUsed = isset ($ usedAttributes ['startTokenPos ' ]);
53
+ $ this ->attributeEndTokenPosUsed = isset ($ usedAttributes ['endTokenPos ' ]);
54
+ $ this ->attributeStartFilePosUsed = isset ($ usedAttributes ['startFilePos ' ]);
55
+ $ this ->attributeEndFilePosUsed = isset ($ usedAttributes ['endFilePos ' ]);
56
+ $ this ->attributeCommentsUsed = isset ($ usedAttributes ['comments ' ]);
46
57
}
47
58
48
59
/**
@@ -230,13 +241,13 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
230
241
$ token = "\0" ;
231
242
}
232
243
233
- if (isset ( $ this ->usedAttributes [ ' startLine ' ]) ) {
244
+ if ($ this ->attributeStartLineUsed ) {
234
245
$ startAttributes ['startLine ' ] = $ this ->line ;
235
246
}
236
- if (isset ( $ this ->usedAttributes [ ' startTokenPos ' ]) ) {
247
+ if ($ this ->attributeStartTokenPosUsed ) {
237
248
$ startAttributes ['startTokenPos ' ] = $ this ->pos ;
238
249
}
239
- if (isset ( $ this ->usedAttributes [ ' startFilePos ' ]) ) {
250
+ if ($ this ->attributeStartFilePosUsed ) {
240
251
$ startAttributes ['startFilePos ' ] = $ this ->filePos ;
241
252
}
242
253
@@ -263,7 +274,7 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
263
274
$ this ->filePos += \strlen ($ value );
264
275
} else {
265
276
if (\T_COMMENT === $ token [0 ] || \T_DOC_COMMENT === $ token [0 ]) {
266
- if (isset ( $ this ->usedAttributes [ ' comments ' ]) ) {
277
+ if ($ this ->attributeCommentsUsed ) {
267
278
$ comment = \T_DOC_COMMENT === $ token [0 ]
268
279
? new Comment \Doc ($ token [1 ], $ this ->line , $ this ->filePos , $ this ->pos )
269
280
: new Comment ($ token [1 ], $ this ->line , $ this ->filePos , $ this ->pos );
@@ -276,13 +287,13 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
276
287
continue ;
277
288
}
278
289
279
- if (isset ( $ this ->usedAttributes [ ' endLine ' ]) ) {
290
+ if ($ this ->attributeEndLineUsed ) {
280
291
$ endAttributes ['endLine ' ] = $ this ->line ;
281
292
}
282
- if (isset ( $ this ->usedAttributes [ ' endTokenPos ' ]) ) {
293
+ if ($ this ->attributeEndTokenPosUsed ) {
283
294
$ endAttributes ['endTokenPos ' ] = $ this ->pos ;
284
295
}
285
- if (isset ( $ this ->usedAttributes [ ' endFilePos ' ]) ) {
296
+ if ($ this ->attributeEndFilePosUsed ) {
286
297
$ endAttributes ['endFilePos ' ] = $ this ->filePos - 1 ;
287
298
}
288
299
0 commit comments