Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, Tokenizer, find all the arguments of the function

Tags:

php

tokenize

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')
like image 530
Anton Avatar asked Jun 06 '11 06:06

Anton


1 Answers

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.

like image 135
lonesomeday Avatar answered Sep 26 '22 02:09

lonesomeday