How to check php file for undefined functions?

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

Is there any way to check undefined functions? I do not need to work with functions defined at runtime and dynamic calls .

Calling the file will obviously not work . I need to check the whole file, not just the branch that will be executed. See the following example.

myfile.php:

 function imhere () {} 

main.php:

 require_once 'myfile.php'; if (true) { imhere(); } else { imnhothere(); // I need to get a warning about this } 
+6
source share
3 answers

Testing undefined functions is not possible in a dynamic language. Note that undefined functions can occur in many 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 inclusion or otherwise). In the worst case scenario, consider this scenario:

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

Is the above program called by the undefined function?

+2
source

Check that the syntax is needed only to check the current contents of the php file, but checking for a specific function or not is something else, because functions can be defined in the required file.

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

Or you might want this .

0
source

After commenting on tennis, I thought I would close some thoughts in response.

It seems that you might need some kind of higher level testing (integration or web testing). With their help, you can test the full modules or your entire application and how they will hang together. This will help you isolate undefined function calls, as well as many other things, by running tests against entire pages or class groups with the appropriate include statements. You can use PHPunit to test integration (and also for unit-), although I'm not sure that you would have benefited much from this.

It would be better to explore web testing. There are a number of tools that you could use (this is by no means intended for a complete list):

  • Selenium , which I believe can be connected to PHPUnit
  • SimpleTest , not sure how well this is supported, but I used it in the past for simple (!) Tests
  • behat , this implements BDD using Gherkin, but I understand that it can be used for web testing ( act with Goutte
0
source

Source: https://habr.com/ru/post/911917/


All Articles