Skip to content

Covariance problem with template type with $this and static #9195

@mvorisek

Description

@mvorisek

Bug report

In #9153 (comment) I was advised to use @template instead of @template-covariant. But then I get another phpstan error:

Method MethodMock::methodName() should return MagicMethod<static(MethodMock)> but returns
MagicMethod<$this(MethodMock)>.
Template type TTargetClass on class MagicMethod is not covariant. Learn more:
[What's Up With @template-covariant?](https://phpstan.org/blog/whats-up-with-template-covariant)

I would say the phpstan error (with @template) is wrong, as $this is strictly a subtype of static.

Code snippet that reproduces the problem

https://phpstan.org/r/6897d3da-b2b5-4a31-b740-927310bd6e72 (with @template-covariant)

https://phpstan.org/r/75af0bc1-1122-4734-8b36-b8cc966c32df (with @template)

Expected output

no error

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