Search code examples
phpoopphpstorm

PHPStorm 9 inspection of class inheritance works unexpectable


I'm facing the following issue in PHPStorm 9:
Say I have an interface FieldInterface that has some methods:

namespace Acme;

interface FieldInterface {
    public function methodA();
    public function methodB();
}

then I have an abstract class that implements base functionality of the interface. That abstract class has the user to implement certain methods, let's say it's methodB in our example:

namespace Acme;

abstract class AbstractField implements FieldInterface {
    public function methodA() {
        // implement methodA
    }

    public abstract function methodB(); // have the user implement it
}

And finally I have some ready-to-use class StringField:

namespace Acme;

class StringField extends AbstractField {
    public function methodB() {
        // implement methodB
    }
}

At this point everything's going well. But if I add new method in the FieldInterface, PHPStorm does not say that anything is wrong with AbstractField while it's obvious that I should add public abstract function newMethod(); in there. However, it spots the error in StringField class instead.
It could be understood from the point that abstract classes are made for the purpose of extention, but usually you extend the abstract class rather than implement underlying interface. The whole meaning of making abstract class is to save user's time for implementing the interface. So why PHPStorm forces me to implement interface in concrete class rather than forcing me to implement it in abstract class that is explicitly implements the interface.
So I wonder if it is a bug in PHPStorm, or maybe it's done on purpose. Either way, is there any workaround?


Solution

  • That's how it should be, showing an error in the abstract class would be wrong.

    In fact, public abstract function methodB(); is redundant because the abstract class already "inherits" this abstract method from the interface as it does not implement it.

    The only workaround is to make AbstractField not abstract.