0

I modified about 600 lines of code amongst over 5000 lines of code by updating function calls to match the new library I created for use with the script. I have spot some errors manually when updating before by hand, but I believe I overlooked some.

So far, the only way I can spot them is to run the code and have it crash when the error happens. This is a bad idea because such errors will happen before resources are freed.

Here's an example in code that explains my question:

Say I have mainline code (called index.php) that consists of this:

<?php
    include "library.php";
    $file=fopen("afile","w");
    doWrite($file);
    brokenFunction();
    fclose($file);
    exit();
?>

and say library.php contains only this:

<?php
  function doWrite($file){
      fwrite($file,"Test");
      doNothing();
  }
?>

Because brokenFunction(); and doNothing(); don't exist, an error is expected. Rather than PHP compile then execute code up until the first failing function call, how do I have PHP check to see if all referenced functions the mainline code links to exist before executing code?

So in my example, I expect an error and the code to stop compiling/executing at $file=fopen("afile","w"); because brokenFunction(); and doNothing(); don't exist.

How do I achieve this?

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37

2 Answers2

0

You can use the built-in function_exists() function:

if (!function_exists('brokenFunction')) {
    throw new \Exception('brokenFunction is missing');
}

But this will only raise an error when executing the code.


Some tools like PHPStorm can check your code (without running it) and throw warnings if a function is missing.

Some other tools are listed in this (closed) SO question: Is there a static code analyzer [like Lint] for PHP files?.

Community
  • 1
  • 1
A.L
  • 10,259
  • 10
  • 67
  • 98
  • Well I'm definitely not going to be adding function_exists 600 times. I'm also not planning to download a complete IDE just to check functions – Mike -- No longer here Feb 25 '16 at 23:58
  • @Mike *just to check functions* That's not that simple to check if a function is declared or not in a given context. An IDE will do this work for you. – A.L Feb 26 '16 at 00:01
0

The best way I've found to globally debug an environment without using @A.L's method and pasting a function_exists call before every edited line, is to use a PHP debugger of some sort, most likely built into an IDE that compares every function call line against a 'test compile' of your code and all included libraries to make sure the called function exists (and would likely underline it in red if it didn't). A PHP IDE like Aptana might be what you're looking for (especially if you see yourself having future updates to run as this solution will have the time overhead of installing/setting up Aptana).

Joe
  • 4,710
  • 1
  • 11
  • 7
  • I'd like to avoid using an IDE just for this purpose. I also run linux. – Mike -- No longer here Feb 26 '16 at 00:02
  • Fair enough... PHP used to have a function called php_check_syntax that's now been merged into the CLI command "php -l " (that's a lower-case L, for lint). The catch is that even it executes the script, so it might or might not fall under the "lost resources" concern (depends on what else your library does). – Joe Feb 26 '16 at 00:44
  • I'm running php 5.4 and that option exists. I tried it on a simple test script file that calls a non-existing function and all I get is `No syntax errors detected in filename.php`. – Mike -- No longer here Feb 26 '16 at 01:37
  • This is because a missing function is a valid syntax. Syntax errors are unclosed strings, misplaced commas, etc. – A.L Feb 26 '16 at 21:54