Skip to content

Add conditional return types to client methods #888

@miken32

Description

@miken32

Is your feature request related to a problem? Please describe.
PhpStan complains about my generated API classes, because they can return either an API object or a PSR response interface. So code like this:

$client = $this->getApiClient();
$result = $client->findController($params);
$list = $result?->getList();

Gives errors like this:

Call to an undefined method App\Generated\Model\SystemControllerList|Psr\Http\Message\ResponseInterface::getList().

Where the client method looks like this:

/**
 * @return null|\App\Generated\Model\SystemControllerList|\Psr\Http\Message\ResponseInterface
 */
public function findController(array $queryParameters = [], string $fetch = self::FETCH_OBJECT)

Describe the solution you'd like
Use conditional return types, because we know when ResponseInterface is returned:

/**
 * @return null|($fetch is 'object' ? \App\Generated\Model\SystemControllerList : \Psr\Http\Message\ResponseInterface)
 */
public function findController(array $queryParameters = [], string $fetch = self::FETCH_OBJECT)

Unfortunately we have to use the fixed string "object" instead of the constant in the expression, but it's fine since it's all generated code anyway.

Describe alternatives you've considered
Currently we post-process files after generation :(

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions