Reputation: 396
I want to extract the whole function given its name and starting line-number.
Output should be something like
function function_name( $a = null, $b = true ) {
$i_am_test = 'foo';
return $i_am_test;
}
or whatever the function definition is. Most tools (including grep etc.) only return the first line function function_name( $a = null, $b = true ) {
but I need the entire function definition.
Upvotes: 2
Views: 696
Reputation: 95392
To accurately extract a function (variable/class/....) from a computer program source file, especially for PHP, you need a real parser for that languages.
Otherwise you'll have some kind of hack that fails for an amazing variety of crazy reasons, some of which have to do with strings and comments confusing the extraction machinery (and trying skip string literals is PHP is nightmare), and some have to do with funny language rules you don't discover until you trip over it (what happens if your PHP file contains HTML that contains stuff that looks like PHP source code?).
Our DMS Software Reengineering Toolkit has a full PHP5 front end that understand PHP syntax completely. It can parse PHP source files, and then be configured to analyze/extract whatever code you want. The parser accurately captures line numbers on its internal ASTs, so it is quite easy to find the code in file at a particular line number; given the code/AST, it is quite easy to print the AST the represents the code at that line number. If you find a function identifier on a particular line and print out the relevant AST, you'll exactly the function source code.
Upvotes: 1