Skip to content

Commit a497a38

Browse files
committed
Compatibility with doctrine/persistence 1.3.0
1 parent 8151e1a commit a497a38

File tree

7 files changed

+148
-3
lines changed

7 files changed

+148
-3
lines changed

extension.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ parameters:
2020
- stubs/ObjectManager.php
2121
- stubs/ObjectManagerDecorator.php
2222
- stubs/ObjectRepository.php
23+
- stubs/Persistence/ManagerRegistry.php
24+
- stubs/Persistence/ObjectManager.php
25+
- stubs/Persistence/ObjectManagerDecorator.php
26+
- stubs/Persistence/ObjectRepository.php
2327

2428
parametersSchema:
2529
doctrine: structure([
@@ -120,6 +124,20 @@ services:
120124
arguments:
121125
managerClass: Doctrine\Common\Persistence\ObjectManager
122126

127+
persistenceManagerRegistryGetRepository:
128+
class: PHPStan\Type\Doctrine\GetRepositoryDynamicReturnTypeExtension
129+
tags:
130+
- phpstan.broker.dynamicMethodReturnTypeExtension
131+
arguments:
132+
managerClass: Doctrine\Persistence\ManagerRegistry
133+
134+
persistenceObjectManagerGetRepository:
135+
class: PHPStan\Type\Doctrine\GetRepositoryDynamicReturnTypeExtension
136+
tags:
137+
- phpstan.broker.dynamicMethodReturnTypeExtension
138+
arguments:
139+
managerClass: Doctrine\Persistence\ObjectManager
140+
123141
## NewExprDynamicReturnTypeExtensions
124142

125143
-

src/Reflection/Doctrine/EntityRepositoryClassReflectionExtension.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public function hasMethod(\PHPStan\Reflection\ClassReflection $classReflection,
4444

4545
$repositoryAncesor = $classReflection->getAncestorWithClassName(ObjectRepository::class);
4646
if ($repositoryAncesor === null) {
47-
return false;
47+
$repositoryAncesor = $classReflection->getAncestorWithClassName(\Doctrine\Persistence\ObjectRepository::class);
48+
if ($repositoryAncesor === null) {
49+
return false;
50+
}
4851
}
4952

5053
$templateTypeMap = $repositoryAncesor->getActiveTemplateTypeMap();
@@ -77,7 +80,10 @@ public function getMethod(\PHPStan\Reflection\ClassReflection $classReflection,
7780
{
7881
$repositoryAncesor = $classReflection->getAncestorWithClassName(ObjectRepository::class);
7982
if ($repositoryAncesor === null) {
80-
throw new \PHPStan\ShouldNotHappenException();
83+
$repositoryAncesor = $classReflection->getAncestorWithClassName(\Doctrine\Persistence\ObjectRepository::class);
84+
if ($repositoryAncesor === null) {
85+
throw new \PHPStan\ShouldNotHappenException();
86+
}
8187
}
8288

8389
$templateTypeMap = $repositoryAncesor->getActiveTemplateTypeMap();

src/Rules/Doctrine/ORM/RepositoryMethodCallRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public function processNode(Node $node, Scope $scope): array
5050
}
5151
$entityClassType = GenericTypeVariableResolver::getType($calledOnType, ObjectRepository::class, 'TEntityClass');
5252
if ($entityClassType === null) {
53-
return [];
53+
$entityClassType = GenericTypeVariableResolver::getType($calledOnType, \Doctrine\Persistence\ObjectRepository::class, 'TEntityClass');
54+
if ($entityClassType === null) {
55+
return [];
56+
}
5457
}
5558
if (!$entityClassType instanceof TypeWithClassName) {
5659
return [];

stubs/Persistence/ManagerRegistry.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence;
4+
5+
interface ManagerRegistry
6+
{
7+
8+
/**
9+
* @template T
10+
* @phpstan-param class-string<T> $persistentObject
11+
* @phpstan-param string $persistentManagerName
12+
* @phpstan-return ObjectRepository<T>
13+
*/
14+
public function getRepository($persistentObject, $persistentManagerName = null);
15+
16+
}

stubs/Persistence/ObjectManager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence;
4+
5+
interface ObjectManager
6+
{
7+
8+
/**
9+
* @template T
10+
* @phpstan-param class-string<T> $className
11+
* @phpstan-param mixed $id
12+
* @phpstan-return T|null
13+
*/
14+
public function find($className, $id);
15+
16+
/**
17+
* @template T
18+
* @phpstan-param T $object
19+
* @phpstan-return T
20+
*/
21+
public function merge($object);
22+
23+
/**
24+
* @template T
25+
* @phpstan-param class-string<T> $className
26+
* @phpstan-return ObjectRepository<T>
27+
*/
28+
public function getRepository($className);
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence;
4+
5+
class ObjectManagerDecorator
6+
{
7+
8+
/**
9+
* @template T
10+
* @phpstan-param class-string<T> $className
11+
* @phpstan-param mixed $id
12+
* @phpstan-return T|null
13+
*/
14+
public function find($className, $id);
15+
16+
/**
17+
* @template T
18+
* @phpstan-param T $object
19+
* @phpstan-return T
20+
*/
21+
public function merge($object);
22+
23+
/**
24+
* @template T
25+
* @phpstan-param class-string<T> $className
26+
* @phpstan-return ObjectRepository<T>
27+
*/
28+
public function getRepository($className);
29+
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence;
4+
5+
/**
6+
* @template TEntityClass
7+
*/
8+
interface ObjectRepository
9+
{
10+
11+
/**
12+
* @phpstan-param mixed $id
13+
* @phpstan-return TEntityClass|null
14+
*/
15+
public function find($id);
16+
17+
/**
18+
* @phpstan-return TEntityClass[]
19+
*/
20+
public function findAll();
21+
22+
/**
23+
* @phpstan-param mixed[] $criteria
24+
* @phpstan-param string[]|null $orderBy
25+
* @phpstan-param int|null $limit
26+
* @phpstan-param int|null $offset
27+
* @phpstan-return TEntityClass[]
28+
*/
29+
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null);
30+
31+
/**
32+
* @phpstan-param mixed[] $criteria The criteria.
33+
* @phpstan-return TEntityClass|null
34+
*/
35+
public function findOneBy(array $criteria);
36+
37+
/**
38+
* @phpstan-return class-string<TEntityClass>
39+
*/
40+
public function getClassName();
41+
42+
}

0 commit comments

Comments
 (0)