Skip to content

PHPStan extension for TypedDataCollection generics #6

Description

@yuriizee

Motivation

TypedDataCollection carries its item type via the #[DataCollection(UserData::class)] attribute, and IDEs infer it — but PHPStan/Psalm at strict levels can't, so users with static analysis see mixed (or false positives) on ->first(), ->filter() callbacks, etc. That's a common deal-breaker when evaluating a DTO package.

Proposal

A phpstan-extension (separate package or extension.neon shipped here) that:

  1. Reads the #[DataCollection] attribute on the property and types the collection as TypedDataCollection<UserData>
  2. Types BaseData::from() / tryFrom() / fromLazy() returns as static (if generics on the base class don't already cover it)
  3. Types lazyCollection() item type the same way

A Psalm plugin can follow the same shape later.

Where to look

  • src/Attributes/DataCollection.php, src/TypedDataCollection.php
  • Prior art: phpstan extensions of other collection libraries (dynamic return type extensions + property type extensions)

Happy to mentor this one — comment here if you want to pick it up and we'll agree on the scope first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions