5

Help me find all the arguments of the function "funcname" using the function token_get_all() in the source code. It sounds simple, but there are many special options, such as arrays as parameters or call static methods as parameters. Maybe there's a simple universal solution?

UPD:

I need the function arguments passed when you call it. Get them to be at an external analysis of the file. For example, there is a php-file:

<?php
funcname('foo');
funcname(array('foo'), 'bar');

The analyzer should begin as follows:

$source = file_get_contents('source.php');
$tokens = token_get_all($source);
...

As a result, need to get a list like this:

[0] => array('foo'),
[1] => array(array('foo'), 'bar')
Anton
  • 811
  • 9
  • 13
  • 1
    Hi Anton, I have a similar issue for static analysis of localisation functions. Did you ever find a solid solution to this? – Gavin Apr 10 '13 at 12:34
  • Does PHP-Parser ( https://github.com/nikic/PHP-Parser ) can help? – vee May 12 '21 at 14:18

1 Answers1

5

Rather than using a tokenizer, use reflection. In this case, use ReflectionFunction:

function funcname ($foo, $bar) {

}

$f = new ReflectionFunction('funcname');
foreach ($f->getParameters() as $p) {
    echo $p->getName(), "\n";
}

This outputs

foo
bar

You can also use this class and related classes (such as ReflectionParameter) to find out more information about a function and its parameters, such as whether a parameter is optional and what its default value is.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318