Search code examples
phpphpdoc

How to document mixed array return type?


What is the correct syntax to document an array of mixed strings and ints?

public function toArray(): array
{
    return [
        'string',
        42,
    ];
}

Here are the options I've considered:

/**
 * @return string|int[]
 */

But this seems to indicate it would either be a string or an int[]

/**
 * @return string[]|int[]
 */

And this would seem to indicate either an array of strings or an array of ints.


Solution

  • You can use

    @return (int|string)[]
    

    More details on phpdoc