Skip to content
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

Bogus (?) InvalidArgument for class-string<Name> if Name is templated #11205

Open
sad-spirit opened this issue Jan 24, 2025 · 1 comment
Open

Comments

@sad-spirit
Copy link
Contributor

The docs say

You can also parameterize class-string with an object name e.g. class-string. This tells Psalm that any matching type must either be a class string of Foo or one of its descendants.

However, if the class has template defined a strange error if emitted when passing a child class to method with such a parameter

https://psalm.dev/r/41335c8656

Copy link

I found these snippets:

https://psalm.dev/r/41335c8656
<?php

class Foo
{
}

class FooChild extends Foo
{
}

/**
 * @template Item of Foo
 */
class Bar
{
}

/**
 * @extends Bar<FooChild>
 */
class BarFooChild extends Bar
{
}

/**
 * @param class-string<Bar> $className
 */
function takesClassName(string $className): void
{
    echo $className;
}

/**
 * @template T of Foo
 * @param class-string<Bar<T>> $className
 */
function takesClassNameWithTemplate(string $className): void
{
    echo $className;
}

takesClassName(BarFooChild::class);
takesClassNameWithTemplate(BarFooChild::class);
Psalm output (using commit 765dcbf):

ERROR: InvalidArgument - 42:16 - Argument 1 of takesClassName expects class-string<Bar<Foo>>, but BarFooChild::class provided

sad-spirit added a commit to sad-spirit/pg-wrapper that referenced this issue Jan 24, 2025
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

1 participant