Skip to content

Add support for magic find* entity repository methods #35 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ conditionalTags:
services:
-
class: PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension
-
class: PHPStan\Reflection\Doctrine\EntityRepositoryClassReflectionExtension
tags:
- phpstan.broker.methodsClassReflectionExtension
-
class: PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Doctrine;

use PHPStan\Reflection\Dummy\DummyMethodReflection;

class EntityRepositoryClassReflectionExtension implements \PHPStan\Reflection\MethodsClassReflectionExtension
{

public function hasMethod(\PHPStan\Reflection\ClassReflection $classReflection, string $methodName): bool
{
return $classReflection->getName() === 'Doctrine\ORM\EntityRepository'
&& (strpos($methodName, 'findBy') === 0 || strpos($methodName, 'findOneBy') === 0);
}

public function getMethod(\PHPStan\Reflection\ClassReflection $classReflection, string $methodName): \PHPStan\Reflection\MethodReflection
{
return new DummyMethodReflection($methodName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\Doctrine;

final class EntityRepositoryClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
{

/** @var \PHPStan\Broker\Broker */
private $broker;

/** @var \PHPStan\Reflection\Doctrine\EntityRepositoryClassReflectionExtension */
private $extension;

protected function setUp(): void
{
$this->broker = $this->createBroker();
$this->extension = new EntityRepositoryClassReflectionExtension();
}

/**
* @return mixed[]
*/
public function dataHasMethod(): array
{
return [
[\Doctrine\ORM\EntityRepository::class, 'findBy', true],
[\Doctrine\ORM\EntityRepository::class, 'findByString', true],
[\Doctrine\ORM\EntityRepository::class, 'findOneByString', true],
[\Doctrine\ORM\EntityRepository::class, 'count', false],
[\Doctrine\ORM\EntityRepository::class, 'find', false],
[\Doctrine\ORM\EntityRepository::class, 'findAll', false],
];
}

/**
* @dataProvider dataHasMethod
*
* @param string $className
* @param string $method
* @param bool $expectedResult
*/
public function testHasMethod(string $className, string $method, bool $expectedResult): void
{
$classReflection = $this->broker->getClass($className);

self::assertSame($expectedResult, $this->extension->hasMethod($classReflection, $method));
}

public function testGetMethod(): void
{
$methodName = 'findOneByString';

$classReflection = $this->broker->getClass(\Doctrine\ORM\EntityRepository::class);
$methodReflection = $this->extension->getMethod($classReflection, $methodName);

self::assertSame($methodName, $methodReflection->getName());
}

}