Skip to content

[WIP] Use real object managers #40

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
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ php:
- 7.1
- 7.2
before_script:
- pecl install -f mongodb-stable
- composer self-update
- composer install
script:
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
"slevomat/coding-standard": "^4.5.2",
"doctrine/common": "^2.7",
"doctrine/orm": "^2.5",
"doctrine/collections": "^1.0"
"doctrine/collections": "^1.0",
"doctrine/mongodb-odm": "^1.2",
"alcaeus/mongo-php-adapter": "^1.1"
},
"conflict": {
"doctrine/common": "<2.7",
"doctrine/orm": "<2.5",
"doctrine/collections": "<1.0"
"doctrine/collections": "<1.0",
"doctrine/mongodb-odm": "<1.2"
},
"autoload": {
"psr-4": {
Expand All @@ -39,5 +42,10 @@
},
"autoload-dev": {
"classmap": ["tests/"]
},
"config": {
"platform": {
"ext-mongo": "1.6.16"
}
}
}
14 changes: 8 additions & 6 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
doctrine:
repositoryClass: Doctrine\ORM\EntityRepository
objectManagerLoader: ~

services:
-
Expand All @@ -12,20 +12,22 @@ services:
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityManagerFindDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ObjectManagerFindDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityManagerGetRepositoryDynamicReturnTypeExtension
arguments:
repositoryClass: %doctrine.repositoryClass%
class: PHPStan\Type\Doctrine\ObjectManagerGetRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectManagerMergeDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\EntityRepositoryDynamicReturnTypeExtension
class: PHPStan\Type\Doctrine\ObjectRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectMetadataResolver
arguments:
objectManagerLoader: %doctrine.objectManagerLoader%
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
excludes_analyse:
- */tests/*/data/*
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

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

public function getClass(): string
Expand Down Expand Up @@ -43,12 +43,7 @@ public function getTypeFromMethodCall(
return $mixedType;
}

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

return $type;
return TypeCombinator::addNull(new ObjectType($argType->getValue()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

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

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

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

public function getClass(): string
Expand Down Expand Up @@ -47,7 +47,14 @@ public function getTypeFromMethodCall(
return new MixedType();
}

return new EntityRepositoryType($argType->getValue(), $this->repositoryClass);
$objectName = $argType->getValue();
$repositoryClass = $this->metadataResolver->getRepositoryClass($objectName);

if ($repositoryClass === null) {
return new MixedType();
}

return new ObjectRepositoryType($objectName, $repositoryClass);
}

}
47 changes: 47 additions & 0 deletions src/Type/Doctrine/ObjectMetadataResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ODMMetadata;
use Doctrine\ORM\Mapping\ClassMetadata as ORMMetadata;
use RuntimeException;
use function file_exists;
use function is_readable;

final class ObjectMetadataResolver
{

/** @var ObjectManager */
private $objectManager;

public function __construct(string $objectManagerLoader)
{
$this->objectManager = $this->getObjectManager($objectManagerLoader);
}

private function getObjectManager(string $objectManagerLoader): ObjectManager
{
if (! file_exists($objectManagerLoader) && ! is_readable($objectManagerLoader)) {
throw new RuntimeException('Object manager could not be loaded');
}

return require $objectManagerLoader;
}

public function getRepositoryClass(string $className): ?string
{
$metatada = $this->objectManager->getClassMetadata($className);

if ($metatada instanceof ORMMetadata) {
return $metatada->customRepositoryClassName ?? 'Doctrine\ORM\EntityRepository';
}

if ($metatada instanceof ODMMetadata) {
return $metatada->customRepositoryClassName ?? 'Doctrine\ODM\MongoDB\DocumentRepository';
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

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

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

public function isMethodSupported(MethodReflection $methodReflection): bool
Expand All @@ -36,7 +36,7 @@ public function getTypeFromMethodCall(
): Type
{
$calledOnType = $scope->getType($methodCall->var);
if (!$calledOnType instanceof EntityRepositoryType) {
if (!$calledOnType instanceof ObjectRepositoryType) {
return new MixedType();
}
$methodName = $methodReflection->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\VerbosityLevel;

class EntityRepositoryType extends ObjectType
class ObjectRepositoryType extends ObjectType
{

/** @var string */
Expand Down
38 changes: 38 additions & 0 deletions tests/DoctrineIntegration/ODM/DocumentManagerIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\DoctrineIntegration\ODM;

use PHPStan\Testing\LevelsTestCase;

final class DocumentManagerIntegrationTest extends LevelsTestCase
{

/**
* @return string[][]
*/
public function dataTopics(): array
{
return [
['documentManagerDynamicReturn'],
['documentRepositoryDynamicReturn'],
['documentManagerMergeReturn'],
['customRepositoryUsage'],
];
}

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

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

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

}
59 changes: 59 additions & 0 deletions tests/DoctrineIntegration/ODM/data/customRepositoryUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\DoctrineIntegration\ODM\CustomRepositoryUsage;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use RuntimeException;

class Example
{
/**
* @var MyRepository
*/
private $repository;

public function __construct(DocumentManager $documentManager)
{
$this->repository = $documentManager->getRepository(MyDocument::class);
}

public function get(): void
{
$test = $this->repository->get('testing');
$test->doSomethingElse();
}
}

/**
* @Document(repositoryClass=MyRepository::class)
*/
class MyDocument
{
/**
* @Id(strategy="NONE", type="string")
*
* @var string
*/
private $id;

public function doSomethingElse(): void
{
}
}

class MyRepository extends DocumentRepository
{
public function get(string $id): MyDocument
{
$document = $this->find($id);

if ($document === null) {
throw new RuntimeException('Not found...');
}

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

namespace PHPStan\DoctrineIntegration\ODM\DocumentManagerDynamicReturn;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use RuntimeException;

class Example
{
/**
* @var DocumentManager
*/
private $documentManager;

public function __construct(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
}

public function findDynamicType(): void
{
$test = $this->documentManager->find(MyDocument::class, 'blah-123');

if ($test === null) {
throw new RuntimeException('Sorry, but no...');
}

$test->doSomething();
}

public function getReferenceDynamicType(): void
{
$test = $this->documentManager->getReference(MyDocument::class, 'blah-123');

if ($test === null) {
throw new RuntimeException('Sorry, but no...');
}

$test->doSomething();
}

public function getPartialReferenceDynamicType(): void
{
$test = $this->documentManager->getPartialReference(MyDocument::class, 'blah-123');

if ($test === null) {
throw new RuntimeException('Sorry, but no...');
}

$test->doSomething();
}
}

/**
* @Document()
*/
class MyDocument
{
/**
* @Id(strategy="NONE", type="string")
*
* @var string
*/
private $id;

public function doSomething(): void
{
}
}
Loading