Skip to content

ManagerRegistry::getRepository returns correct EntityRepository #46

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 1 commit into from
Feb 12, 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
6 changes: 6 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ services:
class: PHPStan\Type\Doctrine\ObjectRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ManagerRegistryGetRepositoryDynamicReturnTypeExtension
arguments:
repositoryClass: %doctrine.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
48 changes: 48 additions & 0 deletions src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

abstract class GetRepositoryDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{

/** @var string */
private $repositoryClass;

public function __construct(string $repositoryClass)
{
$this->repositoryClass = $repositoryClass;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getRepository';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (count($methodCall->args) === 0) {
return ParametersAcceptorSelector::selectSingle(
$methodReflection->getVariants()
)->getReturnType();
}
$argType = $scope->getType($methodCall->args[0]->value);
if (!$argType instanceof ConstantStringType) {
return new MixedType();
}

return new ObjectRepositoryType($argType->getValue(), $this->repositoryClass);
}

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

namespace PHPStan\Type\Doctrine;

class ManagerRegistryGetRepositoryDynamicReturnTypeExtension extends GetRepositoryDynamicReturnTypeExtension
{

public function getClass(): string
{
return 'Doctrine\Common\Persistence\ManagerRegistry';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,12 @@

namespace PHPStan\Type\Doctrine;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

class ObjectManagerGetRepositoryDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
class ObjectManagerGetRepositoryDynamicReturnTypeExtension extends GetRepositoryDynamicReturnTypeExtension
{

/** @var string */
private $repositoryClass;

public function __construct(string $repositoryClass)
{
$this->repositoryClass = $repositoryClass;
}

public function getClass(): string
{
return 'Doctrine\Common\Persistence\ObjectManager';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getRepository';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (count($methodCall->args) === 0) {
return ParametersAcceptorSelector::selectSingle(
$methodReflection->getVariants()
)->getReturnType();
}
$argType = $scope->getType($methodCall->args[0]->value);
if (!$argType instanceof ConstantStringType) {
return new MixedType();
}

return new ObjectRepositoryType($argType->getValue(), $this->repositoryClass);
}

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

namespace PHPStan\DoctrineIntegration\Persistence;

use PHPStan\Testing\LevelsTestCase;

final class ManagerRegistryIntegrationTest extends LevelsTestCase
{

/**
* @return string[][]
*/
public function dataTopics(): array
{
return [
['managerRegistryRepositoryDynamicReturn'],
];
}

public function getDataPath(): string
{
return __DIR__ . '/data';
}

public function getPhpStanExecutablePath(): string
{
return __DIR__ . '/../../../vendor/bin/phpstan';
}

public function getPhpStanConfigPath(): ?string
{
return __DIR__ . '/phpstan.neon';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Call to an undefined method Doctrine\\ORM\\EntityRepository<PHPStan\\DoctrineIntegration\\Persistence\\ManagerRegistryRepositoryDynamicReturn\\MyEntity>::nonexistant().",
"line": 31,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace PHPStan\DoctrineIntegration\Persistence\ManagerRegistryRepositoryDynamicReturn;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use RuntimeException;

class Example
{
/**
* @var ManagerRegistry
*/
private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}

public function findDynamicType(): void
{
$test = $this->managerRegistry->getRepository(MyEntity::class)->createQueryBuilder('e');

$test->getQuery();
}

public function errorWithDynamicType(): void
{
$this->managerRegistry->getRepository(MyEntity::class)->nonexistant();
}
}

/**
* @ORM\Entity()
*/
class MyEntity
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;
}
2 changes: 2 additions & 0 deletions tests/DoctrineIntegration/Persistence/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- ../../../extension.neon