Skip to content

Can't return ReflectionClass<object> on PHP 8.4 ("Template type T on class ReflectionClass is not covariant") #12459

@lkrms

Description

@lkrms

Bug report

Given this only surfaces on PHPStan 2.1.2, and only on PHP 8.4, I'm guessing it's a non-obvious side-effect of #12435, but I didn't dig into it too deeply.

At any rate, I'm seeing this on all of my PHPUnit tests that return ReflectionClass, and I don't think the way they're annotated is problematic?

Code for convenience:

<?php declare(strict_types=1);

class MyTest
{
    /**
     * @return array<array{ReflectionClass<object>}>
     */
    public static function myProvider(): array
    {
        return [
            [
                new ReflectionClass(MyTest::class),
            ],
        ];
    }
}

Code snippet that reproduces the problem

https://phpstan.org/r/a3a788e2-e5dd-4d7d-997f-90cd344dc695

Expected output

No errors should be reported here.

Did PHPStan help you today? Did it make you happy in any way?

No response

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