Skip to content
Closed
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 @@ -114,6 +114,10 @@ services:
class: PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityRepositoryMatchingDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectMetadataResolver
arguments:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use Doctrine\ORM\EntityRepository;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\Type;

class EntityRepositoryMatchingDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return EntityRepository::class;
}

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

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$callerType = $scope->getType($methodCall->var);
$entityType = $callerType->getTemplateType(EntityRepository::class, 'TEntityClass');

return new IntersectionType([
new GenericObjectType('Doctrine\Common\Collections\Collection', [new IntegerType(), $entityType]),
new GenericObjectType('Doctrine\Common\Collections\Selectable', [new IntegerType(), $entityType]),
]);
}

}
6 changes: 3 additions & 3 deletions stubs/EntityManagerInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Doctrine\ORM;

use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;

interface EntityManagerInterface extends ObjectManager
{
Expand All @@ -29,7 +29,7 @@ interface EntityManagerInterface extends ObjectManager
/**
* @template T of object
* @phpstan-param class-string<T> $className
* @phpstan-return ObjectRepository<T>
* @phpstan-return EntityRepository<T>
*/
public function getRepository($className);

Expand Down
9 changes: 0 additions & 9 deletions stubs/EntityRepository.stub
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ class EntityRepository implements ObjectRepository
*/
protected function getEntityName();

/**
* @param \Doctrine\Common\Collections\Criteria $criteria
*
* @return \Doctrine\Common\Collections\Collection
*
* @psalm-return \Doctrine\Common\Collections\Collection<int, TEntityClass>
*/
public function matching(Criteria $criteria);

/**
* @param __doctrine-literal-string $alias
* @param __doctrine-literal-string|null $indexBy
Expand Down
1 change: 1 addition & 0 deletions tests/DoctrineIntegration/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/getRepository.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/isEmpty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/Collection.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/repositoryMatching.php');
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/DoctrineIntegration/data/repositoryMatching.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RepositoryMatching;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use function PHPStan\Testing\assertType;

class Foo
{

/** @var EntityManager */
private $entityManager;

public function doFoo(): void
{
$repository = $this->entityManager->getRepository(MyEntity::class);
$criteria = Criteria::create();
$results = $repository->matching($criteria);
assertType('Doctrine\Common\Collections\Collection<int, RepositoryMatching\MyEntity>&Doctrine\Common\Collections\Selectable<int, RepositoryMatching\MyEntity>', $results);
foreach ($results as $result) {
assertType(MyEntity::class, $result);
}
}

/**
* @param EntityRepository<MyEntity> $repository
*/
public function withTypedRepository(EntityRepository $repository): void
{
$criteria = Criteria::create();
$results = $repository->matching($criteria);
assertType('Doctrine\Common\Collections\Collection<int, RepositoryMatching\MyEntity>&Doctrine\Common\Collections\Selectable<int, RepositoryMatching\MyEntity>', $results);
foreach ($results as $result) {
assertType(MyEntity::class, $result);
}
}

}

class MyEntity
{

}
Loading