|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\DoctrineIntegration\ORM\EntityRepositoryDynamicReturn; |
| 4 | + |
| 5 | +use Doctrine\ORM\EntityManager; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use Doctrine\ORM\EntityRepository; |
| 8 | +use Doctrine\ORM\Mapping as ORM; |
| 9 | +use RuntimeException; |
| 10 | + |
| 11 | +class Example |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var EntityRepository |
| 15 | + */ |
| 16 | + private $repository; |
| 17 | + |
| 18 | + public function __construct(EntityManagerInterface $entityManager) |
| 19 | + { |
| 20 | + $this->repository = $entityManager->getRepository(MyEntity::class); |
| 21 | + } |
| 22 | + |
| 23 | + public function findDynamicType(): void |
| 24 | + { |
| 25 | + $test = $this->repository->find(1); |
| 26 | + |
| 27 | + if ($test === null) { |
| 28 | + throw new RuntimeException('Sorry, but no...'); |
| 29 | + } |
| 30 | + |
| 31 | + $test->doSomething(); |
| 32 | + $test->doSomethingElse(); |
| 33 | + } |
| 34 | + |
| 35 | + public function findOneByDynamicType(): void |
| 36 | + { |
| 37 | + $test = $this->repository->findOneBy(['blah' => 'testing']); |
| 38 | + |
| 39 | + if ($test === null) { |
| 40 | + throw new RuntimeException('Sorry, but no...'); |
| 41 | + } |
| 42 | + |
| 43 | + $test->doSomething(); |
| 44 | + $test->doSomethingElse(); |
| 45 | + } |
| 46 | + |
| 47 | + public function findAllDynamicType(): void |
| 48 | + { |
| 49 | + $items = $this->repository->findAll(); |
| 50 | + |
| 51 | + foreach ($items as $test) { |
| 52 | + $test->doSomething(); |
| 53 | + $test->doSomethingElse(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function findByDynamicType(): void |
| 58 | + { |
| 59 | + $items = $this->repository->findBy(['blah' => 'testing']); |
| 60 | + |
| 61 | + foreach ($items as $test) { |
| 62 | + $test->doSomething(); |
| 63 | + $test->doSomethingElse(); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class Example2 |
| 69 | +{ |
| 70 | + /** |
| 71 | + * @var EntityRepository<MyEntity> |
| 72 | + */ |
| 73 | + private $repository; |
| 74 | + |
| 75 | + public function __construct(EntityManagerInterface $entityManager) |
| 76 | + { |
| 77 | + $this->repository = $entityManager->getRepository(MyEntity::class); |
| 78 | + } |
| 79 | + |
| 80 | + public function findDynamicType(): void |
| 81 | + { |
| 82 | + $test = $this->repository->find(1); |
| 83 | + |
| 84 | + if ($test === null) { |
| 85 | + throw new RuntimeException('Sorry, but no...'); |
| 86 | + } |
| 87 | + |
| 88 | + $test->doSomething(); |
| 89 | + $test->doSomethingElse(); |
| 90 | + } |
| 91 | + |
| 92 | + public function findOneByDynamicType(): void |
| 93 | + { |
| 94 | + $test = $this->repository->findOneBy(['blah' => 'testing']); |
| 95 | + |
| 96 | + if ($test === null) { |
| 97 | + throw new RuntimeException('Sorry, but no...'); |
| 98 | + } |
| 99 | + |
| 100 | + $test->doSomething(); |
| 101 | + $test->doSomethingElse(); |
| 102 | + } |
| 103 | + |
| 104 | + public function findAllDynamicType(): void |
| 105 | + { |
| 106 | + $items = $this->repository->findAll(); |
| 107 | + |
| 108 | + foreach ($items as $test) { |
| 109 | + $test->doSomething(); |
| 110 | + $test->doSomethingElse(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public function findByDynamicType(): void |
| 115 | + { |
| 116 | + $items = $this->repository->findBy(['blah' => 'testing']); |
| 117 | + |
| 118 | + foreach ($items as $test) { |
| 119 | + $test->doSomething(); |
| 120 | + $test->doSomethingElse(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +class Example3MagicMethods |
| 126 | +{ |
| 127 | + /** |
| 128 | + * @var EntityRepository<MyEntity> |
| 129 | + */ |
| 130 | + private $repository; |
| 131 | + |
| 132 | + public function __construct(EntityManagerInterface $entityManager) |
| 133 | + { |
| 134 | + $this->repository = $entityManager->getRepository(MyEntity::class); |
| 135 | + } |
| 136 | + |
| 137 | + public function findDynamicType(): void |
| 138 | + { |
| 139 | + $test = $this->repository->findById(1); |
| 140 | + foreach ($test as $item) { |
| 141 | + $item->doSomething(); |
| 142 | + $item->doSomethingElse(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public function findDynamicType2(): void |
| 147 | + { |
| 148 | + $test = $this->repository->findByNonexistent(1); |
| 149 | + } |
| 150 | + |
| 151 | + public function findOneByDynamicType(): void |
| 152 | + { |
| 153 | + $test = $this->repository->findOneById(1); |
| 154 | + |
| 155 | + if ($test === null) { |
| 156 | + throw new RuntimeException('Sorry, but no...'); |
| 157 | + } |
| 158 | + |
| 159 | + $test->doSomething(); |
| 160 | + $test->doSomethingElse(); |
| 161 | + } |
| 162 | + |
| 163 | + public function findOneDynamicType2(): void |
| 164 | + { |
| 165 | + $test = $this->repository->findOneByNonexistent(1); |
| 166 | + } |
| 167 | + |
| 168 | + public function countBy(): void |
| 169 | + { |
| 170 | + $test = $this->repository->countById(1); |
| 171 | + if ($test === 'foo') { |
| 172 | + |
| 173 | + } |
| 174 | + $test = $this->repository->countByNonexistent('test'); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * @ORM\Entity() |
| 180 | + */ |
| 181 | +class MyEntity |
| 182 | +{ |
| 183 | + /** |
| 184 | + * @ORM\Id() |
| 185 | + * @ORM\GeneratedValue() |
| 186 | + * @ORM\Column(type="integer") |
| 187 | + * |
| 188 | + * @var int |
| 189 | + */ |
| 190 | + private $id; |
| 191 | + |
| 192 | + public function doSomething(): void |
| 193 | + { |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +interface EntityInterface |
| 198 | +{ |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +class GetRepositoryOnNonClasses |
| 203 | +{ |
| 204 | + |
| 205 | + public function doFoo(EntityManagerInterface $entityManager): void |
| 206 | + { |
| 207 | + $entityManager->getRepository(EntityInterface::class); |
| 208 | + } |
| 209 | + |
| 210 | + public function doBar(EntityManagerInterface $entityManager): void |
| 211 | + { |
| 212 | + $entityManager->getRepository('nonexistentClass'); |
| 213 | + } |
| 214 | + |
| 215 | +} |
| 216 | + |
| 217 | +abstract class BaseEntity |
| 218 | +{ |
| 219 | + |
| 220 | + /** |
| 221 | + * @return EntityRepository<static> |
| 222 | + */ |
| 223 | + public function getRepository(): EntityRepository |
| 224 | + { |
| 225 | + return $this->getEntityManager()->getRepository(static::class); |
| 226 | + } |
| 227 | + |
| 228 | + abstract public function getEntityManager(): EntityManager; |
| 229 | + |
| 230 | +} |
| 231 | + |
| 232 | +class Bug180Repository extends EntityRepository |
| 233 | +{ |
| 234 | + public const ALIAS = 'o'; |
| 235 | + |
| 236 | + |
| 237 | + public function testingMethod(): int |
| 238 | + { |
| 239 | + $qb = $this->createQueryBuilder(); |
| 240 | + |
| 241 | + return (int) $qb->getQuery()->getSingleScalarResult(); |
| 242 | + } |
| 243 | +} |
0 commit comments