我正在使用 swagger 创建 OpenApi 文档。 我现在切换到 php 属性,第 6 级的 phpstan 抱怨缺少特定的数组声明,只要我使用注释,它就完全正常工作。
我举个例子:
<?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 = [];
}
这会导致 phpstan 分析级别 6 出现以下错误:
------ ---------------------------------------------------------------------------------------------------
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
------ ---------------------------------------------------------------------------------------------------
提到的行是数组定义。
当然我可以随时切换到5级,但这感觉就像放弃一样。 使 phpstan 通过的正确数组注释是什么?
php8.2 nelmio/api-doc-bundle:4.28 phpstan:1.11 phpstan/phpdoc-解析器:1.29
你需要这样写属性:
/** @var array<string> */
#[OA\Property(description: 'Rechte', type: 'array', items: new OA\Items(type: 'string'))]
public array $grants = [];
这是 nikic/PHP-Parser 的已知限制:https://github.com/nikic/PHP-Parser/issues/762 它在属性和属性之间看不到 PHPDocs。