Skip to content

allIsInstanceOf does not work with generics #187

Description

@ruudk

I noticed an interesting behavior in this extension.

With the following code:

class AssertReturn {
  /**
     * @template TClass of object
     * @param class-string<TClass> $class
     *
     * @throws InvalidArgumentException
     * @return list<TClass>
     */
    public static function allInstancesOf(mixed $list, string $class) : array
    {
        Assert::isList($list);
        dumpType($list); // Dumped type: list<mixed>
        Assert::allIsInstanceOf($list, $class);
        dumpType($list); // Dumped type: list<object>

        return $list;
    }
}

This is not what I was expecting or hoping for.

When using the Debugger I notice this:

Image

So it's related to the fact that I use generics to pass the TClass.

What's the best way to deal to mitigate this problem, if it's possible.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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