Skip to content

Commit

Permalink
Merge pull request #9677 from greg0ire/assert-drivers-are-orm-specific
Browse files Browse the repository at this point in the history
Document ORM drivers only really load ORM metadata
  • Loading branch information
greg0ire authored Apr 24, 2022
2 parents b7e9dd0 + 85238d4 commit a552df6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 486 deletions.
22 changes: 13 additions & 9 deletions lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -69,10 +69,14 @@ public function __construct($reader, $paths = null)

/**
* {@inheritDoc}
*
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @template T of object
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, PersistenceClassMetadata $metadata)
{
assert($metadata instanceof Mapping\ClassMetadata);
$class = $metadata->getReflectionClass()
// this happens when running annotation driver in combination with
// static reflection services. This is not the nicest fix
Expand Down Expand Up @@ -298,7 +302,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value)
);

if ($metadata->inheritanceType !== ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
if ($metadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
// Evaluate DiscriminatorColumn annotation
if (isset($classAnnotations[Mapping\DiscriminatorColumn::class])) {
$discrColumnAnnot = $classAnnotations[Mapping\DiscriminatorColumn::class];
Expand Down Expand Up @@ -543,7 +547,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
private function loadRelationShipMapping(
ReflectionProperty $property,
array &$mapping,
ClassMetadata $metadata,
PersistenceClassMetadata $metadata,
array $joinColumns,
string $className
): void {
Expand Down Expand Up @@ -647,7 +651,7 @@ private function loadRelationShipMapping(
/**
* Attempts to resolve the fetch mode.
*
* @psalm-return \Doctrine\ORM\Mapping\ClassMetadata::FETCH_* The fetch mode as defined in ClassMetadata.
* @psalm-return ClassMetadata::FETCH_* The fetch mode as defined in ClassMetadata.
*
* @throws MappingException If the fetch mode is not valid.
*/
Expand All @@ -663,7 +667,7 @@ private function getFetchMode(string $className, string $fetchMode): int
/**
* Attempts to resolve the generated mode.
*
* @psalm-return ClassMetadataInfo::GENERATED_*
* @psalm-return ClassMetadata::GENERATED_*
*
* @throws MappingException If the fetch mode is not valid.
*/
Expand Down Expand Up @@ -763,7 +767,7 @@ private function joinColumnToArray(Mapping\JoinColumn $joinColumn): array
* precision: int,
* notInsertable?: bool,
* notUpdateble?: bool,
* generated?: ClassMetadataInfo::GENERATED_*,
* generated?: ClassMetadata::GENERATED_*,
* enumType?: class-string,
* options?: mixed[],
* columnName?: string,
Expand Down
22 changes: 14 additions & 8 deletions lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver;
use LogicException;
use ReflectionClass;
Expand Down Expand Up @@ -98,10 +98,16 @@ public function isTransient($className)
return true;
}

public function loadMetadataForClass($className, ClassMetadata $metadata): void
/**
* {@inheritDoc}
*
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @template T of object
*/
public function loadMetadataForClass($className, PersistenceClassMetadata $metadata): void
{
assert($metadata instanceof ClassMetadataInfo);

$reflectionClass = $metadata->getReflectionClass();

$classAttributes = $this->reader->getClassAnnotations($reflectionClass);
Expand Down Expand Up @@ -238,7 +244,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata): void
constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAttribute->value)
);

if ($metadata->inheritanceType !== Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
if ($metadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
// Evaluate DiscriminatorColumn annotation
if (isset($classAttributes[Mapping\DiscriminatorColumn::class])) {
$discrColumnAttribute = $classAttributes[Mapping\DiscriminatorColumn::class];
Expand Down Expand Up @@ -491,7 +497,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata): void

// Check for `fetch`
if ($associationOverride->fetch) {
$override['fetch'] = constant(Mapping\ClassMetadata::class . '::FETCH_' . $associationOverride->fetch);
$override['fetch'] = constant(ClassMetadata::class . '::FETCH_' . $associationOverride->fetch);
}

$metadata->setAssociationOverride($fieldName, $override);
Expand Down Expand Up @@ -558,7 +564,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata): void
* @param string $className The class name.
* @param string $fetchMode The fetch mode.
*
* @return int The fetch mode as defined in ClassMetadata.
* @return ClassMetadata::FETCH_* The fetch mode as defined in ClassMetadata.
*
* @throws MappingException If the fetch mode is not valid.
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use InvalidArgumentException;

Expand Down Expand Up @@ -166,8 +167,13 @@ public function setInflector(Inflector $inflector): void

/**
* {@inheritDoc}
*
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @template T of object
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, PersistenceClassMetadata $metadata)
{
$this->reverseEngineerMappingFromDatabase();

Expand Down
15 changes: 10 additions & 5 deletions lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
use Doctrine\ORM\Mapping\ClassMetadata as Metadata;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -53,8 +53,13 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS

/**
* {@inheritDoc}
*
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @template T of object
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, PersistenceClassMetadata $metadata)
{
$xmlRoot = $this->getElement($className);
assert($xmlRoot instanceof SimpleXMLElement);
Expand Down Expand Up @@ -168,7 +173,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$inheritanceType = (string) $xmlRoot['inheritance-type'];
$metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType));

if ($metadata->inheritanceType !== Metadata::INHERITANCE_TYPE_NONE) {
if ($metadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
// Evaluate <discriminator-column...>
if (isset($xmlRoot->{'discriminator-column'})) {
$discrColumn = $xmlRoot->{'discriminator-column'};
Expand Down Expand Up @@ -685,7 +690,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)

// Check for `fetch`
if (isset($overrideElement['fetch'])) {
$override['fetch'] = constant(Metadata::class . '::FETCH_' . (string) $overrideElement['fetch']);
$override['fetch'] = constant(ClassMetadata::class . '::FETCH_' . (string) $overrideElement['fetch']);
}

$metadata->setAssociationOverride($fieldName, $override);
Expand Down
15 changes: 10 additions & 5 deletions lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
use Doctrine\ORM\Mapping\ClassMetadata as Metadata;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -60,8 +60,13 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS

/**
* {@inheritDoc}
*
* @psalm-param class-string<T> $className
* @psalm-param ClassMetadata<T> $metadata
*
* @template T of object
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
public function loadMetadataForClass($className, PersistenceClassMetadata $metadata)
{
$element = $this->getElement($className);

Expand Down Expand Up @@ -186,7 +191,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
if (isset($element['inheritanceType'])) {
$metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType'])));

if ($metadata->inheritanceType !== Metadata::INHERITANCE_TYPE_NONE) {
if ($metadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
// Evaluate discriminatorColumn
if (isset($element['discriminatorColumn'])) {
$discrColumn = $element['discriminatorColumn'];
Expand Down Expand Up @@ -683,7 +688,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)

// Check for `fetch`
if (isset($associationOverrideElement['fetch'])) {
$override['fetch'] = constant(Metadata::class . '::FETCH_' . $associationOverrideElement['fetch']);
$override['fetch'] = constant(ClassMetadata::class . '::FETCH_' . $associationOverrideElement['fetch']);
}

$metadata->setAssociationOverride($fieldName, $override);
Expand Down
Loading

0 comments on commit a552df6

Please sign in to comment.