I have been on a the lookout for tools to help me validate source code quality for PHP. Since PHP isn’t compile, a lot of errors which would be detected by a compiler are not found until runtime. And some errors won’t be revealed runtime in a specific context. I already know and use PHPUnit to write unit tests but there are still some errors that could/should be detected without running a test with the right context. For example, I know the code below would generate a warning with Visual Studio.

1
2
3
4
if (isset($_GET["userId"]) {
$userId = (integer)$_GET["userId"];
}
$user = loadUser($userId);

That’s all good and fine when a userId is sent but what happens when there isn’t one ?

Visual Studio doesn’t recommend a variable declaration inside a if statement arguing that, if the code doesn’t enter the if, the variable won’t be defined. With that warning, then the code would have been, for example :

1
2
3
4
5
$userId = $currentUserId;
if (isset($_GET["userId"]) {
$userId = (integer)$_GET["userId"];
}
$user = loadUser($userId);

Anyway, my search lead me to try Phantm, a tool written in Scala that can be used to detect potential programming errors inside PHP applications. It will both work for small isolated script as well as full-blown applications. phantm stands for “PHp ANalyzer for Type Mistakes”.

 

It took me a little while to get it to run so here is the solution :

  1. Download the phantm release jar
  2. Download the JFlex jar
  3. Download and install Scala
  4. Run phantm with
    1
    scala -classpath JFlex.jar  phantm-1.0.6.jar /Users/samobk/Workspace/bktrunk/application/controllers/api/v3/ApiBaseController.php
I have found some issues with Phantm, especially the lack of support for PHP namespaces but I will investigate it some more and try to incorporate it to my development workflow.
Filed under: Computing, Development, PHP

Tagged with: , , , , , , ,

Liked this page?

Tweet it

Subscribe to the RSS feed or sign up for the newsletter now.

Posts related to PHP Type Mismatch detection with Phantm

1 Comment to "PHP Type Mismatch detection with Phantm"

  1. Comment by Sylvester on October 2, 2012 at 8:04 pm Reply

    certainly like your website however you have to check the
    spelling on several of your posts. Several of them are rife
    with spelling problems and I in finding it very bothersome to
    inform the reality then again I will definitely come back again.

Name (required)

Email (required)

Website

Leave a Reply

  • About Me


    photo of Samantha Halfon Samantha Halfon
    Software Engineer
    blueKiwi software
    Paris, France
    I enjoy playing with my computer(s), listening to Bob Dylan (and related artists) and watching movies (especially if they were directed by Martin Scorsese or John Cassavetes). Sometimes, I play a little guitar... If not doing any of the above, I am either riding a small red bike around Paris, or, making videos. About my videomaking please check out World Wide Angle and its blog.
  • Flickr