Skip to content

Commit 4312b85

Browse files
committed
WIP refactored name class parsing into tname() method
1 parent 0515fb8 commit 4312b85

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

src/CodeCoverage/Util/Tokenizer.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,70 @@ private function tline($token) {
7171
throw new Exception('Scalar tokens are not yet precalculated');
7272
}
7373

74+
private function tname(array $tokens, $idx) {
75+
$tconst = $this->tconst($tokens[$idx]);
76+
77+
if ($tconst === T_REQUIRE || $tconst === T_REQUIRE_ONCE || $tconst === T_INCLUDE || $tconst === T_INCLUDE_ONCE) {
78+
if ($this->tconst(tokens[$idx+2]) === T_CONSTANT_ENCAPSED_STRING) {
79+
return trim($tokens[$idx+2], "'\"");
80+
}
81+
return null;
82+
}
83+
84+
if ($tconst === T_FUNCTION) {
85+
for ($i = $idx + 1; $i < count($tokens); $i++) {
86+
$token = $tokens[$i];
87+
$tconst = $this->tconst($token);
88+
$tclass = $this->tclass($token);
89+
90+
if ($tconst === T_STRING) {
91+
$name = (string)$token;
92+
break;
93+
} elseif ($tconst === T_STRING && $tclass === 'PHP_Token_AMPERSAND' && $this->tconst($tokens[$i+1]) === T_STRING) {
94+
$name = (string)$tokens[$i+1];
95+
break;
96+
} elseif ($tclass === 'PHP_Token_OPEN_BRACKET') {
97+
$name = 'anonymous function';
98+
break;
99+
}
100+
}
101+
102+
if ($name != 'anonymous function') {
103+
for ($i = $idx; $i; --$i) {
104+
$tconst = $this->tconst($tokens[$i]);
105+
if ($tconst === T_NAMESPACE) {
106+
$name = $this->tname($tokens, $i) . '\\' . $name;
107+
break;
108+
}
109+
110+
if ($tconst === T_INTERFACE) {
111+
break;
112+
}
113+
}
114+
}
115+
116+
return $name;
117+
}
118+
119+
if ($tconst === T_CLASS || $tconst === T_TRAIT) {
120+
return (string) $tokens[$idx + 2];
121+
}
122+
123+
if ($tconst === T_NAMESPACE) {
124+
$namespace = (string)$tokens[$idx+2];
125+
126+
for ($i = $idx + 3;; $i += 2) {
127+
if (isset($tokens[$i]) && $this->tconst($tokens[$i]) === T_NS_SEPARATOR) {
128+
$namespace .= '\\' . $tokens[$i+1];
129+
} else {
130+
break;
131+
}
132+
}
133+
134+
return $namespace;
135+
}
136+
}
137+
74138
/**
75139
* @return string
76140
*/
@@ -216,18 +280,18 @@ public function tokenize() {
216280
);
217281

218282
if ($this->tconst($token) === T_CLASS) {
219-
$class = (string)$tokens[$i + 2];
283+
$class = $this->tname($tokens, $i + 2);
220284
$classEndLine = $token->getEndLine();
221285
$this->classes[$class] = $tmp;
222286
} else {
223-
$trait = (string)$tokens[$i + 2];
287+
$trait = $this->tname($tokens, $i + 2);
224288
$traitEndLine = $token->getEndLine();
225289
$this->traits[$trait] = $tmp;
226290
}
227291
break;
228292

229293
case 'PHP_Token_FUNCTION':
230-
$name = $token->getName();
294+
$name = $this->tname($tokens, $idx);
231295
$tmp = array(
232296
'docblock' => $token->getDocblock(),
233297
'keywords' => $this->getKeywords($tokens, $i),

0 commit comments

Comments
 (0)