0

I have a abstract class User:

abstract class User

and a child of User, "Administrator":

class Administrator extends User

When i create a new Administrator and want to persist with this signature:

public function persist(Administrator $object): void
$this->repo->persist(new Administrator())

PHPSTAN is complaining:

Parameter #1 $object of method AdministratorRepository::persist() expects Administrator, User given

Why it thinks it's User where i am instantiating an new Administrator directly? I think its about Covariance and Contravariance but cant figure out how to tell PHPSTAN correctly?

I checked the https://phpstan.org/blog/whats-up-with-template-covariant but did not get a solution to work properly.

Thomas Baier
  • 434
  • 3
  • 8
  • With a code like `$this->repo->persist(new Administrator())`, PHPStan will definitely not complain about `Parameter #1 $object of method AdministratorRepository::persist() expects Administrator, User given`. Please reproduce your problem on phpstan.org/try, and ask about this in GitHub Discussions in PHPStan repository. Thanks. – Ondřej Mirtes Dec 13 '22 at 19:31
  • My return type of the abstract User was self instead of static - my fault! – Thomas Baier Dec 13 '22 at 20:29

0 Answers0