Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

https://phpstan.org/r/c81d0b3a-ce5a-49c7-983c-d7a310a16c68

I want to get something like this to work:

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

/**
 * API interop.
 *
 * @template T
 * @phpstan-type ResponsePayload<T> array{
 *  data: T,
 *  links: ?array{
 *      self: string,
 *      first: string,
 *      last: string,
 *      next: string,
 *  },
 *  pager: ?array{
 *      total: int,
 *      current: int,
 *      limit: int,
 *  },
 * } API response template
 */

/** @return ResponsePayload<array> */
function x(): array {
    return ['data' => []];
}

function y(): array {
    $x = x()['data'];
    return $x;
}

It defines a basic array structure returned from API call to the back-end service. But it does not seems to work right.
Is there a way to achieve what I want?

You must be logged in to vote

Replies: 1 comment · 1 reply

Comment options

As far as I know, @phpstan-type can be used in conjunction with classes, interfaces, and traits (https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases)
Also, as far as I know, type aliases do not support generics.
In my opinion, if you want to use @phpstan-type without a class, interface, or trait, the best solution is to use global type aliases: https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases. However, generics cannot be used in this context.
Perhaps someone can come up with a better solution, but I can't see one 🙂

You must be logged in to vote
1 reply
@AnrDaemon
Comment options

The moment, when you envy the JavaScript world. There, it is easily possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.