From e32185815803f39c1bcf9dfecfb20007c98b6ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Sun, 5 Jan 2025 01:09:55 +0100 Subject: [PATCH] MappedObjectRule: minor refactoring --- src/Rules/MappedObjectRule.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Rules/MappedObjectRule.php b/src/Rules/MappedObjectRule.php index 563fa907..cd2b80b9 100644 --- a/src/Rules/MappedObjectRule.php +++ b/src/Rules/MappedObjectRule.php @@ -28,7 +28,7 @@ final class MappedObjectRule implements Rule public const ClassName = 'class'; /** @var array */ - private array $alreadyResolved = []; + private array $resolvedClasses = []; public function resolveArgs(array $args, ArgsFieldContext $context): MappedObjectArgs { @@ -40,13 +40,13 @@ public function resolveArgs(array $args, ArgsFieldContext $context): MappedObjec $type = $checker->checkString(self::ClassName); // Load object to ensure whole hierarchy is valid even if not used - if (!array_key_exists($type, $this->alreadyResolved)) { - $this->alreadyResolved[$type] = null; + if (!array_key_exists($type, $this->resolvedClasses)) { + $this->resolvedClasses[$type] = null; try { /** @phpstan-ignore-next-line Meta loader validates type */ $context->getMetaLoader()->load($type); } catch (Throwable $e) { - unset($this->alreadyResolved[$type]); + unset($this->resolvedClasses[$type]); throw $e; } @@ -63,7 +63,7 @@ public function getArgsType(): string } /** - * @param mixed $value + * @param mixed $value * @param MappedObjectArgs $args * @return MappedObject|array * @throws InvalidData @@ -81,11 +81,12 @@ public function processValue($value, Args $args, FieldContext $context) public function createType(Args $args, TypeContext $context): MappedObjectType { + $type = new MappedObjectType($args->class); + if (in_array($args->class, $context->getProcessedClasses(), true)) { - return new MappedObjectType($args->class); + return $type; } - $type = new MappedObjectType($args->class); foreach ($context->getMeta($args->class)->getFields() as $fieldName => $fieldMeta) { $type->addField( $fieldName,