|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace PHPStan\DoctrineIntegration\ORM\DbalQueryBuilderExecuteDynamicReturn; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Connection; |
| 6 | +use Doctrine\DBAL\Query\QueryBuilder; |
| 7 | + |
| 8 | +class Example |
| 9 | +{ |
| 10 | + /** @var Connection */ |
| 11 | + private $connection; |
| 12 | + |
| 13 | + public function __construct(Connection $connection) |
| 14 | + { |
| 15 | + $this->connection = $connection; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @return mixed[] |
| 20 | + */ |
| 21 | + public function testCaseOne(int $userId): array |
| 22 | + { |
| 23 | + return $this->connection->createQueryBuilder() |
| 24 | + ->select('*') |
| 25 | + ->from('user') |
| 26 | + ->where('user.id = :id') |
| 27 | + ->setParameter('id', $userId) |
| 28 | + ->execute() |
| 29 | + ->fetchAll(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return mixed[] |
| 34 | + */ |
| 35 | + public function testCaseTwo(int $userId): array |
| 36 | + { |
| 37 | + $qb = $this->connection->createQueryBuilder(); |
| 38 | + $qb->select('*'); |
| 39 | + $qb->from('user'); |
| 40 | + $qb->where('user.id = :id'); |
| 41 | + $qb->setParameter('id', $userId); |
| 42 | + |
| 43 | + return $qb->execute()->fetchAll(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return mixed[] |
| 48 | + */ |
| 49 | + public function testCaseThree(?int $userId = null): array |
| 50 | + { |
| 51 | + $qb = $this->connection->createQueryBuilder(); |
| 52 | + $qb->select('*'); |
| 53 | + $qb->from('user'); |
| 54 | + |
| 55 | + if ($userId !== null) { |
| 56 | + $qb->where('user.id = :id'); |
| 57 | + $qb->setParameter('id', $userId); |
| 58 | + } |
| 59 | + |
| 60 | + return $qb->execute()->fetchAll(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return mixed[] |
| 65 | + */ |
| 66 | + public function testCaseFourA(?int $userId = null): array |
| 67 | + { |
| 68 | + $qb = $this->connection->createQueryBuilder(); |
| 69 | + $qb->select('*'); |
| 70 | + $qb->from('user'); |
| 71 | + |
| 72 | + if ($userId !== null) { |
| 73 | + return $this->testCaseFourB($qb, $userId); |
| 74 | + } |
| 75 | + |
| 76 | + return $qb->execute()->fetchAll(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return mixed[] |
| 81 | + */ |
| 82 | + private function testCaseFourB(QueryBuilder $qb, int $userId): array |
| 83 | + { |
| 84 | + $qb->where('user.id = :id'); |
| 85 | + $qb->setParameter('id', $userId); |
| 86 | + |
| 87 | + return $qb->execute()->fetchAll(); |
| 88 | + } |
| 89 | +} |
0 commit comments