@@ -64,6 +64,13 @@ private function tclass($token) {
64
64
}
65
65
}
66
66
67
+ private function tline ($ token ) {
68
+ if (isset ($ token [3 ])) {
69
+ return $ token [3 ];
70
+ }
71
+ throw new Exception ('Scalar tokens are not yet precalculated ' );
72
+ }
73
+
67
74
/**
68
75
* @return string
69
76
*/
@@ -129,11 +136,29 @@ private function getInterfaces(array $tokens, $i) {
129
136
}
130
137
131
138
public function tokenize () {
132
- $ line = 1 ;
133
139
$ sourceCode = file_get_contents ($ this ->filename );
134
140
$ tokens = token_get_all ($ sourceCode );
135
141
$ numTokens = count ($ tokens );
136
142
143
+ // precalculate in which line the tokens reside, for later lookaheads
144
+ $ line = 1 ;
145
+ for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
146
+ $ token =& $ tokens [$ i ];
147
+
148
+ if (is_array ($ token )) {
149
+ $ name = substr (token_name ($ token [0 ]), 2 );
150
+ $ text = $ token [1 ];
151
+
152
+ $ token [2 ] = $ line ;
153
+ } else {
154
+ $ text = $ token ;
155
+ }
156
+
157
+ $ lines = substr_count ($ text , "\n" );
158
+ $ line += $ lines ;
159
+ }
160
+
161
+ $ line = 1 ;
137
162
for ($ i = 0 ; $ i < $ numTokens ; ++$ i ) {
138
163
$ token = $ tokens [$ i ];
139
164
@@ -147,7 +172,6 @@ public function tokenize() {
147
172
$ tokenClass = self ::$ customTokens [$ token ];
148
173
}
149
174
150
- $ token = new $ tokenClass ($ text , $ line , $ this , $ i );
151
175
switch ($ tokenClass ) {
152
176
case 'PHP_Token_HALT_COMPILER ' :
153
177
break ;
@@ -160,7 +184,7 @@ public function tokenize() {
160
184
'interfaces ' => $ this ->getInterfaces ($ tokens , $ i ),
161
185
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
162
186
'docblock ' => $ token ->getDocblock (),
163
- 'startLine ' => $ line ,
187
+ 'startLine ' => $ this -> tline ( $ token ) ,
164
188
'endLine ' => $ token ->getEndLine (),
165
189
'package ' => $ token ->getPackage (),
166
190
'file ' => $ this ->filename
@@ -184,33 +208,31 @@ public function tokenize() {
184
208
'keywords ' => $ this ->getKeywords ($ tokens , $ i ),
185
209
'visibility ' => $ token ->getVisibility (),
186
210
'signature ' => $ token ->getSignature (),
187
- 'startLine ' => $ line ,
211
+ 'startLine ' => $ this -> tline ( $ token ) ,
188
212
'endLine ' => $ token ->getEndLine (),
189
213
'ccn ' => $ token ->getCCN (),
190
214
'file ' => $ this ->filename
191
215
);
192
216
193
- if ($ class === false &&
194
- $ trait === false &&
195
- $ interface === false ) {
196
- $ this ->functions [$ name ] = $ tmp ;
197
- } elseif ($ class !== false ) {
198
- $ this ->classes [$ class ]['methods ' ][$ name ] = $ tmp ;
199
- } elseif ($ trait !== false ) {
200
- $ this ->traits [$ trait ]['methods ' ][$ name ] = $ tmp ;
201
- } else {
202
- $ this ->interfaces [$ interface ]['methods ' ][$ name ] = $ tmp ;
203
- }
204
- break ;
217
+ if ($ class === false && $ trait === false && $ interface === false ) {
218
+ $ this ->functions [$ name ] = $ tmp ;
219
+ } elseif ($ class !== false ) {
220
+ $ this ->classes [$ class ]['methods ' ][$ name ] = $ tmp ;
221
+ } elseif ($ trait !== false ) {
222
+ $ this ->traits [$ trait ]['methods ' ][$ name ] = $ tmp ;
223
+ } else {
224
+ $ this ->interfaces [$ interface ]['methods ' ][$ name ] = $ tmp ;
225
+ }
226
+ break ;
205
227
206
228
case 'PHP_Token_CLOSE_CURLY ' :
207
- if ($ classEndLine !== false && $ classEndLine == $ line ) {
229
+ if ($ classEndLine !== false && $ classEndLine == $ this -> tline ( $ token ) ) {
208
230
$ class = false ;
209
231
$ classEndLine = false ;
210
- } elseif ($ traitEndLine !== false && $ traitEndLine == $ line ) {
232
+ } elseif ($ traitEndLine !== false && $ traitEndLine == $ this -> tline ( $ token ) ) {
211
233
$ trait = false ;
212
234
$ traitEndLine = false ;
213
- } elseif ($ interfaceEndLine !== false && $ interfaceEndLine == $ line ) {
235
+ } elseif ($ interfaceEndLine !== false && $ interfaceEndLine == $ this -> tline ( $ token ) ) {
214
236
$ interface = false ;
215
237
$ interfaceEndLine = false ;
216
238
}
0 commit comments