Skip to content

Commit 1f2e082

Browse files
jackbentleyondrejmirtes
authored andcommitted
Add support for magic find* entity repository methods
1 parent 212841b commit 1f2e082

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

extension.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ conditionalTags:
1111
services:
1212
-
1313
class: PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension
14+
-
15+
class: PHPStan\Reflection\Doctrine\EntityRepositoryClassReflectionExtension
16+
tags:
17+
- phpstan.broker.methodsClassReflectionExtension
1418
-
1519
class: PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension
1620
tags:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\Doctrine;
4+
5+
use PHPStan\Reflection\Dummy\DummyMethodReflection;
6+
7+
class EntityRepositoryClassReflectionExtension implements \PHPStan\Reflection\MethodsClassReflectionExtension
8+
{
9+
10+
public function hasMethod(\PHPStan\Reflection\ClassReflection $classReflection, string $methodName): bool
11+
{
12+
return $classReflection->getName() === 'Doctrine\ORM\EntityRepository'
13+
&& (strpos($methodName, 'findBy') === 0 || strpos($methodName, 'findOneBy') === 0);
14+
}
15+
16+
public function getMethod(\PHPStan\Reflection\ClassReflection $classReflection, string $methodName): \PHPStan\Reflection\MethodReflection
17+
{
18+
return new DummyMethodReflection($methodName);
19+
}
20+
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\Doctrine;
4+
5+
final class EntityRepositoryClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
6+
{
7+
8+
/** @var \PHPStan\Broker\Broker */
9+
private $broker;
10+
11+
/** @var \PHPStan\Reflection\Doctrine\EntityRepositoryClassReflectionExtension */
12+
private $extension;
13+
14+
protected function setUp(): void
15+
{
16+
$this->broker = $this->createBroker();
17+
$this->extension = new EntityRepositoryClassReflectionExtension();
18+
}
19+
20+
/**
21+
* @return mixed[]
22+
*/
23+
public function dataHasMethod(): array
24+
{
25+
return [
26+
[\Doctrine\ORM\EntityRepository::class, 'findBy', true],
27+
[\Doctrine\ORM\EntityRepository::class, 'findByString', true],
28+
[\Doctrine\ORM\EntityRepository::class, 'findOneByString', true],
29+
[\Doctrine\ORM\EntityRepository::class, 'count', false],
30+
[\Doctrine\ORM\EntityRepository::class, 'find', false],
31+
[\Doctrine\ORM\EntityRepository::class, 'findAll', false],
32+
];
33+
}
34+
35+
/**
36+
* @dataProvider dataHasMethod
37+
*
38+
* @param string $className
39+
* @param string $method
40+
* @param bool $expectedResult
41+
*/
42+
public function testHasMethod(string $className, string $method, bool $expectedResult): void
43+
{
44+
$classReflection = $this->broker->getClass($className);
45+
46+
self::assertSame($expectedResult, $this->extension->hasMethod($classReflection, $method));
47+
}
48+
49+
public function testGetMethod(): void
50+
{
51+
$methodName = 'findOneByString';
52+
53+
$classReflection = $this->broker->getClass(\Doctrine\ORM\EntityRepository::class);
54+
$methodReflection = $this->extension->getMethod($classReflection, $methodName);
55+
56+
self::assertSame($methodName, $methodReflection->getName());
57+
}
58+
59+
}

0 commit comments

Comments
 (0)