Getting more from your PHP and JavaScript code

Posted by jeff.griffiths on 2008-04-23 13:38

A small but important feature in recent releases of Komodo is the ability to provide the autocomplete system with type hinting through doc comments for PHP and JavaScript code. Using the PHPDoc and JSDoc conventions you can add a lot of smarts to how Komodo parses your code.

Example:

/**
 * mine
 * @param path {String}
 * @param domtag {element}
 * @returns {String}
 */

function mine(path, domtag) {
 
}

If you paste the above function into a javaScript file in Komodo, you should get appropriate completions for the arguments:

js_arg_ac

This works similarly in PHP, but instead using the PHPDoc syntax:

<?php

/*
 * function Mine
 * @param PDO $model
 * @param DOMDocument $node
 * @return DOMDocument
 */

function Mine($model, $node) {
  $model->exec($sql);
  return $newnode;
}

$domnode = Mine($x, $y);
?>

A screenshot of autocomplete using this code, this time getting autocompletion on the value returned from the function using the hinting supplied the @return tag in the doc string:

doc_php_ac

If you get used to using these simple documentation styles for all of your code, you'll find that Komodo suddenly starts to get a lot smarter when supplying autocomplete lists, and your code is more consistently documented as well =).

JSDoc manual: http://jsdoc.sourceforge.net/
PHPDoc manual: http://manual.phpdoc.org/HTMLframesConverter/default/

stan | Fri, 2008-04-25 00:47

This is an awesome feature, something I was missing in Komodo up until now. I was wondering if the following code would work:

class SomeClassName
{
  function SomeMethod()
  {
    /** @var SomeOtherClass **/
    $someLocalVar = SomeMethodThatReturnsMixed();
    $someLocalVar->…
  }
}

Will this supply an autocomplete list for $someLocalVar as an instance of SomeOtherClass? It is quite useful when you have foreach loops:

…
/** @var  $listEntry  SomeClass **/
foreach ($list as $listEntry) $listEntry->…

BTW, when you say recent releases, which build number are you referring to?

jeff.griffiths | Fri, 2008-04-25 16:06

I think we started doing this with 4.2.0, but it got smarter with 4.3. For example, we started parsing @return for PHP functions in 4.3.

As for your example, we don't currently do anything like that. In cases like iterator loops where you can be much more sure what type the iterator is. For example, we have some ideas around supporting something like this:

$dir = new DirectoryIterator($path);
foreach ($dir as $item) {
    // $item is a DirectoryIterator
    $item->
}

We're thinking of extending the standard code intelligence catalog data to add this, but this is currently just an idea.

--
JeffG

dmitryd | Mon, 2008-05-05 11:13

I discovered Komodo Editor today and I am very impressed with it. I mainly use PHP and Zend Studio to edit/debug PHP code. I have a strong wish to migrate to Komodo IDE now ;) The only thing that keeps me from doing it is one small missing feature.

Zend Studio and some other IDEs support PHP type hinting through semi-PHPdoc constructs. For example, this code will produce code completion in any PHP IDE:

$var = new MyClass;
$var->...

But the following code will not:

$class = 'MyClass';
$var = new $class;
$var->...

Zend Studio solves it with the following syntax:

$class = 'MyClass';
$var = new $class;
/* @var $var MyClass */
$var->...

In this case Zend Studio will assume $var is of type MyClass and make code completion. I'd love the same to be implemented in Komodo IDE. I work on a large open source project (TYPO3) that uses non-direct object creation all the time, so I will miss code completion always.

Screenshot of image completion in Zend Studio:
Code completion in Zend Studio

dmitryd | Mon, 2008-05-05 11:33