feat: Add support for dynamic object and array shapes within PHPDoc comments
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 25/100
Direção de pesquisa
Start by reading PHPStan's type parser and existing tests for object and array shapes; the issue does not name specific files or tests. Then trace how PHPDoc shape types are parsed and resolved to assess how the proposed mapped syntax would fit. Done means supporting the examples in the issue with tests for iterable sources and type unions.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Feature request
I would like to propose a new syntax for creating dynamic object and array shapes natively within PHPDoc comments.
Currently, it is really hard to transform generic parameter collections, variadics, or shape templates into a dynamic, structured object{} or array{} shape without writing custom DynamicMethodReturnTypeExtension classes in PHP. I propose introducing a native structural mapped type syntax inside both object and array shapes that can iterate over any iterable type or type union.
Proposed Syntax
By combining index signatures with a colon constraint syntax, we can declare inline mapped shapes inside docblocks without introducing new reserved keywords to the lexer. The constraint can accept any iterable source type:
object{[K: IterableSourceType]: ValueType}
array{[K: IterableSourceType]: ValueType}
This syntax sets up a clear, symmetrical parser boundary where the engine unrolls the keys based on the provided iterable type:
array{ [ K : IterableSourceType ] : ValueType }
└───┬───────────────────┘ │
│ └─ Value mapping assignment (TypeNode)
└─ Key iterator and constraint collection source
Type Examples
- Creating a Dynamic Array Shape via key-of:
/**
* @template T of array<string, mixed>
*
* @param T $data
* @return array{[K: key-of<T>]: T[K]}
*/
function foo(array $data): array
{
// ...
}
- Iterating a Type Union (Enum-like behavior for Arrays):
/**
* @return array{[K: 'status'|'code'|'message']: string}
* // Resolves to: array{status: string, code: string, message: string}
*/
- Unrolling Variadic Generics into an Object Shape:
/**
* @template TColName of string
* @template TColVal
* @param array<int, Column<TColName, TColVal>> $columns
* @return object{[K: TColName]: TColVal}
*/
Benefits & Use Cases
This feature removes the boilerplate of writing framework-specific extensions for highly dynamic PHP architectures. Real-world applications include:
- Fluent APIs & Query Builders: Unrolling variadic arguments (like a list of custom configuration or column objects) into a strongly-typed result payload array or object shape.
- Data Mappers & Hydrators: Safely transforming array shapes or dynamic dictionary structures into matching structural object/array shapes during execution.
- DTO & Array Transformations: Defining generic wrapper components that dynamically pass down, pluck, filter, or merge array key properties through strict type inference.
Did PHPStan help you today? Did it make you happy in any way?
I am really happy with PHPStan, I use it in almost all of my projects, and I would be really happy if this new feature lands in!
- Linguagem predominante
- PHP
- Estrelas
- 14.2k
- Forks
- 972
- Merge médio
- 13h 25min
- PRs com merge (30d)
- 142
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de phpstan/phpstan
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 63/100
Mantenedores costumam responder em até 1 dia
-
Variable in declare() {} block seems not to be considered usedTalvez já em andamento @phpstan-bot assumiu há 32 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
phpstan/phpstan#15394 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
parallel.loadLimit never reads the system load averageTalvez já em andamento @midhunmonachan assumiu há 60 dias. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
phpstan/phpstan#15241 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
phpstan/phpstan#14597 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
Todas as issues de phpstan/phpstan
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 63/100
phpstan/phpstan-phpunit#361 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
HE-Arc/le-panier-du-coin#49 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
Mantenedores costumam responder em até 2 dias
-
15.0
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·