False positive: PHP S1185 when input parameters have different type hints

Inspection S1185 for PHP (Overriding methods should do more than simply call the same method in super class) generates false positive when the subclass has different type hints.

Extra PHP detail: A __construct() is even allowed to have a completely different signature.

Example code:

class A
{
    public function __construct(FooInterface $arg1)
    {
        // init..
    }
}

class DefaultA extends A
{
    public function __construct(FooConcrete $arg1)
    {
        parent::__construct($arg1);
    }
}

In the given example, the constructor gets another type hinted arg. This is allowed and even recommended code in most popular frameworks! For some DI autowiring frameworks (for instance, Laravel), it is The Way to automatically get the proper dependency injected.

So with regards to this rule:

  • For __construct(), a type hint may change and that’s sufficient reason to overload the method (given example)
  • For __construct(), it’s even true that the amount of arguments may even change AND that the type hints don’t need to follow the liskov substitution principle (i.e. don’t even need to be the type or a wider type).
  • For any other method, replacing a type hint with a broader type hint is allowed (liskov substitution principle).

Hi @talkinnl ,
Welcome to the community and thanks for this report!

You are correct, this is a false positive and we should not raise an issue. It seems we are not correctly handling type declarations in this case. I’ve created a ticket to fix this issue, which will be tackled in the next development iteration of the PHP Analyzer.

Thanks for the contribution,
Jonas

2 Likes