0

I have trouble passing PHPStan tests because of parent/child classes (besides, code runs well). These two kind of errors always show up:

  • Parameter #1 $a of method xxx expects ConcreteChildClass, AbstractParentClass given
  • Property ClassName::$a (ConcreteChildClass) does not accept AbstractParentClass

Here is the code (must be compatible with PHP 5.6):

<?php

abstract class AbstractClassA
{
    /**
     * @return static
     */
    abstract public function postProcess();
}

class ConcreteClassA extends AbstractClassA
{
    /**
     * @return ConcreteClassA
     */
    public function postProcess()
    {
        return $this;
    }
}

abstract class AbstractClassB
{
    /**
     * @return AbstractClassA
     */
    abstract public function deserialize();

    /**
     * @return AbstractClassA
     */
    public function someMethodA($params)
    {
        return $this->someMethodB();
    }

    /**
     * @return AbstractClassA
     */
    public function someMethodB()
    {
        $object = $this->deserialize();

        return $object->postProcess();
    }
}

class ConcreteClassB extends AbstractClassB
{
    /**
     * @return ConcreteClassA
     */
    public function deserialize()
    {
        $object = new ConcreteClassA();
        // some assignements

        return $object;
    }
}

And how this code is used:


class SomeClass
{
    /** @var ConcreteClassA $propertyA */
    public $propertyA;

    /** @var ConcreteClassB $propertyB */
    public $propertyB;

    public function someMethod()
    {
        $this->propertyA = $this->propertyB->someMethodA($params);
        $this->someProperty->someOtherMethod($this->propertyA);
        // again, someOtherMethod expects ConcreteClassA but AbstractClassA given...
    }

I tried to "play" with @template without success...

Please also note that I have limited control over abstract classes.

Thanks,

2
  • You’ll have better chance by getting answers if you ask in PHPStan’s GitHub discussions and also provide a phpstan.org/try reproduction so that we can play with the code sample 😊 Commented Jul 13, 2022 at 20:13
  • Thanks @OndřejMirtes I'll do that (didn't notice discussions were enabled on the repo 😊) Commented Jul 13, 2022 at 21:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.