Skip to content

Commit

Permalink
[GH-3519] Deprecate passing the same class with different discriminat…
Browse files Browse the repository at this point in the history
…or values.
  • Loading branch information
beberlei authored and greg0ire committed Nov 13, 2024
1 parent a4a15ad commit 4e4acd3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Upgrade to 2.21

Using the same class several times in a discriminator map is deprecated.
In 4.0, this will be an error.

# Upgrade to 2.20

## Add `Doctrine\ORM\Query\OutputWalker` interface, deprecate `Doctrine\ORM\Query\SqlWalker::getExecutor()`
Expand Down
19 changes: 19 additions & 0 deletions src/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
use ReflectionProperty;
use RuntimeException;

use function array_count_values;
use function array_diff;
use function array_filter;
use function array_flip;
use function array_intersect;
use function array_keys;
Expand All @@ -37,6 +39,7 @@
use function enum_exists;
use function explode;
use function gettype;
use function implode;
use function in_array;
use function interface_exists;
use function is_array;
Expand Down Expand Up @@ -3207,6 +3210,22 @@ final public function getDiscriminatorColumn(): array
*/
public function setDiscriminatorMap(array $map)
{
if (count(array_flip($map)) !== count($map)) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/3519',
<<<'DEPRECATION'
Mapping a class to multiple discriminator values is deprecated,
and the discriminator mapping of %s contains duplicate values
for the following discriminator values: %s.
DEPRECATION,
$this->name,
implode(', ', array_keys(array_filter(array_count_values($map), static function (int $value): bool {
return $value > 1;
})))
);
}

foreach ($map as $value => $className) {
$this->addDiscriminatorMapClass($value, $className);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,14 @@ public function testClassNameMappingDiscriminatorValue(): void
$xmlElement->children()->{'discriminator-map'}->{'discriminator-mapping'}[0]->attributes()['value']
);
}

public function testDiscriminatorMapWithSameClassMultipleTimesDeprecated(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/3519');

$cm = new ClassMetadata(CMS\CmsUser::class);
$cm->setDiscriminatorMap(['foo' => CMS\CmsUser::class, 'bar' => CMS\CmsUser::class]);
}
}

/** @MappedSuperclass */
Expand Down

0 comments on commit 4e4acd3

Please sign in to comment.