6

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
}
12
  • What do you mean "check for undefined functions"? Check for calls to functions which are undefined? Also, what do you mean by "functions defined at runtime"? Custom functions defined using function x() { ... }? - as in, only include built-in PHP and extension functions when checking? Commented Mar 29, 2012 at 7:38
  • @JaniHartikainen it should check recursively the included files of course. Commented Mar 29, 2012 at 7:47
  • I am not sure, but you can try using is_callable. this will give warnings at runtime. php.net/manual/en/function.is-callable.php Commented Mar 29, 2012 at 8:01
  • @UdaySawant I don't need it at runtime, I need to parse the files from command line, just like I would do with php -l Commented Mar 29, 2012 at 8:03
  • 1
    Ok I've posted an answer as there's a bit too much info for a comment! Commented Mar 29, 2012 at 12:56

3 Answers 3

2

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?

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

3 Comments

That's why I explicitly said I don't need to check for special cases like your first example.
The second example instead should give no warning, since I also said it should check every branch, regardless (added an example).
@Lohoris You didn't explicitly say not to check for Jon's first example. You explicitly said not to check for functions defined at runtime, not dynamic function calls like in Jon's example.
0

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.

2 Comments

...*if* you happen to hit the code path which has the call to the undefined function.
Of course it won't work, because I need to check for the whole file, not just the part of it that gets executed... (@Jon exactly)
0

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

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.