o0'.
o0'.

Reputation: 11863

How can I check a php file for undefined functions?

Calling php -l checks the syntax of a php file.

Is there a way to check for undefined functions? I don't need it to work with functions defined at runtime nor with dynamic calls.

Calling the file obviously won't work: I need to check the whole file, not only the branch that gets executed. See the follwing example.

myfile.php:

function imhere () {}

main.php:

require_once 'myfile.php';

if (true) {
    imhere();
}
else {
    imnhothere(); // I need to get a warning about this
}

Upvotes: 6

Views: 236

Answers (3)

liquorvicar
liquorvicar

Reputation: 6126

Following comment tennis I thought I would wrap up some thoughts into an answer.

It seems like what you might need is some higher-level testing (either integration or web testing). With these you can test complete modules or your whole application and how they hang together. This would help you isolate calls to undefined functions as well as a host of other things by running tests against whole pages or groups of classes with the relevant includes statements. You can PHPunit for integration testing (as well as for unit-) although I'm not sure that would gain you that much at this point.

Better would be to investigate web-testing. There are a number of tools you could use (this is by no means intended to be a complete list):

  • Selenium which I believe can hook into PHPUnit
  • SimpleTest, not sure how well maintained this is but I have used it in the past for simple (!) tests
  • behat, this implements BDD using Gherkin but I understand it can be used to do web-testing (behat with Goutte

Upvotes: 0

Jon
Jon

Reputation: 437874

Checking for undefined functions is impossible in a dynamic language. Consider that undefined functions can manifest in lots of ways:

undefined();
array_filter('undefined', $array);
$prefix = 'un'; $f = $prefix.'defined'; $f();

This is in addition to the fact that there may be functions that are used and defined conditionally (through includes or otherwise). In the worst case, consider this scenario:

if(time() & 1) {
    function foo() {}
}

foo();

Does the above program call an undefined function?

Upvotes: 2

xdazz
xdazz

Reputation: 160963

Check the syntax only need to scan the current content of the php file, but check for function defined or not is something different because functions could be defined in the required file.

The simplest way is just to execute the file, and undefined function will give you a PHP Fatal error: Call to undefined function .....

Or you may want this.

Upvotes: 0

Related Questions