36

I use several associative arrays in my PHP application and I'm using PHP documentor to comment my sources. I never really did specify comments for the arrays in an array, but now I need to do that and don't know how.

$array = array('id' => 'test', 'class' => 'tester', 'options' => array('option1' => 1, 'option2' => 2))

How do I comment this array in the correct way for @var and @param comments? I could do this like this, but I don't know if this is correct:

@param string $array['id']
@param string $array['class']
@param int $array['options']['option1']

But how to do this for the @var part?

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
Abenil
  • 1,048
  • 3
  • 12
  • 26

4 Answers4

32

You can't document each key, but you can tell phpDocumentor what type it is.

You could do something like this:

/**
 * Form the array like this:
 * <code>
 * $array = array(
 *   'id'      => 'foo',          // the id
 *   'class'   => 'myClass',     // the class
 * );
 * 
 * </code>
 *
 * @var array[string]string 
 */
$array;
Stephen Fuhry
  • 12,624
  • 6
  • 56
  • 55
  • 8
    Has this been confirmed to work with auto-complete/intellisense in any IDEs, I wonder? According to [the phpDoc ABNF for type definitions](http://www.phpdoc.org/docs/latest/for-users/types.html#abnf), there's no allowance for a type to be specified for the array index. And it specifies array as `@var string[]` (the `array` component is only supposed to be present for "unspecified" arrays). – Sepster Sep 08 '12 at 16:40
  • @Sepster I don't think most IDEs are smart enough to recognize this, unfortunately. Your mileage may vary, but I even find Zend Studio's implementation a bit lacking when it comes to this sort of accute type awareness. – Stephen Fuhry Sep 18 '12 at 22:28
  • 2
    Updated link for ABNF mentioned in Sepster's comment: https://www.phpdoc.org/docs/latest/references/phpdoc/types.html – faintsignal Dec 01 '16 at 17:30
  • 1
    The example is confusing, it's not possible by looking at the `@var` annotation which type is used for the array key or value. Now I can't figure out if the `string` type hint inside or after the squared brackets is specifying the type of the key. – luukvhoudt Jun 11 '20 at 20:30
  • The latest ABNF for types: https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types – Jordan Pickwell Jul 14 '20 at 19:08
  • I think this answer may be misleading. It says "phpDocumentor". However the link actually leads to "PHPLint". I think it's a different software. There's no reason why PHPLint syntax would also be valid phpDoc syntax. – Gherman May 17 '21 at 17:22
  • While this does not answer the question directly, the provided solution is probably the best one. Better only to refactor the array into DTO. – AnrDaemon Jun 09 '22 at 17:07
  • As @Gherman pointed out the answer is misleading. The phpDocumentor documentation does not provide key hinting: https://docs.phpdoc.org/3.0/guide/references/phpdoc/types.html#arrays – Paperclip Oct 28 '22 at 09:23
11

I would look at the WordPress Inline Documentation Reference for some hints, though it's not currently comprehensive.

Use @param or @var or @property, whichever is appropriate in your context

According to those guidelines, you might document your associative array like this:

/**
 * @property array $my_array {
 *     An array of parameters that customize the way the parser works.
 *
 *     @type boolean $ignore_whitespace Whether to gobble up whitespace. Default true.
 *     @type string $error_level What the error reporting level is. Default 'none'.
 *                               Accepts 'none', 'low', 'high'.
 * }
 */
Andy
  • 4,901
  • 5
  • 35
  • 57
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
  • 3
    This notation for documenting array structures has never made it into official PHPDoc spec despite serious discussion in 2013-14 about adding it. – Andrew Sauder Dec 14 '16 at 20:50
  • 2
    Seems to be some related discussion at https://github.com/phpDocumentor/phpDocumentor2/issues/650 – Ben Creasy Jan 24 '17 at 01:45
  • The WordPress guide seems similar to what Psalm recommends: https://psalm.dev/docs/annotating_code/type_syntax/array_types/#object-like-arrays – Gogowitsch May 03 '21 at 16:15
8

What works in Phpstorm is:

/**
 * @return array{ hand: Hand, card: CardType | null }
 */
Alex
  • 81
  • 1
  • 2
5

For me this works fine in PhpStorm for nice return value description:

/**
 * @param string $requestUri
 * @return array[
 *  'controller' => string,
 *  'action' => string
 * ]
 */
  • 4
    Tried this in Webstorm 2020.1 EAP for a param description and it mangled the help popup. In my experience this doesn't work. – Chad Mar 09 '20 at 21:28
  • 1
    I can confirm this does not work with VSCode or PHPDocumentor either. – BlackPanther Oct 13 '22 at 09:48
  • I voted your answer up, but I never accepted it. For me this was exactly what I was looking for back then. Thank you! I accepted it now. :D – Abenil Jul 05 '23 at 09:00