include_validate proposal

I posted this on the bug report board over at PHP.net

https://bugs.php.net/bug.php?id=68951

It’s a proposal for include_validate(), a statement that analyzes but does not load a php file for parse errors, references to non-existent classes and functions, or attempts to register classes and functions which are already declared. It is meant for use in large PHP applications that run 3rd party code such as Drupal or Wordpress, as an aid to the install process (you probably wouldn’t want to verify every file every time you run the program).

Thoughts?

How does one analyze a file for references to non-existant functions without loading it into the functional workspace to identify the functions that exist? Especially since includes can occur inside non-global scopes…

Parse/Syntax errors, sure, you can do that pretty easily by running it through the tokenizer and see if it hits a fault. But… there’s a functional gap there.

I’m not sure I can see the point in this. How often is software released with parse errors?

Secondly, how is this useful? I’d rather see the error message telling me there’s a problem than having it silently omitted because there was an error.

If you really wanted this kind of functionality then a better proposal would be making include/require throw an exception rather than a fatal error. That way you can do what you want with it.

The point of this is to protect the code that is running the module, particularly during it’s development. Drupal 8’s beta can become completely mucked up if a module throws a parse error while being installed, which can happen when you’re building the thing. It’s dev tool. To say this isn’t useful is like saying assert isn’t useful or debug_backtrace isn’t useful on the grounds that there’s no production use for them either. This is a framework and CMS development aid that lets the host code protect itself from module code under dev.

But surely parse errors will be identified during the development process of those modules?

I’m not saying it’s not useful I’m just not sure that highlighting a design flaw in drupal is a legitimate use-case. This sort of thing is easily fixed in several ways

  1. If you use inversion of control throughout this never happens anyway. using new A(new B(new C(new D) means that no line of code will be run until all the classes have been loaded.

  2. Use a directory iterator to include all the files prior to starting install process if this is a legitimate concern. As it’s only done by an install the performance overhead won’t matter.

  3. Write your own include_validate function:

function include_validate($file) {
   $str = system('php ' . $file);
   if (strpos($str, 'Parse Error')) throw new Exception('Parse error...');
   require_once $file;
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.