<?php
namespace Test;
$function = function () {
return $a + $b . "Hello {$c} World!";
};
// Reflect the closure and fetch relevant properties
$reflection = new \ReflectionFunction($function);
$fileName = $reflection->getFileName();
$startLine = $reflection->getStartLine();
$endLine = $reflection->getEndLine();
// Slice out the lines
$lines = array_slice(file($fileName), $startLine - 1, ($endLine - $startLine) + 1);
$code = sprintf('<?php %s ; ?>', implode($lines));
// Tokenize and filter T_WHITESPACE
$tokens = array_filter(token_get_all($code), function ($token) {
return !(is_array($token) && $token[0] === T_WHITESPACE);
});
// Dump for shits and giggles
foreach ($tokens as $token)
{
echo PHP_EOL;
if (is_array($token))
{
echo str_pad(token_name($token[0]), 30, '.') . trim($token[1]);
continue;
}
echo str_pad('SYMBOL', 30, '.') . $token;
}
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- T_OPEN_TAG....................<?php
T_VARIABLE....................$function
SYMBOL........................=
T_FUNCTION....................function
SYMBOL........................(
SYMBOL........................)
SYMBOL........................{
T_RETURN......................return
T_VARIABLE....................$a
SYMBOL........................+
T_VARIABLE....................$b
SYMBOL.........................
SYMBOL........................"
T_ENCAPSED_AND_WHITESPACE.....Hello
T_CURLY_OPEN..................{
T_VARIABLE....................$c
SYMBOL........................}
T_ENCAPSED_AND_WHITESPACE.....World!
SYMBOL........................"
SYMBOL........................;
SYMBOL........................}
SYMBOL........................;
SYMBOL........................;
T_CLOSE_TAG...................?>
- Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_STRING in /in/erXcJ on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_STRING in /in/erXcJ on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/erXcJ on line 3
Process exited with code 255.
preferences:
261.91 ms | 401 KiB | 366 Q