phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Function IDENTITY() loses null type

Open jlherren opened this issue 3 years ago • 0 comments

Given this Doctrine setup (see below for details):

  • Supplier has a nullable many-to-one to Country
  • Country has a non-nullable many-to-one to Continent

And this query:

$rows = $this->em->createQueryBuilder()
    ->select(
        'supplier.id AS supplierId',
        'IDENTITY(country.continent) AS continentId1', 
        'continent.id AS continentId2',
    )
    ->from(Supplier::class, 'supplier')
    ->leftJoin('supplier.country', 'country')
    ->leftJoin('country.continent', 'continent')
    ->getQuery()
    ->getResult();
\PHPStan\dumpType($rows);

It dumps the following type:

Dumped type: array<array{supplierId: int, continentId1: int|numeric-string, continentId2: int|null}>

The continent ID retrieved using IDENTITY(country.continent) is not nullable, but it should be nullable due to suppliers not necessarily having a country. (I'm less concerned about the numeric-string.)

Details about entities used Supplier:
use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Supplier {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    public int $id;

    /**
     * @ORM\ManyToOne(targetEntity="Country")
     * @ORM\JoinColumn(nullable=true)
     */
    public ?Country $country;
}

Country

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Country {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\ManyToOne(targetEntity="Continent")
     * @ORM\JoinColumn(nullable=false)
     */
    public Continent $continent;
}

Continent

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Continent {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string")
     */
    public string $name;
}

jlherren avatar Feb 19 '22 18:02 jlherren