Skip to content

allIsInstanceOf does not work with generics #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ruudk opened this issue Mar 3, 2025 · 1 comment
Open

allIsInstanceOf does not work with generics #187

ruudk opened this issue Mar 3, 2025 · 1 comment

Comments

@ruudk
Copy link
Contributor

ruudk commented Mar 3, 2025

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.

@herndlm
Copy link
Contributor

herndlm commented Mar 3, 2025

I might be a good idea to investigate if phpdoc on webmozarts/assert is already enough to deal with everything. you can try maybe adding a test case and removing the implementation of it here and see what happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants