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

Invalid class-string<T> where T as A|B #8582

Closed
alex-dev opened this issue Oct 13, 2022 · 4 comments · Fixed by #10194
Closed

Invalid class-string<T> where T as A|B #8582

alex-dev opened this issue Oct 13, 2022 · 4 comments · Fixed by #10194

Comments

@alex-dev
Copy link

https://psalm.dev/r/41dd38b32b

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/41dd38b32b
<?php

class A {}
class B {}

/**
 * @template T as A|B
 *
 * @param class-string<T> $_class
 */
function test(string $_class): void {}
Psalm output (using commit f8d1dd8):

ERROR: InvalidDocblock - 9:11 - Invalid templated classname 'A|B' in docblock for test

@orklah
Copy link
Collaborator

orklah commented Oct 13, 2022

IIRC, Psalm doesn't support class-string<A|B> so it makes sense templates wouldn't work either like this

@MidnightDesign
Copy link
Contributor

MidnightDesign commented Sep 11, 2023

IIRC, Psalm doesn't support class-string<A|B> so it makes sense templates wouldn't work either like this

Maybe this has changed since your comment, but currently it does work.

Also, here's a reproduction case that might be able to prove the usefulness of this feature: Psalm playground link / PHPStan playground link

Edit: changed the links

@psalm-github-bot
Copy link

psalm-github-bot bot commented Sep 11, 2023

I found these snippets:

https://psalm.dev/r/369cccaf3b
<?php

class A {}
class B {}

/**
 * @param class-string<A|B> $_class
 */
function test(string $_class): void {}
Psalm output (using commit 37cc4fd):

No issues!
https://psalm.dev/r/f9eb38c536
<?php

final class Add {}
final class Remove {}
final class Modify {}

class ChangeUtil
{
    /**
     * @template T of Add | Remove | Modify
     * @param iterable<mixed, T> $changes
     * @param class-string<T> $class
     * @return list<T>
     */
	public static function changeByType(iterable $changes, string $class): array
    {
        $filtered = [];
        foreach ($changes as $change) {
            if (!$change instanceof $class) {
                continue;
            }
            $filtered[] = $change;
        }
        return $filtered;
    }
}
Psalm output (using commit 37cc4fd):

ERROR: InvalidDocblock - 12:15 - Invalid templated classname 'Add|Modify|Remove' in docblock for ChangeUtil::changeByType

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

Successfully merging a pull request may close this issue.

3 participants