Skip to content

Commit 59938ca

Browse files
authored
Merge pull request #12238 from mpdude/fix-collections-deprecation
Avoid triggering a deprecation notice in doctrine/collections
2 parents 298dc9b + da67f32 commit 59938ca

27 files changed

+131
-106
lines changed

phpstan-persistence2.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,9 @@ parameters:
110110
message: '#injectObjectManager#'
111111
identifier: 'method.deprecatedInterface'
112112
path: src/UnitOfWork.php
113+
114+
-
115+
message: '#^Static method Doctrine\\Common\\Collections\\Criteria\:\:create\(\) invoked with 1 parameter, 0 required\.$#'
116+
identifier: arguments.count
117+
count: 3
118+
path: src/Persisters/Collection/OneToManyPersister.php

src/Persisters/Collection/OneToManyPersister.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function count(PersistentCollection $collection)
103103
// only works with single id identifier entities. Will throw an
104104
// exception in Entity Persisters if that is not the case for the
105105
// 'mappedBy' field.
106-
$criteria = new Criteria(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner()));
106+
$criteria = Criteria::create(true)->where(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner()));
107107

108108
return $persister->count($criteria);
109109
}
@@ -135,7 +135,7 @@ public function containsKey(PersistentCollection $collection, $key)
135135
// only works with single id identifier entities. Will throw an
136136
// exception in Entity Persisters if that is not the case for the
137137
// 'mappedBy' field.
138-
$criteria = new Criteria();
138+
$criteria = Criteria::create(true);
139139

140140
$criteria->andWhere(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner()));
141141
$criteria->andWhere(Criteria::expr()->eq($mapping['indexBy'], $key));
@@ -158,7 +158,7 @@ public function contains(PersistentCollection $collection, $element)
158158
// only works with single id identifier entities. Will throw an
159159
// exception in Entity Persisters if that is not the case for the
160160
// 'mappedBy' field.
161-
$criteria = new Criteria(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner()));
161+
$criteria = Criteria::create(true)->where(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner()));
162162

163163
return $persister->exists($element, $criteria);
164164
}

tests/Tests/ORM/Cache/Persister/Entity/EntityPersisterTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testInvokeExpandParameters(): void
143143
public function testInvokeExpandCriteriaParameters(): void
144144
{
145145
$persister = $this->createPersisterDefault();
146-
$criteria = new Criteria();
146+
$criteria = Criteria::create(true);
147147

148148
$this->entityPersister->expects(self::once())
149149
->method('expandCriteriaParameters')
@@ -313,7 +313,7 @@ public function testInvokeLoadCriteria(): void
313313
$rsm = new ResultSetMappingBuilder($this->em);
314314
$persister = $this->createPersisterDefault();
315315
$entity = new Country('Foo');
316-
$criteria = new Criteria();
316+
$criteria = Criteria::create(true);
317317

318318
$this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']);
319319
$rsm->addEntityResult(Country::class, 'c');

tests/Tests/ORM/Functional/ClassTableInheritanceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,13 @@ public function testMatching(): void
494494
$this->_em->flush();
495495

496496
$repository = $this->_em->getRepository(CompanyEmployee::class);
497-
$users = $repository->matching(new Criteria(
497+
$users = $repository->matching(Criteria::create(true)->where(
498498
Criteria::expr()->eq('department', 'IT')
499499
));
500500
self::assertCount(1, $users);
501501

502502
$repository = $this->_em->getRepository(CompanyManager::class);
503-
$users = $repository->matching(new Criteria(
503+
$users = $repository->matching(Criteria::create(true)->where(
504504
Criteria::expr()->eq('department', 'IT')
505505
));
506506
self::assertCount(1, $users);

tests/Tests/ORM/Functional/EntityRepositoryCriteriaTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testLteDateComparison(): void
6666
$this->loadFixture();
6767

6868
$repository = $this->_em->getRepository(DateTimeModel::class);
69-
$dates = $repository->matching(new Criteria(
69+
$dates = $repository->matching(Criteria::create(true)->where(
7070
Criteria::expr()->lte('datetime', new DateTime('today'))
7171
));
7272

@@ -98,7 +98,7 @@ public function testIsNullComparison(): void
9898
$this->loadNullFieldFixtures();
9999
$repository = $this->_em->getRepository(DateTimeModel::class);
100100

101-
$dates = $repository->matching(new Criteria(
101+
$dates = $repository->matching(Criteria::create(true)->where(
102102
Criteria::expr()->isNull('time')
103103
));
104104

@@ -110,7 +110,7 @@ public function testEqNullComparison(): void
110110
$this->loadNullFieldFixtures();
111111
$repository = $this->_em->getRepository(DateTimeModel::class);
112112

113-
$dates = $repository->matching(new Criteria(
113+
$dates = $repository->matching(Criteria::create(true)->where(
114114
Criteria::expr()->eq('time', null)
115115
));
116116

@@ -122,7 +122,7 @@ public function testNotEqNullComparison(): void
122122
$this->loadNullFieldFixtures();
123123
$repository = $this->_em->getRepository(DateTimeModel::class);
124124

125-
$dates = $repository->matching(new Criteria(
125+
$dates = $repository->matching(Criteria::create(true)->where(
126126
Criteria::expr()->neq('time', null)
127127
));
128128

@@ -134,14 +134,14 @@ public function testCanCountWithoutLoadingCollection(): void
134134
$this->loadFixture();
135135
$repository = $this->_em->getRepository(DateTimeModel::class);
136136

137-
$dates = $repository->matching(new Criteria());
137+
$dates = $repository->matching(Criteria::create(true));
138138

139139
self::assertFalse($dates->isInitialized());
140140
self::assertCount(3, $dates);
141141
self::assertFalse($dates->isInitialized());
142142

143143
// Test it can work even with a constraint
144-
$dates = $repository->matching(new Criteria(
144+
$dates = $repository->matching(Criteria::create(true)->where(
145145
Criteria::expr()->lte('datetime', new DateTime('today'))
146146
));
147147

@@ -169,7 +169,7 @@ public function testCanContainsWithoutLoadingCollection(): void
169169

170170
$this->_em->clear();
171171

172-
$criteria = new Criteria();
172+
$criteria = Criteria::create(true);
173173
$criteria->andWhere($criteria->expr()->contains('content', 'Criteria'));
174174

175175
$user = $this->_em->find(User::class, $user->id);

tests/Tests/ORM/Functional/EntityRepositoryTest.php

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ public function testMatchingEmptyCriteria(): void
734734
$this->loadFixture();
735735

736736
$repository = $this->_em->getRepository(CmsUser::class);
737-
$users = $repository->matching(new Criteria());
737+
$users = $repository->matching(Criteria::create(true));
738738

739739
self::assertCount(4, $users);
740740
}
@@ -745,7 +745,7 @@ public function testMatchingCriteriaEqComparison(): void
745745
$this->loadFixture();
746746

747747
$repository = $this->_em->getRepository(CmsUser::class);
748-
$users = $repository->matching(new Criteria(
748+
$users = $repository->matching(Criteria::create(true)->where(
749749
Criteria::expr()->eq('username', 'beberlei')
750750
));
751751

@@ -758,7 +758,7 @@ public function testMatchingCriteriaNeqComparison(): void
758758
$this->loadFixture();
759759

760760
$repository = $this->_em->getRepository(CmsUser::class);
761-
$users = $repository->matching(new Criteria(
761+
$users = $repository->matching(Criteria::create(true)->where(
762762
Criteria::expr()->neq('username', 'beberlei')
763763
));
764764

@@ -771,7 +771,7 @@ public function testMatchingCriteriaInComparison(): void
771771
$this->loadFixture();
772772

773773
$repository = $this->_em->getRepository(CmsUser::class);
774-
$users = $repository->matching(new Criteria(
774+
$users = $repository->matching(Criteria::create(true)->where(
775775
Criteria::expr()->in('username', ['beberlei', 'gblanco'])
776776
));
777777

@@ -784,7 +784,7 @@ public function testMatchingCriteriaNotInComparison(): void
784784
$this->loadFixture();
785785

786786
$repository = $this->_em->getRepository(CmsUser::class);
787-
$users = $repository->matching(new Criteria(
787+
$users = $repository->matching(Criteria::create(true)->where(
788788
Criteria::expr()->notIn('username', ['beberlei', 'gblanco', 'asm89'])
789789
));
790790

@@ -797,7 +797,7 @@ public function testMatchingCriteriaLtComparison(): void
797797
$firstUserId = $this->loadFixture();
798798

799799
$repository = $this->_em->getRepository(CmsUser::class);
800-
$users = $repository->matching(new Criteria(
800+
$users = $repository->matching(Criteria::create(true)->where(
801801
Criteria::expr()->lt('id', $firstUserId + 1)
802802
));
803803

@@ -810,7 +810,7 @@ public function testMatchingCriteriaLeComparison(): void
810810
$firstUserId = $this->loadFixture();
811811

812812
$repository = $this->_em->getRepository(CmsUser::class);
813-
$users = $repository->matching(new Criteria(
813+
$users = $repository->matching(Criteria::create(true)->where(
814814
Criteria::expr()->lte('id', $firstUserId + 1)
815815
));
816816

@@ -823,7 +823,7 @@ public function testMatchingCriteriaGtComparison(): void
823823
$firstUserId = $this->loadFixture();
824824

825825
$repository = $this->_em->getRepository(CmsUser::class);
826-
$users = $repository->matching(new Criteria(
826+
$users = $repository->matching(Criteria::create(true)->where(
827827
Criteria::expr()->gt('id', $firstUserId)
828828
));
829829

@@ -836,7 +836,7 @@ public function testMatchingCriteriaGteComparison(): void
836836
$firstUserId = $this->loadFixture();
837837

838838
$repository = $this->_em->getRepository(CmsUser::class);
839-
$users = $repository->matching(new Criteria(
839+
$users = $repository->matching(Criteria::create(true)->where(
840840
Criteria::expr()->gte('id', $firstUserId)
841841
));
842842

@@ -850,7 +850,7 @@ public function testMatchingCriteriaAssocationByObjectInMemory(): void
850850

851851
$user = $this->_em->find(CmsUser::class, $userId);
852852

853-
$criteria = new Criteria(
853+
$criteria = Criteria::create(true)->where(
854854
Criteria::expr()->eq('user', $user)
855855
);
856856

@@ -871,7 +871,7 @@ public function testMatchingCriteriaAssocationInWithArray(): void
871871

872872
$user = $this->_em->find(CmsUser::class, $userId);
873873

874-
$criteria = new Criteria(
874+
$criteria = Criteria::create(true)->where(
875875
Criteria::expr()->in('user', [$user])
876876
);
877877

@@ -891,13 +891,13 @@ public function testMatchingCriteriaContainsComparison(): void
891891

892892
$repository = $this->_em->getRepository(CmsUser::class);
893893

894-
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar')));
894+
$users = $repository->matching(Criteria::create(true)->where(Criteria::expr()->contains('name', 'Foobar')));
895895
self::assertCount(0, $users);
896896

897-
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom')));
897+
$users = $repository->matching(Criteria::create(true)->where(Criteria::expr()->contains('name', 'Rom')));
898898
self::assertCount(1, $users);
899899

900-
$users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev')));
900+
$users = $repository->matching(Criteria::create(true)->where(Criteria::expr()->contains('status', 'dev')));
901901
self::assertCount(2, $users);
902902
}
903903

@@ -907,13 +907,19 @@ public function testMatchingCriteriaStartsWithComparison(): void
907907

908908
$repository = $this->_em->getRepository(CmsUser::class);
909909

910-
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo')));
910+
$users = $repository->matching(Criteria::create(true)->where(
911+
Criteria::expr()->startsWith('name', 'Foo')
912+
));
911913
self::assertCount(0, $users);
912914

913-
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R')));
915+
$users = $repository->matching(Criteria::create(true)->where(
916+
Criteria::expr()->startsWith('name', 'R')
917+
));
914918
self::assertCount(1, $users);
915919

916-
$users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de')));
920+
$users = $repository->matching(Criteria::create(true)->where(
921+
Criteria::expr()->startsWith('status', 'de')
922+
));
917923
self::assertCount(2, $users);
918924
}
919925

@@ -923,13 +929,19 @@ public function testMatchingCriteriaEndsWithComparison(): void
923929

924930
$repository = $this->_em->getRepository(CmsUser::class);
925931

926-
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo')));
932+
$users = $repository->matching(Criteria::create(true)->where(
933+
Criteria::expr()->endsWith('name', 'foo')
934+
));
927935
self::assertCount(0, $users);
928936

929-
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman')));
937+
$users = $repository->matching(Criteria::create(true)->where(
938+
Criteria::expr()->endsWith('name', 'oman')
939+
));
930940
self::assertCount(1, $users);
931941

932-
$users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev')));
942+
$users = $repository->matching(Criteria::create(true)->where(
943+
Criteria::expr()->endsWith('status', 'ev')
944+
));
933945
self::assertCount(2, $users);
934946
}
935947

@@ -939,8 +951,8 @@ public function testMatchingCriteriaNullAssocComparison(): void
939951
$fixtures = $this->loadFixtureUserEmail();
940952
$user = $this->_em->find(CmsUser::class, $fixtures[0]->id);
941953
$repository = $this->_em->getRepository(CmsUser::class);
942-
$criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email'));
943-
$criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null));
954+
$criteriaIsNull = Criteria::create(true)->where(Criteria::expr()->isNull('email'));
955+
$criteriaEqNull = Criteria::create(true)->where(Criteria::expr()->eq('email', null));
944956

945957
$user->setEmail(null);
946958
$this->_em->persist($user);
@@ -997,7 +1009,7 @@ public function testMatchingInjectionPrevented(): void
9971009
$this->expectExceptionMessage('Unrecognized field: ');
9981010

9991011
$repository = $this->_em->getRepository(CmsUser::class);
1000-
$result = $repository->matching(new Criteria(
1012+
$result = $repository->matching(Criteria::create(true)->where(
10011013
Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei')
10021014
));
10031015

tests/Tests/ORM/Functional/EnumTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function testEnumCollectionMatchingOnOneToMany(Comparison $comparison): v
557557
$library = $this->_em->find(Library::class, $library->id);
558558
self::assertFalse($library->books->isInitialized(), 'Pre-condition: lazy collection');
559559

560-
$result = $library->books->matching(Criteria::create()->where($comparison));
560+
$result = $library->books->matching(Criteria::create(true)->where($comparison));
561561

562562
self::assertCount(1, $result);
563563
self::assertSame($nonfictionBook->id, $result[0]->id);
@@ -588,7 +588,7 @@ public function testEnumCollectionMatchingOnManyToMany(Comparison $comparison):
588588
$category = $this->_em->find(BookCategory::class, $category->id);
589589
self::assertFalse($category->books->isInitialized(), 'Pre-condition: lazy collection');
590590

591-
$result = $category->books->matching(Criteria::create()->where($comparison));
591+
$result = $category->books->matching(Criteria::create(true)->where($comparison));
592592

593593
self::assertCount(1, $result);
594594
self::assertSame($nonfictionBook->id, $result[0]->id);

0 commit comments

Comments
 (0)