11

Say I have a function like this:

function theFunction() {
    $arr = array();
    for ($i=0;$i<10;$i++) {
        $arr[] = new theObject($i);
    }
    return $arr;
}

I need to document the return type of the function. I could of course just usearray, but that does not provide all the information that can be provided, and doesn't tell the developer much about the true nature of the function.

How do I document the type "array of [type]" in PHPDoc?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • have you tried `@var ObjectType[]`? – Robert Jun 11 '13 at 11:10
  • @Robert no, but searching for that gave a result! :) http://www.phpdoc.org/docs/latest/for-users/types.html#arrays –  Jun 11 '13 at 11:12
  • See https://github.com/phpDocumentor/phpDocumentor2/issues/650 – Ben Creasy Jan 24 '17 at 01:44
  • For anyone visiting this question, there is a much better duplicate question here with a comprehensive set of (correct) answers [PHPDoc type hinting for array of objects?](https://stackoverflow.com/questions/778564/phpdoc-type-hinting-for-array-of-objects) – BadHorsie Mar 03 '22 at 18:21

4 Answers4

15

From the documentation of phpDocumentor

The value represented by Type can be an array. The type MUST be defined following the format of one of the following options:

  1. unspecified, no definition of the contents of the represented array is given. Example: @return array

  2. specified containing a single type, the Type definition informs the reader of the type of each array element. Only one Type is then expected as element for a given array.

    Example: @return int[]

    Please note that mixed is also a single type and with this keyword it is possible to indicate that each array element contains any possible type.

  3. specified containing multiple types, the Type definition informs the reader of the type of each array element. Each element can be of any of the given types. Example: @return (int|string)[]

    Note
    many IDEs probably do not support this notation yet.

Pang
  • 9,564
  • 146
  • 81
  • 122
Robert
  • 19,800
  • 5
  • 55
  • 85
  • For multiple types, PHPStorm also seems okay with `int[]|string[]` but I'm not sure that it should be recommended over `(int|string)[]`. – gapple Jan 16 '17 at 21:39
  • 7
    I don't think int[]|string[] means the same than (int|string)[]. The first represents an array of int elements or an array of string elements. The latter represents an array that can have elements of both types mixed. – Federico J. Álvarez Valero Feb 14 '17 at 10:06
8

In PHPDoc you can do the following for type hinting array members:

@var array<\My\Folder\ClassName>

UPDATE

You can also explicitly declare the key for associative arrays as follows:

@var array<string, \My\Folder\ClassName>

And according to the answer posted here you can even explicitly declare the keys in case you would like to do so like this:

/**
 * @return array[
 *  'controller' => string,
 *  'action' => string
 * ]
 */

Note: This is tested and works perfectly well in PHP storm:

enter image description here

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • How do you document the key of the array as well? Could this work: `@var array` (array mapping strings to ints) or `@var array` (array mapping ints to Type)? Thank you. – tonix Aug 27 '20 at 07:56
  • The link you provided me are about JavaScript. Associative arrays in PHP don't have numbered keys, the key is a string... How would you document that? Thank you! – tonix Aug 28 '20 at 07:49
  • @tonix, so sorry for that, I somehow thought this was about Javascript, but I was apparently no longer awake while answering. I updated my answer... – Wilt Aug 28 '20 at 09:51
  • OK! Thank you! This `array` syntax is pretty similar to Java's generics. – tonix Aug 28 '20 at 20:33
  • This answer is completely wrong. Proof that people don't even check what they copy and paste from Stack Overflow. – BadHorsie Mar 04 '22 at 09:20
  • 1
    @BadHorsie I always try to thoroughly test and confirm the working of the code in my answers. I added a screenshot from my PHP-storm that I made today. As you see the Intellij IDE understands the type declaration perfectly well, so not sure why you accuse me of copy pasting without checking... – Wilt Mar 07 '22 at 08:29
  • @Wilt Sorry, should've been more specific. I am referring to the `@var array` syntax that remains from your original answer. I believe PhpStorm can interpret it but [phpDocumentor doesn't use this type of syntax](https://docs.phpdoc.org/guide/references/phpdoc/types.html#arrays). The standard way to define an array of strings would be `@var string[]`. The copy/paste comment was directed at all the people who upvoted the original answer with JavaScript context as well, not yourself. – BadHorsie Mar 07 '22 at 20:31
0

In PHP 7.4+ you can use type declarations to enforce the return type of a method.

While this is not meant for documentation, it does a better job of documenting than phpDocs did.

In your case, you can do:

function theFunction(): array {
    return [];
}
SamGoody
  • 13,758
  • 9
  • 81
  • 91
-2

If I'm remembering right you supply the return type and a description, can you not put it in the description?

/**
 * blah
 * @return array array of types
 */
Dale
  • 10,384
  • 21
  • 34
  • That's what I'm doing now, but it would be neater to use the standard way if it existed :) –  Jun 11 '13 at 11:11