Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check a php file for undefined functions?

Tags:

php

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
}
like image 490
o0'. Avatar asked Mar 29 '12 07:03

o0'.


People also ask

How do you check if a function is working in PHP?

Determining if a PHP function is available You can determine whether or not a PHP function is enabled for your web site by using the function_exists() function.

What is an undefined function in PHP?

Code Inspection: Undefined functionReports the references to functions that are not defined in the project files, configured include paths, or among the PHP predefined functions. In the following example, the undefined_function() is not defined in the built-in library and project files.

How to fix Notice Undefined variable in PHP?

The solution simply is to check if it has been set before referencing. We can use the isset() function, which checks if the variable 'is set' or not before referencing to them. Isset() function returns True or False depending on it.


1 Answers

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?

like image 92
Jon Avatar answered Sep 21 '22 22:09

Jon