Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any PHP static analyzers that detect non-existent class method calls?

Below is a syntactically valid PHP program, which works half of the time. In any static language, the equivalent lines would be a compile error:

<?php
class A {
 function a() { return 1; }
}

$x = new A();

if(rand(1,100) > 50) {
  print $x->b();
}
else {
  print $x->a();
}

?>

Sample output from PHP:

C:\temp>php static.php
1
C:\temp>php static.php
1
C:\temp>php static.php

Fatal error: Call to undefined method A::b() in C:\temp\static.php on line 9

Call Stack:
    0.9747     323920   1. {main}() C:\temp\static.php:0

Dynamic language proponents get all excited because, hey, this program works 50% of the time, whereas the equivalent program on a static language would fail to compile and therefore, work 0% of the time.

So, on to my question. Are there any PHP static analysis tools out there that will detect this specific class of problems?

I have read the related question: Is there a static code analyzer [like Lint] for PHP files?

But instead of trying all the tools mentioned in there one by one, I thought I'd ask a more specific question to zero-in on the one that can do this.

like image 340
Alex R Avatar asked May 24 '11 00:05

Alex R


1 Answers

PhpStorm IDE can find this and many other errors in PHP-code. It's Inspections feature of this IDE.

example for this code

I'm just user of this IDE, it's not marketing :)

like image 101
OZ_ Avatar answered Oct 20 '22 14:10

OZ_