Skip to content

Should this 'Parameter of method SubClass::__construct() is not contravariant with parameter of method ParentClass::__construct()' error be unignorable? #12272

Closed

Description

@wvega

Bug report

The code from https://phpstan.org/r/28422159-3b33-499a-9c14-0856c73bf57b generates the following error:

Parameter #2 $code (int) of method SpecificException::__construct() is not contravariant with parameter #2 $previous (Throwable|null) of method BaseException::__construct().

and when I try to add a similar error to the baseline on a project, PHPStan reports that: Unignorable errors could not be added to the baseline.

The code in question could probably have a better structure, but it doesn't generate any PHP warnings or errors: https://3v4l.org/abZFe, so I'm wondering whether that PHPStan error should be marked as unignorable or reported at all. This old issue suggests to me that doing this type of check on the constructor methods may be an accident.

Note that when I remove the ExceptionContract interface from the example, PHPStan no longer detects an error: https://phpstan.org/r/baca06c9-6103-445b-92ec-a0c945ab6d62

Code snippet that reproduces the problem

https://phpstan.org/r/28422159-3b33-499a-9c14-0856c73bf57b

Expected output

I think PHPStan should either allow the error to be ignored or don't report an error.

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

I rely on PHPStan every day. It makes code reviews much easier because I can focus on the solution approach and business needs knowing that PHPStan is checking our code for other problems.

Activity

  1. ondrejmirtes commented on Dec 16, 2024

    @ondrejmirtes
    Member

    This is a legit bug which should be fixed.

  2. added this to the Easy fixes milestone on Dec 16, 2024
  3. changed the title [-]Should this 'Parameter SubClass::__construct() is not contravariant with parameter ParentClass::__construct()' error be unignorable?[/-] [+]Should this 'Parameter of method SubClass::__construct() is not contravariant with parameter of method ParentClass::__construct()' error be unignorable?[/+] on Dec 16, 2024
  4. stof commented on Dec 21, 2024

    @stof
    Contributor

    @ondrejmirtes this code is perfectly valid in PHP: https://3v4l.org/LFC0S
    A constructor does not need to be contravariant with a parent constructor. Only interfaces enforces some constructor signature for child classes, but SpecificException is contravariant with the signature of the interface it implements.

  5. ondrejmirtes commented on Dec 21, 2024

    @ondrejmirtes
    Member

    Yes, I already stated this is a PHPStan bug that should be fixed. Otherwise I'd close this issue.

  6. phpstan-bot commented on Feb 26, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#4945

  7. github-actions commented on Mar 30, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  8. locked as resolved and limited conversation to collaborators on Mar 30, 2026
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