Skip to content

[WIP] Support Doctrine MongoDB ODM #36

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

Closed
wants to merge 3 commits into from
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
85 changes: 79 additions & 6 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
parameters:
doctrine:
repositoryClass: Doctrine\ORM\EntityRepository
odm:
mongodb:
registryClass: Doctrine\Bundle\MongoDBBundle\ManagerRegistry
managerClass: Doctrine\ODM\MongoDB\DocumentManager
repositoryClass: Doctrine\ODM\MongoDB\DocumentRepository
orm:
registryClass: Doctrine\Bundle\DoctrineBundle\Registry
managerClass: Doctrine\ORM\EntityManagerInterface
repositoryClass: Doctrine\ORM\EntityRepository
persistence:
registryClass: Doctrine\Common\Persistence\ManagerRegistry
managerClass: Doctrine\Common\Persistence\ObjectManager
repositoryClass: Doctrine\Common\Persistence\ObjectRepository

services:
-
Expand All @@ -11,21 +23,82 @@ services:
class: PHPStan\Type\Doctrine\DoctrineSelectableDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Doctrine\ObjectManagerFindDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectManagerMergeDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityManagerFindDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ObjectRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Doctrine\EntityManagerGetRepositoryDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ObjectManagerGetRepositoryDynamicReturnTypeExtension
arguments:
repositoryClass: %doctrine.repositoryClass%
managerClass: %doctrine.persistence.managerClass%
repositoryClass: %doctrine.persistence.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectManagerMergeDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ObjectManagerGetRepositoryDynamicReturnTypeExtension
arguments:
managerClass: %doctrine.odm.mongodb.managerClass%
repositoryClass: %doctrine.odm.mongodb.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectManagerGetRepositoryDynamicReturnTypeExtension
arguments:
managerClass: %doctrine.orm.managerClass%
repositoryClass: %doctrine.orm.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Doctrine\ManagerRegistryGetManagerDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.persistence.registryClass%
managerClass: %doctrine.persistence.managerClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ManagerRegistryGetManagerDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.odm.mongodb.registryClass%
managerClass: %doctrine.odm.mongodb.managerClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityRepositoryDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ManagerRegistryGetManagerDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.orm.registryClass%
managerClass: %doctrine.orm.managerClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Doctrine\ManagerRegistryGetRepositoryDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.persistence.registryClass%
repositoryClass: %doctrine.persistence.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ManagerRegistryGetRepositoryDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.odm.mongodb.registryClass%
repositoryClass: %doctrine.odm.mongodb.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ManagerRegistryGetRepositoryDynamicReturnTypeExtension
arguments:
registryClass: %doctrine.orm.registryClass%
repositoryClass: %doctrine.orm.repositoryClass%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
55 changes: 12 additions & 43 deletions src/Type/Doctrine/EntityManagerFindDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,22 @@

namespace PHPStan\Type\Doctrine;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use const E_USER_DEPRECATED;
use function trigger_error;

class EntityManagerFindDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
class EntityManagerFindDynamicReturnTypeExtension extends ObjectManagerFindDynamicReturnTypeExtension
{

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

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), [
'find',
'getReference',
'getPartialReference',
], true);
}

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

$type = new ObjectType($argType->getValue());
if ($methodReflection->getName() === 'find') {
$type = TypeCombinator::addNull($type);
}

return $type;
@trigger_error(
sprintf(
'Class %s is deprecated and will be removed. Use %s instead',
self::class,
ObjectManagerFindDynamicReturnTypeExtension::class
),
E_USER_DEPRECATED
);
}

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

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;
use const E_USER_DEPRECATED;
use function trigger_error;

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

/** @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 EntityRepositoryType($argType->getValue(), $this->repositoryClass);
@trigger_error(
sprintf(
'Class %s is deprecated and will be removed. Use %s instead',
self::class,
ObjectManagerGetRepositoryDynamicReturnTypeExtension::class
),
E_USER_DEPRECATED
);

parent::__construct('Doctrine\Common\Persistence\ObjectManager', $repositoryClass);
}

}
53 changes: 12 additions & 41 deletions src/Type/Doctrine/EntityRepositoryDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,22 @@

namespace PHPStan\Type\Doctrine;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use const E_USER_DEPRECATED;
use function trigger_error;

class EntityRepositoryDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
class EntityRepositoryDynamicReturnTypeExtension extends ObjectRepositoryDynamicReturnTypeExtension
{

public function getClass(): string
public function __construct()
{
return 'Doctrine\ORM\EntityRepository';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
$methodName = $methodReflection->getName();
return strpos($methodName, 'findBy') === 0
|| strpos($methodName, 'findOneBy') === 0
|| $methodName === 'findAll'
|| $methodName === 'find';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
$calledOnType = $scope->getType($methodCall->var);
if (!$calledOnType instanceof EntityRepositoryType) {
return new MixedType();
}
$methodName = $methodReflection->getName();
$entityType = new ObjectType($calledOnType->getEntityClass());

if ($methodName === 'find' || strpos($methodName, 'findOneBy') === 0) {
return TypeCombinator::addNull($entityType);
}

return new ArrayType(new IntegerType(), $entityType);
@trigger_error(
sprintf(
'Class %s is deprecated and will be removed. Use %s instead',
self::class,
ObjectRepositoryDynamicReturnTypeExtension::class
),
E_USER_DEPRECATED
);
}

}
31 changes: 13 additions & 18 deletions src/Type/Doctrine/EntityRepositoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

namespace PHPStan\Type\Doctrine;

use PHPStan\Type\ObjectType;
use PHPStan\Type\VerbosityLevel;
use const E_USER_DEPRECATED;
use function trigger_error;

class EntityRepositoryType extends ObjectType
class EntityRepositoryType extends ObjectRepositoryType
{

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

public function __construct(string $entityClass, string $repositoryClass)
{
parent::__construct($repositoryClass);
$this->entityClass = $entityClass;
}

public function getEntityClass(): string
{
return $this->entityClass;
}

public function describe(VerbosityLevel $level): string
{
return sprintf('%s<%s>', parent::describe($level), $this->entityClass);
@trigger_error(
sprintf(
'Class %s is deprecated and will be removed. Use %s instead',
self::class,
ObjectRepositoryType::class
),
E_USER_DEPRECATED
);

parent::__construct($entityClass, $repositoryClass);
}

}
Loading