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 9 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 @@
{
$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);

Check warning on line 131 in src/Blameable/Mapping/Driver/Xml.php

View check run for this annotation

Codecov / codecov/patch

src/Blameable/Mapping/Driver/Xml.php#L131

Added line #L131 was not covered by tests
}
}
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 @@
{
$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);

Check warning on line 137 in src/Blameable/Mapping/Driver/Yaml.php

View check run for this annotation

Codecov / codecov/patch

src/Blameable/Mapping/Driver/Yaml.php#L137

Added line #L137 was not covered by tests
}
}
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 @@
{
$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);

Check warning on line 133 in src/IpTraceable/Mapping/Driver/Xml.php

View check run for this annotation

Codecov / codecov/patch

src/IpTraceable/Mapping/Driver/Xml.php#L133

Added line #L133 was not covered by tests
}
}
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 @@
{
$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);

Check warning on line 133 in src/IpTraceable/Mapping/Driver/Yaml.php

View check run for this annotation

Codecov / codecov/patch

src/IpTraceable/Mapping/Driver/Yaml.php#L133

Added line #L133 was not covered by tests
}
}
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 @@
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']));

Check warning on line 94 in src/ReferenceIntegrity/ReferenceIntegrityListener.php

View check run for this annotation

Codecov / codecov/patch

src/ReferenceIntegrity/ReferenceIntegrityListener.php#L94

Added line #L94 was not covered by tests
}

$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 @@
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']));

Check warning on line 120 in src/ReferenceIntegrity/ReferenceIntegrityListener.php

View check run for this annotation

Codecov / codecov/patch

src/ReferenceIntegrity/ReferenceIntegrityListener.php#L120

Added line #L120 was not covered by tests
}

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']));

Check warning on line 124 in src/ReferenceIntegrity/ReferenceIntegrityListener.php

View check run for this annotation

Codecov / codecov/patch

src/ReferenceIntegrity/ReferenceIntegrityListener.php#L124

Added line #L124 was not covered by tests
}

$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 @@
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
10 changes: 5 additions & 5 deletions src/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,11 @@ 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']);
if (isset($mapping['length']) && strlen($slug) > ($mapping->length ?? $mapping['length'])) {
$slug = substr($slug, 0, $mapping->length ?? $mapping['length']);
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
}

if (isset($mapping['nullable']) && $mapping['nullable'] && 0 === strlen($slug)) {
if (isset($mapping['nullable']) && ($mapping->nullable ?? $mapping['nullable']) && 0 === strlen($slug)) {
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
$slug = null;
}

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

$mapping = $meta->getFieldMapping($config['slug']);
if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) {
if (isset($mapping['length']) && strlen($generatedSlug) > ($mapping->length ?? $mapping['length'])) {
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
$generatedSlug = substr(
$generatedSlug,
0,
$mapping['length'] - (strlen($uniqueSuffix) + strlen($config['separator']))
($mapping->length ?? $mapping['length']) - (strlen($uniqueSuffix) + strlen($config['separator']))
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
);
$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 @@

$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()));

Check warning on line 56 in src/SoftDeleteable/Mapping/Validator.php

View check run for this annotation

Codecov / codecov/patch

src/SoftDeleteable/Mapping/Validator.php#L56

Added line #L56 was not covered by tests
}
}
}
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