Skip to content

[DoctrineBridge] remove deprecated code #43100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\ChoiceList\Loader\AbstractChoiceLoader;
use Symfony\Component\Form\Exception\LogicException;

/**
* Loads choices using a Doctrine object manager.
Expand Down Expand Up @@ -68,36 +69,23 @@ protected function doLoadValuesForChoices(array $choices): array
// know that the IDs are used as values
// Attention: This optimization does not check choices for existence
if ($this->idReader) {
trigger_deprecation('symfony/doctrine-bridge', '5.1', 'Not defining explicitly the IdReader as value callback when query can be optimized is deprecated. Don\'t pass the IdReader to "%s" or define the "choice_value" option instead.', __CLASS__);
// Maintain order and indices of the given objects
$values = [];
foreach ($choices as $i => $object) {
if ($object instanceof $this->class) {
$values[$i] = $this->idReader->getIdValue($object);
}
}

return $values;
throw new LogicException('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
}

return parent::doLoadValuesForChoices($choices);
}

protected function doLoadChoicesForValues(array $values, ?callable $value): array
{
$legacy = $this->idReader && null === $value;

if ($legacy) {
trigger_deprecation('symfony/doctrine-bridge', '5.1', 'Not defining explicitly the IdReader as value callback when query can be optimized is deprecated. Don\'t pass the IdReader to "%s" or define the "choice_value" option instead.', __CLASS__);
if ($this->idReader && null === $value) {
throw new LogicException('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');
}

$idReader = null;
if (\is_array($value) && $value[0] instanceof IdReader) {
$idReader = $value[0];
} elseif ($value instanceof \Closure && ($rThis = (new \ReflectionFunction($value))->getClosureThis()) instanceof IdReader) {
$idReader = $rThis;
} elseif ($legacy) {
$idReader = $this->idReader;
}

// Optimize performance in case we have an object loader and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
use Symfony\Component\Form\Exception\LogicException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -191,12 +192,11 @@ public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
$this->assertSame([], $loader->loadValuesForChoices([]));
}

/**
* @group legacy
*/
public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
{
$this->expectDeprecation('Since symfony/doctrine-bridge 5.1: Not defining explicitly the IdReader as value callback when query can be optimized is deprecated. Don\'t pass the IdReader to "Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader" or define the "choice_value" option instead.');
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');

$loader = new DoctrineChoiceLoader(
$this->om,
$this->class,
Expand Down Expand Up @@ -293,12 +293,11 @@ public function testLoadChoicesForValuesDoesNotLoadIfEmptyValues()
$this->assertSame([], $loader->loadChoicesForValues([]));
}

/**
* @group legacy
*/
public function testLegacyLoadChoicesForValuesLoadsOnlyChoicesIfValueUseIdReader()
{
$this->expectDeprecation('Since symfony/doctrine-bridge 5.1: Not defining explicitly the IdReader as value callback when query can be optimized is deprecated. Don\'t pass the IdReader to "Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader" or define the "choice_value" option instead.');
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Not defining the IdReader explicitly as a value callback when the query can be optimized is not supported.');

$loader = new DoctrineChoiceLoader(
$this->om,
$this->class,
Expand All @@ -315,17 +314,8 @@ public function testLegacyLoadChoicesForValuesLoadsOnlyChoicesIfValueUseIdReader
$this->repository->expects($this->never())
->method('findAll');

$this->objectLoader->expects($this->once())
->method('getEntitiesByIds')
->with('idField', [4 => '3', 7 => '2'])
->willReturn($choices);

$this->idReader->expects($this->any())
->method('getIdValue')
->willReturnMap([
[$this->obj2, '2'],
[$this->obj3, '3'],
]);
$this->objectLoader->expects($this->never())
->method('getEntitiesByIds');

$this->assertSame(
[4 => $this->obj3, 7 => $this->obj2],
Expand Down