Skip to content

Unable to annotate element type on SplObjectStorage #1838

@morozov

Description

@morozov

Please see https://phpstan.org/r/4670a61f-e909-47cc-84aa-e3b96c4d7f6f.

I'd like to be able to iterate over a SplObjectStorage instance and expect all elements of the collection to be of a certain type by specifying this one place, the property declaration. However, in this case assignment to a property annotated as /** @var SplObjectStorage|T[] */ is detected by PHPStan as a type violation.

Other than specifying the element type during iteration like the following, is there a better way to do that?

/** @var T[] $collection */
foreach ($collection as $item) {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions