Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document an array with subarrays (mixed type)?

To document a variable that can take an array that receives a vector whose values ​​are strings:

/*
 * @var string[] An array of string objects.
 */
$foo = array('A', 'B', 'C');

To document a variable that can take an array that receives a vector whose values ​​are integer:

/*
 * @var int[] An array of string objects.
 */
$foo = array(1, 5, 0);

how should I document a variable whose values ​​are mixed arrays?

I need document a array like this:

$foo = array(
    array('value A',  1, $this),
    array('value b',  2, NULL),
    array('value X', 15, new Test)
);

I imagine it's something like this:

/*
 * @var array[][string|int|object|null] Description.
 */
like image 669
Guilherme Nascimento Avatar asked Sep 29 '22 23:09

Guilherme Nascimento


1 Answers

According to the current draft of PHPDoc (FIG PSR-5 applicant), the @var tag is deprecated. They suggest using an @type tag instead.

The type of variable is still an array; the contents of that array would be be briefly mentioned in the description.

/**
 * @var array $foo An array of string elements.
 */

or

/**
 * @type array $foo An array of string elements.
 */

If the variable might contain things other than strings, I might say An array of mixed elements., or if I knew specifically what they might be An array of bool|string|object elements.

If the variable itself might be of various types, I would give the list of types it might be.

/**
 * @type bool|string|array $foo Mixed type, depending on result of baz().
 */
like image 95
Amgine Avatar answered Oct 26 '22 11:10

Amgine