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 @@ 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
30 changes: 15 additions & 15 deletions src/References/ReferencesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ public function postLoad(EventArgs $eventArgs)

if (isset($config['referenceOne'])) {
foreach ($config['referenceOne'] as $mapping) {
$property = $meta->reflClass->getProperty($mapping['field']);
$property = $meta->reflClass->getProperty($mapping->field ?? $mapping['field']);
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
$property->setAccessible(true);
if (isset($mapping['identifier'])) {
$referencedObjectId = $meta->getFieldValue($object, $mapping['identifier']);
$referencedObjectId = $meta->getFieldValue($object, $mapping->identifier ?? $mapping['identifier']);
if (null !== $referencedObjectId) {
$property->setValue(
$object,
$ea->getSingleReference(
$this->getManager($mapping['type']),
$mapping['class'],
$this->getManager($mapping->type ?? $mapping['type']),
$mapping->class ?? $mapping['class'],
$referencedObjectId
)
);
Expand All @@ -112,16 +112,16 @@ public function postLoad(EventArgs $eventArgs)

if (isset($config['referenceMany'])) {
foreach ($config['referenceMany'] as $mapping) {
$property = $meta->reflClass->getProperty($mapping['field']);
$property = $meta->reflClass->getProperty($mapping->field ?? $mapping['field']);
$property->setAccessible(true);
if (isset($mapping['mappedBy'])) {
$id = $ea->extractIdentifier($om, $object);
$manager = $this->getManager($mapping['type']);
$class = $mapping['class'];
$manager = $this->getManager($mapping->type ?? $mapping['type']);
$class = ($mapping->class ?? $mapping['class']);
$refMeta = $manager->getClassMetadata($class);
$refConfig = $this->getConfiguration($manager, $refMeta->getName());
if (isset($refConfig['referenceOne'][$mapping['mappedBy']])) {
$refMapping = $refConfig['referenceOne'][$mapping['mappedBy']];
if (isset($refConfig['referenceOne'][$mapping->mappedBy ?? $mapping['mappedBy']])) {
$refMapping = $refConfig['referenceOne'][$mapping->mappedBy ?? $mapping['mappedBy']];
$identifier = $refMapping['identifier'];
$property->setValue(
$object,
Expand Down Expand Up @@ -220,18 +220,18 @@ public function updateManyEmbedReferences(EventArgs $eventArgs)

if (isset($config['referenceManyEmbed'])) {
foreach ($config['referenceManyEmbed'] as $mapping) {
$property = $meta->reflClass->getProperty($mapping['field']);
$property = $meta->reflClass->getProperty($mapping->field ?? $mapping['field']);
$property->setAccessible(true);

$id = $ea->extractIdentifier($om, $object);
$manager = $this->getManager('document');

$class = $mapping['class'];
$class = ($mapping->class ?? $mapping['class']);
$refMeta = $manager->getClassMetadata($class);
// Trigger the loading of the configuration to validate the mapping
$this->getConfiguration($manager, $refMeta->getName());

$identifier = $mapping['identifier'];
$identifier = ($mapping->identifier ?? $mapping['identifier']);
$property->setValue(
$object,
new LazyCollection(
Expand Down Expand Up @@ -271,17 +271,17 @@ private function updateReferences(EventArgs $eventArgs): void
if (isset($config['referenceOne'])) {
foreach ($config['referenceOne'] as $mapping) {
if (isset($mapping['identifier'])) {
$property = $meta->reflClass->getProperty($mapping['field']);
$property = $meta->reflClass->getProperty($mapping->field ?? $mapping['field']);
$property->setAccessible(true);
$referencedObject = $property->getValue($object);

if (is_object($referencedObject)) {
$manager = $this->getManager($mapping['type']);
$manager = $this->getManager($mapping->type ?? $mapping['type']);
$identifier = $ea->getIdentifier($manager, $referencedObject);

$meta->setFieldValue(
$object,
$mapping['identifier'],
$mapping->identifier ?? $mapping['identifier'],
$identifier
);
}
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/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getDateValue($meta, $field)

return $this->getObjectManager()->getConnection()->convertToPHPValue(
$this->getRawDateValue($mapping),
$mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? Types::DATETIME_MUTABLE)
$mapping instanceof FieldMapping ? $mapping->type : ($mapping->type ?? $mapping['type'] ?? Types::DATETIME_MUTABLE)
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -58,7 +58,7 @@ public function getDateValue($meta, $field)
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping->type ?? $mapping['type'] ?? '');

if ('integer' === $type) {
return (int) $datetime->format('U');
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);
}
}
6 changes: 3 additions & 3 deletions src/Timestampable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function readExtendedMetadata($meta, array &$config)
$mapping = $this->_getMapping($meta->getName());

if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
foreach (($mapping->fields ?? $mapping['fields']) as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['timestampable'])) {
$mappingProperty = $fieldMapping['gedmo']['timestampable'];
$mappingProperty = ($fieldMapping->gedmo ?? $fieldMapping['gedmo'])['timestampable'];
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->getName()}");
}
Expand Down 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);
}
}
4 changes: 2 additions & 2 deletions src/Timestampable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getDateValue($meta, $field)

return $this->getObjectManager()->getConnection()->convertToPHPValue(
$this->getRawDateValue($mapping),
$mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? Types::DATETIME_MUTABLE)
$mapping instanceof FieldMapping ? $mapping->type : $mapping['type'] ?? Types::DATETIME_MUTABLE
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -58,7 +58,7 @@ public function getDateValue($meta, $field)
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping->type ?? $mapping['type'] ?? '');

if ('integer' === $type) {
return (int) $datetime->format('U');
Expand Down
Loading