2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.