psalm-plugin-doctrine
psalm-plugin-doctrine copied to clipboard
Add template for QueryBuilder
should fix #117
See PR that reverted the same changes :) https://github.com/psalm/psalm-plugin-doctrine/pull/114
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
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
Oh yeah, didnt saw it. Maybe we could make
select
make itmixed
? 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.
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