<?php
function parse_tokens($code) {
$ns = '';
$tokens = token_get_all($code);
/* Assume token 0 = '<?php' and token 1 = whitespace */
for ($i = 2, $c = count($tokens); $i < $c; ++$i) {
if (!is_array($tokens[$i])) {
continue;
}
switch ($tokens[$i][0]) {
case T_CLASS:
list($class, $i) = $this->parse_class($tokens, $i);
if ($class) {
$test = $this->instantiate_test($loader, $ns . $class);
if ($test) {
$this->runner->run_test_case($test);
}
}
break;
case T_NAMESPACE:
//list($ns, $i) = $this->parse_namespace($tokens, $i, $ns);
break;
}
}
}
function parse_class($tokens, $i) {
/* $i = 'class' and $i+1 = whitespace */
$i += 2;
while (!is_array($tokens[$i]) || T_STRING !== $tokens[$i][0]) {
++$i;
}
$class = $tokens[$i][1];
print "$class\n";
if (0 === stripos($class, 'test')) {
return [$class, $i];
}
return [false, $i];
}
$file = <<<'FILE'
class TestTextBefore {}
<?php
/*
class TestComment {}
*/
class Test {}
<<<STRING
class TestString {}
STRING;
class test2 {
public function test1() {}
public function test2() {}
public function test3() {}
}
class NotATest {}
class // valid tokens between the 'class' keyword and the test name
/* should be handled correctly */
Test3 {}
?>
class TestTestAfter {}
FILE;
parse_tokens($file);
preferences:
88.28 ms | 402 KiB | 5 Q