psalm-plugin-doctrine icon indicating copy to clipboard operation
psalm-plugin-doctrine copied to clipboard

Add template for QueryBuilder

Open DanielBadura opened this issue 2 years ago • 5 comments

should fix #117

DanielBadura avatar Jun 17 '22 14:06 DanielBadura

See PR that reverted the same changes :) https://github.com/psalm/psalm-plugin-doctrine/pull/114

andrew-demb avatar Jun 17 '22 15:06 andrew-demb

Oh yeah, didnt saw it. Maybe we could make select make it mixed? Im not sure, just dropping this here: https://psalm.dev/r/245c5181cb

DanielBadura avatar Jun 17 '22 15:06 DanielBadura

I found these snippets:

https://psalm.dev/r/245c5181cb
<?php

/**
 * @template T of mixed
 */
class QueryBuilder
{
    /**
     * @return static<mixed>
     */
    public function select(): self
    {
        return $this;
    }

    /**
     * @return Query<T>
     */
    public function getQuery()
    {
        return new Query();
    }
}

/**
 * @template T of mixed
 */
class Query {}
class Foo {}

/** @var QueryBuilder<Foo> $builder */
$builder = new QueryBuilder();

/** @psalm-trace $query1 */
$query1 = $builder->getQuery();

/** @psalm-trace $query2 */
$query2 = $builder->select()->getQuery();
Psalm output (using commit 10ea05a):

INFO: Trace - 35:1 - $query1: Query<Foo>

INFO: Trace - 38:1 - $query2: Query<mixed>

INFO: UnusedVariable - 35:1 - $query1 is never referenced or the value is not used

INFO: UnusedVariable - 38:1 - $query2 is never referenced or the value is not used

psalm-github-bot[bot] avatar Jun 17 '22 15:06 psalm-github-bot[bot]

Oh yeah, didnt saw it. Maybe we could make select make it mixed? Im not sure, just dropping this here: https://psalm.dev/r/245c5181cb

Might be a solution the beginning of a solution, but

  • you should also add support for addSelect.
  • you also need to support
$qb = $this->createQueryBuilder('foo');
$qb->select('bar');
$result = $qb->getQuery()->getResult();

I would recommend to add some test about the issue https://github.com/psalm/psalm-plugin-doctrine/pull/114 to prove your solution will not introduce the regression again.

VincentLanglet avatar Oct 09 '22 15:10 VincentLanglet

I found these snippets:

https://psalm.dev/r/245c5181cb
<?php

/**
 * @template T of mixed
 */
class QueryBuilder
{
    /**
     * @return static<mixed>
     */
    public function select(): self
    {
        return $this;
    }

    /**
     * @return Query<T>
     */
    public function getQuery()
    {
        return new Query();
    }
}

/**
 * @template T of mixed
 */
class Query {}
class Foo {}

/** @var QueryBuilder<Foo> $builder */
$builder = new QueryBuilder();

/** @psalm-trace $query1 */
$query1 = $builder->getQuery();

/** @psalm-trace $query2 */
$query2 = $builder->select()->getQuery();
Psalm output (using commit 028ac7f):

INFO: Trace - 35:1 - $query1: Query<Foo>

INFO: Trace - 38:1 - $query2: Query<mixed>

INFO: UnusedVariable - 35:1 - $query1 is never referenced or the value is not used

INFO: UnusedVariable - 38:1 - $query2 is never referenced or the value is not used

psalm-github-bot[bot] avatar Oct 09 '22 15:10 psalm-github-bot[bot]