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.
PhpStorm IDE can find this and many other errors in PHP-code. It's Inspections
feature of this IDE.
I'm just user of this IDE, it's not marketing :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With