feat: Add support for dynamic object and array shapes within PHPDoc comments
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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!
- Ngôn ngữ chính
- PHP
- Star
- 14.2k
- Fork
- 972
- Merge trung bình
- 13 giờ 25 phút
- Pull request đã merge (30 ngày)
- 142
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của phpstan/phpstan
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Variable in declare() {} block seems not to be considered usedCó thể đã có người làm @phpstan-bot đã nhận 32 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
phpstan/phpstan#15394 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
parallel.loadLimit never reads the system load averageCó thể đã có người làm @midhunmonachan đã nhận 60 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
phpstan/phpstan#15241 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
phpstan/phpstan#14597 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của phpstan/phpstan
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
phpstan/phpstan-phpunit#361 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
HE-Arc/le-panier-du-coin#49 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 2 ngày
-
15.0
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·