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 givenProperty 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,