views:

371

answers:

3

Is there a syntax for documenting functions which take a single configuration array, rather than individual parameters?

I'm thinking specifically of CodeIgniter-style libraries, which use a mechanism similar to this:

<?php

//
// Library definition
//

class MyLibrary {
  var $foo;
  var $bar;
  var $baz;
  // ... and many more vars...


  /* Following is how CodeIgniter documents their built-in libraries,
   * which is mostly useless.  AFAIK they should be specifying a name
   * and description for their @param (which they don't) and omitting
   * @return for constructors 
   */

  /** 
   * @access public
   * @param array
   * @return void
   */
  function MyLibrary($config = array()) {
    foreach ($config as $key => $value) {
      $this->$key = $value;
    }
  }
}

//
// Library usage:
//

// Iniitialize our configuration parameters
$config['foo'] = 'test';
$config['bar'] = 4;
$config['baz'] = array('x', 'y', 'z');

$x = new MyLibrary($config);

?>

So my question is, is there some supprted way of documenting the configuration array beyond just the purely textual description? Actually specifying a proper "@param [type] [name] [desc]" that allows PHPDoc to parse out useful values?

UPDATE

As an aside, CodeIgniter really does just overwrite it's own values with those passed in via the $config array as above, effectively allowing you to clobber private members. I'm not a fan, but I'm stuck with it.

+3  A: 

I've never seen any "good" way of documenting this -- and I've never seen anything that could be used by IDEs (such as Eclipse PDT) for parameters hinting either :-(

I would have said "do like your framework does", but as you said, what it does, here, is not quite good enough...


Maybe a quick/sort list of possible keys might be better than nothing, though ; a bit like this :

@param array $config [key1=>int, otherKey=>string]

Not sure how it would be interpreted by phpDocumentor or an IDE... But might be worth a try ?


This is, btw, one reason for which I tend to avoid that kind of way of passing parameters -- at least when there are not too many (optionnal) parameters to a method.

Pascal MARTIN
I'd avoid it if I could, unfortunatly CodeIgniter's conventions require this sort of sloppy config array usage rather than, oh say, regular old parameters.
meagar
@meagar : yeah, I guessed/understood that, but couldn't resist ^^ (and this might be helpful if someone else, some day, arrives on this question/answer)
Pascal MARTIN
That's similar to how I do it. PHPDoc will take the list and add it to the documentation just like any other string, so it is better than nothing. PDT cannot make sense of it though. It will just know it is an array.
Gordon
A: 

A text description, to whatever degree of completeness you want, is really your only option. You can make it as legible as you want, but code analysis tools (phpDocumentor, IDE support) has no way to know how your $array is actually structured at runtime.

I agree with the many commenters that writing code this way exchanges coding convenience for code legibility.

ashnazg
A: 

The correct array @param notation for arrays is as specified in PHPlint

Example:

 /**
 * Does stuff
 *
 * @param array[int|string]float $bar
 * @param array[int|string]array[string]Object $multidim
 *
 * @return array[int]string
 */
public function foo(array $bar, array $multidim)
{
    // do stuff here

    return array('foo', 'bar', 'baz');
}
Structed
This doesn't address my question about documenting which specific array keys are used as optional named parameters.
meagar