Detected possibly uninitialized variables

This module is extension for the Coder module and it uses slightly modified php-initialized tool from Jakub Vrána to detect uninitialized variables. I have been using this tool for year now and based on my experience I must admit it helps me to find a lot of bugs, despite it generates also significant number of false positives. It allows to quickly find bugs in the completely unknown code because you can focus only on the parts which generated the warnings. E.g. finding 10 uninitialized variables in the thousand lines of the Media module including 2 regular bugs took me about half an hour.

You can try the original implementation online at author's website: http://www.vrana.cz/php-initialized/.

Tool for those who loves or hates developing for Drupal

The module aims to reduce number of notices which Drupal generates - especially the contrib modules - when E_ALL error reporting mode is enabled, because using php variables without initialization seems to be a common habit in the Drupal world and it can be seen even in the core. When you want to begin some serious development with E_ALL or E_STRICT error reporting enabled, you often need to spend half day or more to fix bugs in ten other modules before you just begin with your module development.

Installation

  1. Download Coder module and its dependencies and enable
  2. Download this module
  3. Download the modified php-initialized tool from branch "no-include" (*) and place it to coder_initialized/php-initialized directory.

Note (*): php-initialized tool is released under Apache license, version 2.0 which isn't compatible with the Drupal's GNU GPL v2 license so it can't be distributed together with the module due to Drupal.org policy.

Todo list:

  • Drush integration
  • Use Libraries API
  • Better Drupal integration
  • Reduce false positives
  • Integrate php-initialized tests

Examples

What kind of errors the php-initialized tool detects?

<?php
// Uninitialized variable $items
function my_module_menu() {
 
$items[] = array(
   
'title' => 'My page',
  );
  return
$items;
}
// Uninitialized variable $output
foreach ($items as $item) {
 
$output .= $item->title . ', ';
}
// Forgotten variables $v and $k
$output = '';
foreach (
$items as $key => $value) {
 
$output .= $k . ' => ' . $v;
}
?>

False positives

There are significant number of false positives which I want to reduce in the future development.

<?php
if ($a = 1) {
 
$b = 1;
}
else {
 
$b = 2;
}
// $b is detected as unitialized
$c = $b;
?>

Credits

Project Information

Downloads