Skip to content
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

Reduce deprecations due to FieldMapping array access #2889

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion doc/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Annotation implements Driver
}
// validate encoding type
$mapping = $meta->getFieldMapping($field);
if ($mapping['type'] != 'string') {
if (($mapping->type ?? $mapping['type']) != 'string') {
throw new \Exception("Only strings can be encoded");
}
// store the metadata
Expand Down
2 changes: 1 addition & 1 deletion src/Blameable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
2 changes: 1 addition & 1 deletion src/Blameable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
2 changes: 1 addition & 1 deletion src/IpTraceable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
2 changes: 1 addition & 1 deletion src/IpTraceable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
2 changes: 1 addition & 1 deletion src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
}

$mapping = $objectMeta->getAssociationMapping($field);
$value = $value ? $this->getEntityManager()->getReference($mapping['targetEntity'], $value) : null;
$value = $value ? $this->getEntityManager()->getReference($mapping->targetEntity ?? $mapping['targetEntity'], $value) : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/Driver/AbstractAnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], $this->validTypes, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], $this->validTypes, true);
}

/**
Expand Down
28 changes: 14 additions & 14 deletions src/ReferenceIntegrity/ReferenceIntegrityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public function preRemove(EventArgs $args)
throw new InvalidMappingException(sprintf("Reference '%s' on '%s' should have 'mappedBy' option defined", $property, $meta->getName()));
}

assert(class_exists($fieldMapping['targetDocument']));
assert(class_exists($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));

$subMeta = $om->getClassMetadata($fieldMapping['targetDocument']);
$subMeta = $om->getClassMetadata($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']);

if (!$subMeta->hasField($fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping['mappedBy'], $fieldMapping['targetDocument']));
if (!$subMeta->hasField($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));
}

$refReflProp = $subMeta->getReflectionProperty($fieldMapping['mappedBy']);
$refReflProp = $subMeta->getReflectionProperty($fieldMapping->mappedBy ?? $fieldMapping['mappedBy']);

if ($meta->isCollectionValuedReference($property)) {
foreach ($refDoc as $refObj) {
Expand All @@ -112,19 +112,19 @@ public function preRemove(EventArgs $args)
throw new InvalidMappingException(sprintf("Reference '%s' on '%s' should have 'mappedBy' option defined", $property, $meta->getName()));
}

assert(class_exists($fieldMapping['targetDocument']));
assert(class_exists($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));

$subMeta = $om->getClassMetadata($fieldMapping['targetDocument']);
$subMeta = $om->getClassMetadata($fieldMapping->targetDocument ?? $fieldMapping['targetDocument']);

if (!$subMeta->hasField($fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping['mappedBy'], $fieldMapping['targetDocument']));
if (!$subMeta->hasField($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Unable to find reference integrity [%s] as mapped property in entity - %s', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));
}

if (!$subMeta->isCollectionValuedReference($fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Reference integrity [%s] mapped property in entity - %s should be a Reference Many', $fieldMapping['mappedBy'], $fieldMapping['targetDocument']));
if (!$subMeta->isCollectionValuedReference($fieldMapping->mappedBy ?? $fieldMapping['mappedBy'])) {
throw new InvalidMappingException(sprintf('Reference integrity [%s] mapped property in entity - %s should be a Reference Many', $fieldMapping->mappedBy ?? $fieldMapping['mappedBy'], $fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));
}

$refReflProp = $subMeta->getReflectionProperty($fieldMapping['mappedBy']);
$refReflProp = $subMeta->getReflectionProperty($fieldMapping->mappedBy ?? $fieldMapping['mappedBy']);

if ($meta->isCollectionValuedReference($property)) {
foreach ($refDoc as $refObj) {
Expand All @@ -143,10 +143,10 @@ public function preRemove(EventArgs $args)
break;
case Validator::RESTRICT:
if ($meta->isCollectionValuedReference($property) && $refDoc->count() > 0) {
throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' collection is restricted", $fieldMapping['targetDocument']));
throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' collection is restricted", $fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));
}
if ($meta->isSingleValuedReference($property) && null !== $refDoc) {
throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' document is restricted", $fieldMapping['targetDocument']));
throw new ReferenceIntegrityStrictException(sprintf("The reference integrity for the '%s' document is restricted", $fieldMapping->targetDocument ?? $fieldMapping['targetDocument']));
}

break;
Expand Down
2 changes: 1 addition & 1 deletion src/Sluggable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sluggable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,12 @@ private function generateSlug(SluggableAdapter $ea, object $object): void
}

// cut slug if exceeded in length
if (isset($mapping['length']) && strlen($slug) > $mapping['length']) {
$slug = substr($slug, 0, $mapping['length']);
$length = $mapping->length ?? $mapping['length'] ?? null;
if (null !== $length && strlen($slug) > $length) {
$slug = substr($slug, 0, $length);
}

if (isset($mapping['nullable']) && $mapping['nullable'] && 0 === strlen($slug)) {
if (($mapping->nullable ?? $mapping['nullable'] ?? false) && 0 === strlen($slug)) {
$slug = null;
}

Expand Down Expand Up @@ -546,11 +547,12 @@ private function makeUniqueSlug(SluggableAdapter $ea, object $object, string $pr
}

$mapping = $meta->getFieldMapping($config['slug']);
if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) {
$length = $mapping->length ?? $mapping['length'] ?? null;
if (null !== $length && strlen($generatedSlug) > $length) {
$generatedSlug = substr(
$generatedSlug,
0,
$mapping['length'] - (strlen($uniqueSuffix) + strlen($config['separator']))
$length - (strlen($uniqueSuffix) + strlen($config['separator']))
);
$this->exponent = strlen($uniqueSuffix) - 1;
if (substr($generatedSlug, -strlen($config['separator'])) == $config['separator']) {
Expand Down
4 changes: 2 additions & 2 deletions src/SoftDeleteable/Mapping/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static function validateField(ClassMetadata $meta, $field)

$fieldMapping = $meta->getFieldMapping($field);

if (!in_array($fieldMapping['type'], self::$validTypes, true)) {
throw new InvalidMappingException(sprintf('Field "%s" (type "%s") must be of one of the following types: "%s" in entity %s', $field, $fieldMapping['type'], implode(', ', self::$validTypes), $meta->getName()));
if (!in_array($fieldMapping->type ?? $fieldMapping['type'], self::$validTypes, true)) {
throw new InvalidMappingException(sprintf('Field "%s" (type "%s") must be of one of the following types: "%s" in entity %s', $field, $fieldMapping->type ?? $fieldMapping['type'], implode(', ', self::$validTypes), $meta->getName()));
}
}
}
2 changes: 1 addition & 1 deletion src/Sortable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sortable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Timestampable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
2 changes: 1 addition & 1 deletion src/Timestampable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($mapping->type ?? $mapping['type'], self::VALID_TYPES, true);
}
}
8 changes: 4 additions & 4 deletions src/Translatable/Query/TreeWalker/TranslationWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private function prepareTranslatedComponents(): void

$mappingFK = $transMeta->getFieldMapping('foreignKey');
$mappingPK = $meta->getFieldMapping($identifier);
$fkColName = $this->getCastedForeignKey($compTblAlias.'.'.$idColName, $mappingFK['type'], $mappingPK['type']);
$fkColName = $this->getCastedForeignKey($compTblAlias.'.'.$idColName, $mappingFK->type ?? $mappingFK['type'], $mappingPK->type ?? $mappingPK['type']);
$sql .= ' AND '.$tblAlias.'.'.$quoteStrategy->getColumnName('foreignKey', $transMeta, $this->platform)
.' = '.$fkColName;
}
Expand All @@ -309,10 +309,10 @@ private function prepareTranslatedComponents(): void
// Treat translation as original field type
$fieldMapping = $meta->getFieldMapping($field);
if ((($this->platform instanceof AbstractMySQLPlatform)
&& in_array($fieldMapping['type'], ['decimal'], true))
&& in_array($fieldMapping->type ?? $fieldMapping['type'], ['decimal'], true))
|| (!($this->platform instanceof AbstractMySQLPlatform)
&& !in_array($fieldMapping['type'], ['datetime', 'datetimetz', 'date', 'time'], true))) {
$type = Type::getType($fieldMapping['type']);
&& !in_array($fieldMapping->type ?? $fieldMapping['type'], ['datetime', 'datetimetz', 'date', 'time'], true))) {
$type = Type::getType($fieldMapping->type ?? $fieldMapping['type']);

// In ORM 2.x, $fieldMapping is an array. In ORM 3.x, it's a data object. Always cast to an array for compatibility across versions.
$substituteField = 'CAST('.$substituteField.' AS '.$type->getSQLDeclaration((array) $fieldMapping, $this->platform).')';
Expand Down
25 changes: 19 additions & 6 deletions src/Tree/Mapping/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Gedmo\Tree\Mapping;

use Doctrine\ORM\Mapping\FieldMapping;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Gedmo\Exception\InvalidMappingException;

Expand Down Expand Up @@ -97,7 +98,7 @@ public function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], self::VALID_TYPES, true);
return $mapping && in_array($this->getMappingType($mapping), self::VALID_TYPES, true);
}

/**
Expand All @@ -112,7 +113,7 @@ public function isValidFieldForPath($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], $this->validPathTypes, true);
return $mapping && in_array($this->getMappingType($mapping), $this->validPathTypes, true);
}

/**
Expand All @@ -127,7 +128,7 @@ public function isValidFieldForPathSource($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], $this->validPathSourceTypes, true);
return $mapping && in_array($this->getMappingType($mapping), $this->validPathSourceTypes, true);
}

/**
Expand All @@ -142,7 +143,7 @@ public function isValidFieldForPathHash($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], $this->validPathHashTypes, true);
return $mapping && in_array($this->getMappingType($mapping), $this->validPathHashTypes, true);
}

/**
Expand All @@ -157,7 +158,7 @@ public function isValidFieldForLockTime($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && ('date' === $mapping['type'] || 'datetime' === $mapping['type'] || 'timestamp' === $mapping['type']);
return $mapping && ('date' === $this->getMappingType($mapping) || 'datetime' === $this->getMappingType($mapping) || 'timestamp' === $this->getMappingType($mapping));
}

/**
Expand All @@ -172,7 +173,7 @@ public function isValidFieldForRoot($meta, $field)
{
$mapping = $meta->getFieldMapping($field);

return $mapping && in_array($mapping['type'], $this->validRootTypes, true);
return $mapping && in_array($this->getMappingType($mapping), $this->validRootTypes, true);
}

/**
Expand Down Expand Up @@ -252,4 +253,16 @@ public function validateMaterializedPathTreeMetadata($meta, array $config)
throw new InvalidMappingException('Missing properties: '.implode(', ', $missingFields)." in class - {$meta->getName()}");
}
}

/**
* @param FieldMapping|array<string, scalar> $mapping
*/
private function getMappingType($mapping): string
{
if ($mapping instanceof FieldMapping) {
return $mapping->type;
}

return $mapping['type'];
}
}
2 changes: 1 addition & 1 deletion src/Tree/Strategy/AbstractMaterializedPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function updateNode(ObjectManager $om, $node, AdapterInterface $ea)
// default behavior: if PathSource field is a string, we append the ID to the path
// path_append_id is true: always append id
// path_append_id is false: never append id
if (true === $config['path_append_id'] || ('string' === $fieldMapping['type'] && false !== $config['path_append_id'])) {
if (true === $config['path_append_id'] || ('string' === ($fieldMapping->type ?? $fieldMapping['type']) && false !== $config['path_append_id'])) {
if (method_exists($meta, 'getIdentifierValue')) {
$identifier = $meta->getIdentifierValue($node);
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/Tree/Strategy/ORM/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ protected function setLevelFieldOnPendingNodes(ObjectManager $em)
}

// Avoid type conversion performance penalty
$type = 'integer' === $mapping['type'] ? ArrayParameterType::INTEGER : ArrayParameterType::STRING;
$type = 'integer' === ($mapping->type ?? $mapping['type'])
? ArrayParameterType::INTEGER
: ArrayParameterType::STRING;

// We calculate levels for all nodes
$sql = 'SELECT c.descendant, MAX(c.depth) + 1 AS levelNum ';
Expand Down
2 changes: 1 addition & 1 deletion src/Uploadable/Mapping/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function validateField($meta, $field, $uploadableField, $validFiel

$fieldMapping = $meta->getFieldMapping($field);

if (!in_array($fieldMapping['type'], $validFieldTypes, true)) {
if (!in_array($fieldMapping->type ?? $fieldMapping['type'], $validFieldTypes, true)) {
$msg = 'Field "%s" to work as an "%s" field must be of one of the following types: "%s".';

throw new InvalidMappingException(sprintf($msg, $field, $uploadableField, implode(', ', $validFieldTypes)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function readExtendedMetadata($meta, array &$config)
// validate encoding type
$mapping = $meta->getFieldMapping($field);

if ('string' !== $mapping['type']) {
if ('string' !== ($mapping->type ?? $mapping['type'])) {
throw new \Exception('Only strings can be encoded');
}

Expand Down