6

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.

Community
  • 1
  • 1
Alex R
  • 11,364
  • 15
  • 100
  • 180

1 Answers1

2

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 :)

OZ_
  • 12,492
  • 7
  • 50
  • 68
  • So this will let you know if some requested page (or codebase) includes code that references methods (or functions) that don't exist within the included pages? Or would you have to go page by page? – Jared Farrish May 24 '11 at 01:03
  • @Jared Farrish First variant. It's IDE, not just editor. And this feature works in javascript too, AFAIK. – OZ_ May 24 '11 at 01:04
  • So it's like a compiler, it will warn you? If you just opened up a new project, how would this work? – Jared Farrish May 24 '11 at 01:06
  • @Jared Farrish do you see red square in top right corner? It means that errors are found. It's not like a compiler, it works instantly when you are typing or open code. Also you can check your files (whole directories) via menu call. You can check how it works - EAP-versions are free. – OZ_ May 24 '11 at 01:09
  • So in other words, you have to open each file and review each file for errors. – Jared Farrish May 24 '11 at 01:14
  • 2
    @Jared Farrish, in any words, you can check whole project per 1 click and get detailed list of errors. Looks like you want to find any cause for criticism. Man, it's not my software - download it, test it. – OZ_ May 24 '11 at 01:18
  • Who said I was criticizing? I was trying to understand. If you can get a report by project of errors, that's great. That was what I was wondering. – Jared Farrish May 24 '11 at 01:28
  • @Jared Farrish, then sorry. Online documentation: http://www.jetbrains.com/phpstorm/webhelp/code-inspection.html – OZ_ May 24 '11 at 01:40
  • So, the relevant pages would be http://www.jetbrains.com/phpstorm/webhelp/analyzing-inspection-results.html and http://www.jetbrains.com/phpstorm/webhelp/running-inspections.html ? Also, I did not downvote this answer. :) – Jared Farrish May 24 '11 at 01:44
  • Looks so. I spend to many time too this answer already, think it's enough :) – OZ_ May 24 '11 at 01:47