phpstan / phpstan-doctrine

Doctrine extensions for PHPStan
MIT License
594 stars 97 forks source link

Property type mapping mismatch: database can contain TestAbstract but property expects T of TestAbstract #247

Open sylfabre opened 2 years ago

sylfabre commented 2 years ago

Hello

I have this error that I can't figure out if it is a bug or expected (and I don't know why)

Property PspAbstractBankAccount::$bankAccount type mapping mismatch: database can contain AbstractBankAccount but property expects T of TestAbstract.

I have this code


<?php

declare(strict_types=1);

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table()
 */
class AbstractBankAccount
{
    /**
     * @ORM\Id
     * @ORM\Column
     * @ORM\GeneratedValue
     */
    protected int $id;
}

/**
 * @template T of AbstractBankAccount
 *
 * @ORM\Entity()
 * @ORM\Table()
 */
class PspAbstractBankAccount
{
    /**
     * @ORM\Id
     * @ORM\Column
     * @ORM\GeneratedValue
     */
    protected int $id;

    /**
     * @var T
     * @ORM\ManyToOne(targetEntity="AbstractBankAccount")
     * @ORM\JoinColumn(nullable=false)
     */
    protected AbstractBankAccount $bankAccount;

    /**
     * @return T
     */
    public function getBankAccount(): AbstractBankAccount
    {
        return $this->bankAccount;
    }

    /**
     * @param T $bankAccount
     */
    public function __construct(AbstractBankAccount $bankAccount) {
        $this->bankAccount = $bankAccount;
    }
}

This is a simplified version of my actual code: both classes are abstract and used with SINGLE_TABLE Doctrine inheritance. My goal is to "tell" phpstan & my IDE that implementations of PspAbstractBankAccount will work with a related implementation of AbstractBankAccount: for instance, the getBankAccount() method of PspEuropeanBankAccount extends PspAbstractBankAccount will return an EuropeanBankAccount extends AbstractBankAccount.

My phpstan setup

$ composer show phpstan/*
phpstan/phpstan                  1.4.0 PHPStan - PHP Static Analysis Tool
phpstan/phpstan-doctrine         1.0.4 Doctrine extensions for PHPStan
phpstan/phpstan-phpunit          1.0.0 PHPUnit extensions and rules for PHPStan
phpstan/phpstan-strict-rules     1.1.0 Extra strict and opinionated rules for PHPStan
phpstan/phpstan-symfony          1.1.1 Symfony Framework extensions and rules for PHPStan
phpstan/phpstan-webmozart-assert 1.0.7 PHPStan webmozart/assert extension

Both commands were run before executing phpstan

$ php bin/console doctrine:cache:clear-metadata
$ php bin/console cache:clear

Thank you for your help!

ksn135 commented 1 year ago

:+1: