Taming Symfony 2 projects with VIM

Over the last few days of Symfony 2 training, my vim-ing has endured a severe whipping from the likes of NetBeans and other full-fledged IDE’s. It didn’t help that the trainer was doing all his code examples in NetBeans. I shuddered everytime we did a namespace or use statement. Symfony 2 takes full advantage of the PHP 5.3 namespace feature, so with all the classes organized hierarchically, the average class ends up being about 5 levels deep.

So last night, I thought I’d try out NetBeans to see if I could use it. To my delight, there was also a plugin called jVi that allows me to use vim keybindings. After 10 minutes of messing around, I just couldn’t do it.

Traversing the project tree required reaching for the mouse. jVi refused to work because of some bug in NetBeans 7.1, and the line-number fonts were hardcoded ugly. Finally, as I was doing a lot of my work on a virtual machine over a samba share, NetBeans was struggling to index all the files for auto-completion and class/method name lookups.

Fine, I thought, surely there must be something I could use in vim.

After poking around a little, I discovered exuberant-ctags + FuzzyFinder + SuperTab.

Broadly speaking, this is how it works.

1. You run something like ctags --recurse --h *.php in the root of your project. ctags scans all your *.php files and generates a file in the root of your project tree called tags.

2. When you use the :FufTag command in vim (provided by FuzzyFinder), it uses the generated tags to find a class or method definitions that match your query.

3. When you type ctrl-] (also by FuzzyFinder) with your text cursor on a class or method name, you’re able to jump straight the definition of that class or method.

4. Finally, with SuperTab enabled, you can hit tab while you’re coding, and it will pull up auto-complete options based on the tags file generated by ctags.

Can’t wait to try it out at work today.

gatlingGun shoots holes in your array

Here’s a snippet I created to generate invalid data from a valid key-value array. It’s really handy for testing. Feel free to use it.

  /**
   * Gatling gun takes an array of valid params and returns every
   * combination of invalid data caused by blanking out individual params.
   *
   * Example:
   *
   * $validData = array('firstName' => 'Gatling',
   *                    'lastName' => 'Gun',
   *                    'age' => 12);
   *
   * $ggData = gatlingGun($validData);
   *
   * $ggData = array(
   *     array('firstName' => '',        'lastName' => '',    'age' => ''),
   *     array('firstName' => '',        'lastName' => '',    'age' => 12),
   *     array('firstName' => '',        'lastName' => 'Gun', 'age' => ''),
   *     array('firstName' => '',        'lastName' => 'Gun', 'age' => 12),
   *     array('firstName' => 'Gatling', 'lastName' => '',    'age' => ''),
   *     array('firstName' => 'Gatling', 'lastName' => '',    'age' => 12),
   *     array('firstName' => 'Gatling', 'lastName' => 'Gun', 'age' => '')
   * );
   *
   *
   * @param array $validData
   * @return array
   *
   */
  function gatlingGun($validData)
  {
      $original = $validData;
      $invalidCombinations = array();

      $max = count($original);
      $keys = array_keys($original);
      $combinations = pow(2, $max) - 1;

      for ($i = 0; $i < $combinations; $i++) {
           $tmp = array();
           $j = count($keys) - 1;
           foreach ($keys as $k => $key) {
              $tmp[$key] = (($i & pow(2,$j)) == 0) ? '' : $original[$key];
              $j--;
          }

          $invalidCombinations[] = $tmp;
      }
      return $invalidCombinations;
  }

Just be careful, the number of combinations it comes up with is 2^n - 1. So the number of invalid combinations from an array with 10 key-value pairs will quickly balloon to 1023.

Do drop me a line to let me know if this has been helpful for you.