Skip to content

False positive for covariant generic parameter #6139

Open

Description

@marcospassos

Bug report

PHPStan complains about passing a covariant parameter to another covariant parameter used in a contravariant position (but the parameter itself is covariant, so it's perfectly valid).

Code snippet that reproduces the problem

https://phpstan.org/r/2d99fd87-09dd-40dd-9092-7fcc68d583f0

Expected output

No errors.
https://psalm.dev/r/a08d55873c

Activity

  1. added this to the Generics milestone on Feb 1, 2022
  2. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

    @marcospassos After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    +PHP 8.0 – 8.1 (1 error)
    +==========
    +
    +21: Template type T is declared as covariant, but occurs in contravariant position in parameter element of method FormatLoader::addElement().
    +
    +PHP 7.1 – 7.4 (3 errors)
    +==========
    +
    +10: Method Element::render() has invalid return type mixed.
    +10: PHPDoc tag @return with type T is not subtype of native type mixed.
     21: Template type T is declared as covariant, but occurs in contravariant position in parameter element of method FormatLoader::addElement().
    Full report

    PHP 8.0 – 8.1 (1 error)

    Line Error
    21 Template type T is declared as covariant, but occurs in contravariant position in parameter element of method FormatLoader::addElement().

    PHP 7.1 – 7.4 (3 errors)

    Line Error
    10 Method Element::render() has invalid return type mixed.
    10 PHPDoc tag @return with type T is not subtype of native type mixed.
    21 Template type T is declared as covariant, but occurs in contravariant position in parameter element of method FormatLoader::addElement().
  3. added a commit that references this issue on Mar 29, 2026
    dbee96f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions