Skip to content

Commit c1c32c9

Browse files
committed
Magic repository methods - test to use generic bound when the type is unknown
1 parent b3a7b16 commit c1c32c9

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.2 || ^8.0",
10-
"phpstan/phpstan": "^1.10"
10+
"phpstan/phpstan": "^1.10.12"
1111
},
1212
"conflict": {
1313
"doctrine/collections": "<1.0",

Diff for: tests/DoctrineIntegration/ORM/data/customRepositoryUsage-6.json

+10
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"message": "Method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\Example::__construct() has parameter $anotherRepository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyRepository but does not specify its types: T",
99
"line": 22,
1010
"ignorable": true
11+
},
12+
{
13+
"message": "Property PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\UseAbstractRepository::$repository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\AbstractRepository does not specify its types: T",
14+
"line": 119,
15+
"ignorable": true
16+
},
17+
{
18+
"message": "Method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\UseAbstractRepository::__construct() has parameter $repository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\AbstractRepository but does not specify its types: T",
19+
"line": 127,
20+
"ignorable": true
1121
}
1222
]

Diff for: tests/DoctrineIntegration/ORM/data/customRepositoryUsage.php

+38
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,41 @@ public function findOneByBlabla(): int
102102
return 1;
103103
}
104104
}
105+
106+
/**
107+
* @template T of MyEntity
108+
* @extends EntityRepository<T>
109+
*/
110+
class AbstractRepository extends EntityRepository
111+
{
112+
113+
}
114+
115+
class UseAbstractRepository
116+
{
117+
118+
/** @var AbstractRepository */
119+
private $repository;
120+
121+
/** @var AbstractRepository<MyEntity> */
122+
private $genericRepository;
123+
124+
/**
125+
* @param AbstractRepository<MyEntity> $genericRepository
126+
*/
127+
public function __construct(
128+
AbstractRepository $repository,
129+
AbstractRepository $genericRepository
130+
)
131+
{
132+
$this->repository = $repository;
133+
$this->genericRepository = $genericRepository;
134+
}
135+
136+
public function find(): void
137+
{
138+
$entity = $this->repository->findOneById(1);
139+
$entity = $this->genericRepository->findOneById(1);
140+
}
141+
142+
}

0 commit comments

Comments
 (0)