Skip to content

Commit

Permalink
MappedObjectRule: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 5, 2025
1 parent 72e2620 commit e321858
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Rules/MappedObjectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class MappedObjectRule implements Rule
public const ClassName = 'class';

/** @var array<string, null> */
private array $alreadyResolved = [];
private array $resolvedClasses = [];

public function resolveArgs(array $args, ArgsFieldContext $context): MappedObjectArgs
{
Expand All @@ -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;
}
Expand All @@ -63,7 +63,7 @@ public function getArgsType(): string
}

/**
* @param mixed $value
* @param mixed $value
* @param MappedObjectArgs $args
* @return MappedObject|array<mixed>
* @throws InvalidData
Expand All @@ -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,
Expand Down

0 comments on commit e321858

Please sign in to comment.