feat: Add support for dynamic object and array shapes within PHPDoc comments

Đang mở
#15,662 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
backend, devtools

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
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

  1. 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
{
    // ...
}
  1. 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}
 */
  1. 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

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpstan

Tất cả issue của phpstan/phpstan

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.