Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a90b38

Browse files
committed
fixup
1 parent 591fd35 commit 4a90b38

File tree

3 files changed

+13
-50
lines changed

3 files changed

+13
-50
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ public function __construct(ObjectManager $manager, string $class, IdReader $idR
5050
$classMetadata = $manager->getClassMetadata($class);
5151

5252
if ($idReader && !$idReader->isSingleId()) {
53-
@trigger_error(sprintf('Passing an instance of "%s" with an entity class "%s" that has a composite id is deprecated since Symfony 4.3 and will throw an exception in 5.0.', IdReader::class, $class), E_USER_DEPRECATED);
53+
@trigger_error(sprintf('Passing an instance of "%s" to "%s" with an entity class "%s" that has a composite id is deprecated since Symfony 4.3 and will throw an exception in 5.0.', IdReader::class, __CLASS__, $class), E_USER_DEPRECATED);
5454

5555
// In Symfony 5.0
56-
// throw new \InvalidArgumentException('The second argument `$idReader` must be null when the query cannot be optimized because of composite id fields.');
56+
// throw new \InvalidArgumentException(sprintf('The second argument `$idReader` of "%s" must be null when the query cannot be optimized because of composite id fields.', __METHOD__));
5757
}
5858

5959
if ((5 > \func_num_args() || false !== func_get_arg(4)) && null === $idReader) {
6060
$idReader = new IdReader($manager, $classMetadata);
6161

6262
if ($idReader->isSingleId()) {
63-
@trigger_error(sprintf('Not explicitly passing an instance of "%s" when it can optimize single id entity "%s" has been deprecated in 4.3 and will not apply any optimization in 5.0.', IdReader::class, $class), E_USER_DEPRECATED);
63+
@trigger_error(sprintf('Not explicitly passing an instance of "%s" to "%s" when it can optimize single id entity "%s" has been deprecated in 4.3 and will not apply any optimization in 5.0.', IdReader::class, __CLASS__, $class), E_USER_DEPRECATED);
6464
} else {
6565
$idReader = null;
6666
}

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,10 @@ public function configureOptions(OptionsResolver $resolver)
163163
};
164164

165165
$choiceName = function (Options $options) {
166-
/** @var IdReader $idReader */
167-
$idReader = $options['id_reader'];
168-
169166
// If the object has a single-column, numeric ID, use that ID as
170167
// field name. We can only use numeric IDs as names, as we cannot
171168
// guarantee that a non-numeric ID contains a valid form name
172-
if ($idReader->isIntId()) {
169+
if ($options['id_reader'] instanceof IdReader && $options['id_reader']->isIntId()) {
173170
return [__CLASS__, 'createChoiceName'];
174171
}
175172

@@ -181,12 +178,9 @@ public function configureOptions(OptionsResolver $resolver)
181178
// are indexed by an incrementing integer.
182179
// Use the ID/incrementing integer as choice value.
183180
$choiceValue = function (Options $options) {
184-
/** @var IdReader $idReader */
185-
$idReader = $options['id_reader'];
186-
187181
// If the entity has a single-column ID, use that ID as value
188-
if ($idReader->isSingleId()) {
189-
return [$idReader, 'getIdValue'];
182+
if ($options['id_reader'] instanceof IdReader && $options['id_reader']->isSingleId()) {
183+
return [$options['id_reader'], 'getIdValue'];
190184
}
191185

192186
// Otherwise, an incrementing integer is used as value automatically
@@ -240,7 +234,11 @@ public function configureOptions(OptionsResolver $resolver)
240234
$this->idReaders[$hash] = new IdReader($options['em'], $classMetadata);
241235
}
242236

243-
return $this->idReaders[$hash];
237+
if ($this->idReaders[$hash]->isSingleId()) {
238+
return $this->idReaders[$hash];
239+
}
240+
241+
return null;
244242
};
245243

246244
$resolver->setDefaults([

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,6 @@ public function testLoadValuesForChoicesLoadsIfSingleIntIdAndValueGiven()
216216
$choices = [$this->obj1, $this->obj2, $this->obj3];
217217
$value = function (\stdClass $object) { return $object->name; };
218218

219-
$this->idReader->expects($this->any())
220-
->method('isSingleId')
221-
->willReturn(true);
222-
223219
$this->repository->expects($this->once())
224220
->method('findAll')
225221
->willReturn($choices);
@@ -240,10 +236,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfValueIsIdReader()
240236

241237
$value = [$this->idReader, 'getIdValue'];
242238

243-
$this->idReader->expects($this->any())
244-
->method('isSingleId')
245-
->willReturn(true);
246-
247239
$this->repository->expects($this->never())
248240
->method('findAll');
249241

@@ -304,10 +296,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfSingleIntId()
304296

305297
$choices = [$this->obj2, $this->obj3];
306298

307-
$this->idReader->expects($this->any())
308-
->method('isSingleId')
309-
->willReturn(true);
310-
311299
$this->idReader->expects($this->any())
312300
->method('getIdField')
313301
->willReturn('idField');
@@ -344,10 +332,6 @@ public function testLoadChoicesForValuesLoadsAllIfSingleIntIdAndValueGiven()
344332
$choices = [$this->obj1, $this->obj2, $this->obj3];
345333
$value = function (\stdClass $object) { return $object->name; };
346334

347-
$this->idReader->expects($this->any())
348-
->method('isSingleId')
349-
->willReturn(true);
350-
351335
$this->repository->expects($this->once())
352336
->method('findAll')
353337
->willReturn($choices);
@@ -370,10 +354,6 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
370354
$choices = [$this->obj2, $this->obj3];
371355
$value = [$this->idReader, 'getIdValue'];
372356

373-
$this->idReader->expects($this->any())
374-
->method('isSingleId')
375-
->willReturn(true);
376-
377357
$this->idReader->expects($this->any())
378358
->method('getIdField')
379359
->willReturn('idField');
@@ -399,7 +379,7 @@ public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader()
399379
/**
400380
* @group legacy
401381
*
402-
* @expectedDeprecation Not explicitly passing an instance of "Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader" when it can optimize single id entity "%s" has been deprecated in 4.3 and will not apply any optimization in 5.0.
382+
* @expectedDeprecation Not explicitly passing an instance of "Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader" to "Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader" when it can optimize single id entity "%s" has been deprecated in 4.3 and will not apply any optimization in 5.0.
403383
*/
404384
public function testLoaderWithoutIdReaderCanBeOptimized()
405385
{
@@ -446,14 +426,6 @@ public function testLoaderWithoutIdReaderCanBeOptimized()
446426

447427
$choices = [$obj1, $obj2];
448428

449-
$this->idReader->expects($this->any())
450-
->method('isSingleId')
451-
->willReturn(true);
452-
453-
$this->idReader->expects($this->any())
454-
->method('getIdField')
455-
->willReturn('idField');
456-
457429
$this->repository->expects($this->never())
458430
->method('findAll');
459431

@@ -462,20 +434,13 @@ public function testLoaderWithoutIdReaderCanBeOptimized()
462434
->with('idField', ['1'])
463435
->willReturn($choices);
464436

465-
$this->idReader->expects($this->any())
466-
->method('getIdValue')
467-
->willReturnMap([
468-
[$obj1, '1'],
469-
[$obj2, '2'],
470-
]);
471-
472437
$this->assertSame([$obj1], $loader->loadChoicesForValues(['1']));
473438
}
474439

475440
/**
476441
* @group legacy
477442
*
478-
* @deprecationMessage Passing an instance of "Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader" with an entity class "stdClass" that has a composite id is deprecated since Symfony 4.3 and will throw an exception in 5.0.
443+
* @deprecationMessage Passing an instance of "Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader" to "Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader" with an entity class "stdClass" that has a composite id is deprecated since Symfony 4.3 and will throw an exception in 5.0.
479444
*/
480445
public function testPassingIdReaderWithoutSingleIdEntity()
481446
{

0 commit comments

Comments
 (0)