Skip to content

Commit 10e56b8

Browse files
committed
WIP added a 2nd pass through all the token which precalculates linenumbers
Required for later token-lookaheads with linenumbers
1 parent ff93f37 commit 10e56b8

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ private function tclass($token) {
6464
}
6565
}
6666

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+
6774
/**
6875
* @return string
6976
*/
@@ -129,11 +136,29 @@ private function getInterfaces(array $tokens, $i) {
129136
}
130137

131138
public function tokenize() {
132-
$line = 1;
133139
$sourceCode = file_get_contents($this->filename);
134140
$tokens = token_get_all($sourceCode);
135141
$numTokens = count($tokens);
136142

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;
137162
for ($i = 0; $i < $numTokens; ++$i) {
138163
$token = $tokens[$i];
139164

@@ -147,7 +172,6 @@ public function tokenize() {
147172
$tokenClass = self::$customTokens[$token];
148173
}
149174

150-
$token = new $tokenClass($text, $line, $this, $i);
151175
switch ($tokenClass) {
152176
case 'PHP_Token_HALT_COMPILER':
153177
break;
@@ -160,7 +184,7 @@ public function tokenize() {
160184
'interfaces'=> $this->getInterfaces($tokens, $i),
161185
'keywords' => $this->getKeywords($tokens, $i),
162186
'docblock' => $token->getDocblock(),
163-
'startLine' => $line,
187+
'startLine' => $this->tline($token),
164188
'endLine' => $token->getEndLine(),
165189
'package' => $token->getPackage(),
166190
'file' => $this->filename
@@ -184,33 +208,31 @@ public function tokenize() {
184208
'keywords' => $this->getKeywords($tokens, $i),
185209
'visibility'=> $token->getVisibility(),
186210
'signature' => $token->getSignature(),
187-
'startLine' => $line,
211+
'startLine' => $this->tline($token),
188212
'endLine' => $token->getEndLine(),
189213
'ccn' => $token->getCCN(),
190214
'file' => $this->filename
191215
);
192216

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;
205227

206228
case 'PHP_Token_CLOSE_CURLY':
207-
if ($classEndLine !== false && $classEndLine == $line) {
229+
if ($classEndLine !== false && $classEndLine == $this->tline($token)) {
208230
$class = false;
209231
$classEndLine = false;
210-
} elseif ($traitEndLine !== false && $traitEndLine == $line) {
232+
} elseif ($traitEndLine !== false && $traitEndLine == $this->tline($token)) {
211233
$trait = false;
212234
$traitEndLine = false;
213-
} elseif ($interfaceEndLine !== false && $interfaceEndLine == $line) {
235+
} elseif ($interfaceEndLine !== false && $interfaceEndLine == $this->tline($token)) {
214236
$interface = false;
215237
$interfaceEndLine = false;
216238
}

0 commit comments

Comments
 (0)