Skip to content

Commit 88931dc

Browse files
committed
Add some generics to tests
1 parent fe38be4 commit 88931dc

35 files changed

Lines changed: 259 additions & 64 deletions

phpstan-baseline.neon

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,6 @@ parameters:
209209
count: 1
210210
path: lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php
211211

212-
# See https://github.com/phpstan/phpstan/issues/5512
213-
-
214-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Functional\\\\My.*Collection\\:\\:.*\\(\\) should return Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Functional\\\\My.*Collection but returns Doctrine\\\\Common\\\\Collections\\\\ArrayCollection\\<\\(int\\|string\\), mixed\\>\\.$#"
215-
count: 3
216-
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomCollectionsTest.php
217-
218212
# These classes are not final
219213
-
220214
message: "#^Unsafe call to private method Doctrine\\\\ODM\\\\MongoDB\\\\Query\\\\Expr\\:\\:convertExpression\\(\\) through static::\\.$#"
@@ -253,6 +247,12 @@ parameters:
253247
count: 1
254248
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/DiscriminatorsDefaultValueTest.php
255249

250+
# Collection elements cannot be covariant, see https://github.com/doctrine/collections/pull/220
251+
-
252+
message: "#^Parameter \\#2 \\$projects of class Documents\\\\Developer constructor expects Doctrine\\\\Common\\\\Collections\\\\Collection\\<int, Documents\\\\Project\\>\\|null, Doctrine\\\\Common\\\\Collections\\\\ArrayCollection\\<int, Documents\\\\SubProject\\> given\\.$#"
253+
count: 1
254+
path: tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php
255+
256256
# See https://github.com/phpstan/phpstan/issues/5860
257257
-
258258
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\UnitOfWork\\:\\:getOrCreateDocument\\(\\) should return T of object but returns \\(Doctrine\\\\Persistence\\\\NotifyPropertyChanged&ProxyManager\\\\Proxy\\\\GhostObjectInterface&T of object\\)\\|\\(ProxyManager\\\\Proxy\\\\GhostObjectInterface&T of object\\)\\|T of object\\.$#"
@@ -284,6 +284,7 @@ parameters:
284284
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
285285
count: 1
286286
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php
287+
287288
-
288289
message: "#Property .+ is never written, only read.#"
289290
path: tests

tests/Doctrine/ODM/MongoDB/Tests/Events/LifecycleListenersTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ public function __construct(TestCase $phpunit)
250250
$this->phpunit = $phpunit;
251251
}
252252

253+
/**
254+
* @param PostCollectionLoadEventArgs<int, TestEmbeddedDocument> $e
255+
*/
253256
public function postCollectionLoad(PostCollectionLoadEventArgs $e): void
254257
{
255258
switch ($this->at++) {

tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testAtomicUpsert(): void
9090
}
9191

9292
/**
93-
* @param array|ArrayCollection|null $clearWith
93+
* @param mixed[]|ArrayCollection<int, mixed>|null $clearWith
9494
*
9595
* @dataProvider provideAtomicCollectionUnset
9696
*/

tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomCollectionsTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class DocumentWithCustomCollection
205205
* targetDocument=EmbeddedDocumentInCustomCollection::class
206206
* )
207207
*
208-
* @var MyEmbedsCollection
208+
* @var MyEmbedsCollection<int, EmbeddedDocumentInCustomCollection>
209209
*/
210210
public $coll;
211211

@@ -225,7 +225,7 @@ class DocumentWithCustomCollection
225225
* targetDocument=DocumentWithCustomCollection::class
226226
* )
227227
*
228-
* @var MyDocumentsCollection
228+
* @var MyDocumentsCollection<int, DocumentWithCustomCollection>
229229
*/
230230
public $refMany;
231231

@@ -236,7 +236,7 @@ class DocumentWithCustomCollection
236236
* targetDocument=DocumentWithCustomCollection::class
237237
* )
238238
*
239-
* @var MyDocumentsCollection
239+
* @var MyDocumentsCollection<int, DocumentWithCustomCollection>
240240
*/
241241
public $inverseRefMany;
242242

@@ -275,15 +275,26 @@ public function __construct(string $name, bool $enabled)
275275
}
276276
}
277277

278+
/**
279+
* @template TKey of array-key
280+
* @template TElement
281+
* @template-extends ArrayCollection<TKey, TElement>
282+
*/
278283
class MyEmbedsCollection extends ArrayCollection
279284
{
285+
/**
286+
* @return MyEmbedsCollection<TKey, TElement>
287+
*/
280288
public function getByName(string $name): MyEmbedsCollection
281289
{
282290
return $this->filter(static function ($item) use ($name) {
283291
return $item->name === $name;
284292
});
285293
}
286294

295+
/**
296+
* @return MyEmbedsCollection<TKey, TElement>
297+
*/
287298
public function getEnabled(): MyEmbedsCollection
288299
{
289300
return $this->filter(static function ($item) {
@@ -303,8 +314,16 @@ public function nothingReally(): void
303314
}
304315
}
305316

317+
/**
318+
* @template TKey of array-key
319+
* @template TElement
320+
* @template-extends ArrayCollection<TKey, TElement>
321+
*/
306322
class MyDocumentsCollection extends ArrayCollection
307323
{
324+
/**
325+
* @return MyDocumentsCollection<TKey, TElement>
326+
*/
308327
public function havingEmbeds(): MyDocumentsCollection
309328
{
310329
return $this->filter(static function ($item) {

tests/Doctrine/ODM/MongoDB/Tests/Functional/DocumentPersisterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DocumentPersisterTest extends BaseTest
3232
/** @var class-string<DocumentPersisterTestDocument> */
3333
private $class;
3434

35-
/** @var DocumentPersister */
35+
/** @var DocumentPersister<DocumentPersisterTestDocument> */
3636
private $documentPersister;
3737

3838
public function setUp(): void

tests/Doctrine/ODM/MongoDB/Tests/Functional/OrphanRemovalEmbedTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public function testClearAndAddEmbedMany(): void
141141
$this->assertNotNull($this->getAddressRepository()->find($address3->id), 'Should have added address 3');
142142
}
143143

144+
/**
145+
* @return DocumentRepository<OrphanRemovalCascadeUser>
146+
*/
144147
private function getUserRepository(): DocumentRepository
145148
{
146149
$repository = $this->dm->getRepository(OrphanRemovalCascadeUser::class);
@@ -150,6 +153,9 @@ private function getUserRepository(): DocumentRepository
150153
return $repository;
151154
}
152155

156+
/**
157+
* @return DocumentRepository<OrphanRemovalCascadeAddress>
158+
*/
153159
private function getAddressRepository(): DocumentRepository
154160
{
155161
$repository = $this->dm->getRepository(OrphanRemovalCascadeAddress::class);

tests/Doctrine/ODM/MongoDB/Tests/Functional/OrphanRemovalTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ public function testOrphanRemovalReferenceManyOnRemoveWithoutCascade(): void
282282
$this->assertNull($this->getProfileRepository()->find($profile2->id), 'Profile 2 should have been removed');
283283
}
284284

285+
/**
286+
* @return DocumentRepository<OrphanRemovalUser>
287+
*/
285288
private function getUserRepository(): DocumentRepository
286289
{
287290
$repository = $this->dm->getRepository(OrphanRemovalUser::class);
@@ -291,6 +294,9 @@ private function getUserRepository(): DocumentRepository
291294
return $repository;
292295
}
293296

297+
/**
298+
* @return DocumentRepository<OrphanRemovalProfile>
299+
*/
294300
private function getProfileRepository(): DocumentRepository
295301
{
296302
$repository = $this->dm->getRepository(OrphanRemovalProfile::class);

tests/Doctrine/ODM/MongoDB/Tests/Functional/RepositoriesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RepositoriesTest extends BaseTest
1414
/** @var User */
1515
private $user;
1616

17-
/** @var DocumentRepository */
17+
/** @var DocumentRepository<User> */
1818
private $repository;
1919

2020
public function setUp(): void

tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1232Test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class GH1232Comment
8787
public $post;
8888
}
8989

90+
/**
91+
* @template-extends DocumentRepository<GH1232Comment>
92+
*/
9093
class GH1232CommentRepository extends DocumentRepository
9194
{
9295
/**

tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1572Test.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,14 @@ public function __construct(GH1572Blog $blog)
9999
}
100100
}
101101

102+
/**
103+
* @template-extends DocumentRepository<GH1572Blog>
104+
*/
102105
class GH1572PostRepository extends DocumentRepository
103106
{
107+
/**
108+
* @return Iterator<GH1572Blog>
109+
*/
104110
public function getPostsForBlog(GH1572Blog $blog): Iterator
105111
{
106112
return $this->createQueryBuilder()

0 commit comments

Comments
 (0)