phpopenapiphpstanswagger-php

cannot get rid of phpstan errors since switching to php attributes for OpenApi - has no value type specified in iterable type array


I am using swagger to create an OpenApi documentation. I am switching now to php attributes and phpstan in level 6 complain about missing specific array declaration, which worked totally fine as long as I was using annotations.

I give you an example:

<?php

declare(strict_types=1);

namespace App\Api\Dto;

use OpenApi\Attributes as OA;

#[OA\Schema()]
class UserDto implements DtoInterface
{
    #[OA\Property(description: 'Personalnummer ', type: 'string')]
    public ?string $uid = null;

    #[OA\Property(description: 'Name,Vorname', type: 'string')]
    public ?string $username = null;

    #[OA\Property(description: 'Stammhaus', type: 'string')]
    public ?string $mainhouse = null;

    #[OA\Property(description: 'Name, Vorname', type: 'integer')]
    public ?int $companyId = null;

    #[OA\Property(description: 'Symfony Rollen in der Applikation', type: 'array')]
    /** @var array<string> */
    public array $roles = [];

    #[OA\Property(description: 'Rechte', type: 'array', items: new OA\Items(type: 'string'))]
    /** @var array<string> */
    public array $grants = [];

    #[OA\Property(description: 'Ressourcen', type: 'array')]
    /** @var array<string, array<string>> */
    public array $resources = [];
}

This results on phpstan analyse level 6 in the following errors:

 ------ ---------------------------------------------------------------------------------------------------
  Line   Api/Dto/UserDto.php
 ------ ---------------------------------------------------------------------------------------------------
  26     Property App\Api\Dto\UserDto::$roles type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
  29     Property App\Api\Dto\UserDto::$grants type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
  33     Property App\Api\Dto\UserDto::$resources type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
 ------ ---------------------------------------------------------------------------------------------------

The lines mentioned being the array definitions.

Of course I can always switch down to level 5, but this feels like giving up. What would be the proper array annotation to make phpstan pass?

php8.2 nelmio/api-doc-bundle:4.28 phpstan:1.11 phpstan/phpdoc-parser:1.29


Solution

  • You need to write properties like this:

        /** @var array<string> */
        #[OA\Property(description: 'Rechte', type: 'array', items: new OA\Items(type: 'string'))]
        public array $grants = [];
    

    This is a known limitation of nikic/PHP-Parser: https://github.com/nikic/PHP-Parser/issues/762 It does not see PHPDocs between attributes and properties.