3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on S0CIX
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Using $this when not in object context in /in/MStua:14 Stack trace: #0 /in/MStua(76): parse_tokens('class TestTextB...') #1 {main} thrown in /in/MStua on line 14
Process exited with code 255.
Output for 5.4.10 - 5.6.28
Fatal error: Using $this when not in object context in /in/MStua on line 14
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/MStua on line 39
Process exited with code 255.