Hmm, works in php as well!
function add(): float
{
$trace = debug_backtrace();
$file = $trace[0]['file'];
$line = $trace[0]['line'];
$content = file($file);
$lineContent = trim($content[$line - 1]);
$ast = token_get_all("<?php\n{$lineContent}");
$args = [];
foreach ($ast as $token) {
if (!is_array($token)) {
continue;
}
if ($token[0] !== T_COMMENT) {
continue;
}
$commentContent = $token[1];
if (str_starts_with($commentContent, '#')) {
$commentContent = substr($commentContent, 1);
} else {
$commentContent = substr($commentContent, 2);
}
$commentContent = trim($commentContent);
$commentContent = preg_replace("@\s+@", " ", $commentContent);
$args = explode(" ", $commentContent);
$args = array_map(function (string $arg) {
if (!is_numeric($arg)) {
throw new InvalidArgumentException('Argument must be a number');
}
return str_contains($arg, '.') ? (float) $arg : (int) $arg;
}, $args);
break;
}
return array_sum($args);
}
echo add(); // 1 2 3
echo add(); // 7 8 9