Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __construct(Settings $settings)
$builder = new TypeNodeBuilder(
new ArrayNodeBuilder(),
new ListNodeBuilder(),
new ShapedArrayNodeBuilder(),
new ShapedArrayNodeBuilder($this->get(ClassDefinitionRepository::class)),
new ScalarNodeBuilder(),
new UnionNodeBuilder(),
new NullNodeBuilder(),
Expand Down
78 changes: 77 additions & 1 deletion src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

namespace CuyZ\Valinor\Mapper\Tree\Builder;

use CuyZ\Valinor\Definition\Repository\ClassDefinitionRepository;
use CuyZ\Valinor\Mapper\Tree\Exception\SourceMustBeIterable;
use CuyZ\Valinor\Mapper\Tree\Exception\UnexpectedKeyInSource;
use CuyZ\Valinor\Mapper\Tree\Shell;
use CuyZ\Valinor\Type\ObjectType;
use CuyZ\Valinor\Type\Types\ShapedArrayType;
use CuyZ\Valinor\Type\Types\UnresolvableType;

use Throwable;

use function array_diff;
use function array_diff_key;
use function array_key_exists;
use function array_keys;
use function array_values;
use function assert;
use function is_array;
use function is_iterable;
Expand All @@ -20,6 +27,9 @@
/** @internal */
final class ShapedArrayNodeBuilder implements NodeBuilder
{
public function __construct(
private ClassDefinitionRepository|null $classDefinitionRepository = null,
) {}
public function build(Shell $shell): Node
{
$type = $shell->type;
Expand All @@ -45,8 +55,11 @@
->child((string)$key, $element->type())
->withAttributes($element->attributes());

$consumedKeys = [];
if (array_key_exists($key, $value)) {
$child = $child->withValue($value[$key]);
$result = $this->resolveNestedKeys($key, $value, $element->type());
$child = $child->withValue($result['value']);
$consumedKeys = $result['consumed_keys'];
} elseif ($element->isOptional()) {
continue;
}
Expand All @@ -60,6 +73,9 @@
}

unset($value[$key]);
foreach ($consumedKeys as $consumedKey) {
unset($value[$consumedKey]);
}
}

// Second phase: if the shaped array is unsealed, we take the remaining
Expand Down Expand Up @@ -95,4 +111,64 @@

return $shell->errors($errors);
}

/**
* @param array<mixed> $parentValue
* @return array{value: mixed, consumed_keys: list<string>}
*/
private function resolveNestedKeys(int|string $key, array $parentValue, mixed $type): array
{
if (! $type instanceof ObjectType || $this->classDefinitionRepository === null) {
return ['value' => $parentValue[$key], 'consumed_keys' => []];
}

if (is_array($parentValue[$key])) {
return ['value' => $parentValue[$key], 'consumed_keys' => []];

Check warning on line 126 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ return ['value' => $parentValue[$key], 'consumed_keys' => []]; } if (is_array($parentValue[$key])) { - return ['value' => $parentValue[$key], 'consumed_keys' => []]; + } try { $classDefinition = $this->classDefinitionRepository->for($type);
}

try {
$classDefinition = $this->classDefinitionRepository->for($type);
$parentKeys = array_keys($parentValue);

Check warning on line 131 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "UnwrapArrayKeys": @@ @@ } try { $classDefinition = $this->classDefinitionRepository->for($type); - $parentKeys = array_keys($parentValue); + $parentKeys = $parentValue; foreach ($classDefinition->methods as $method) { if ($method->name !== '__construct' && !$method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) { continue;

foreach ($classDefinition->methods as $method) {
if ($method->name !== '__construct'

Check warning on line 134 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ $classDefinition = $this->classDefinitionRepository->for($type); $parentKeys = array_keys($parentValue); foreach ($classDefinition->methods as $method) { - if ($method->name !== '__construct' && !$method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) { + if (!($method->name !== '__construct') && $method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) { continue; } $paramNames = [];
&& !$method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) {

Check warning on line 135 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "LogicalNot": @@ @@ $classDefinition = $this->classDefinitionRepository->for($type); $parentKeys = array_keys($parentValue); foreach ($classDefinition->methods as $method) { - if ($method->name !== '__construct' && !$method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) { + if ($method->name !== '__construct' && $method->attributes->has(\CuyZ\Valinor\Mapper\Object\Constructor::class)) { continue; } $paramNames = [];
continue;
}

$paramNames = [];
$hasAllRequired = true;
$hasAtLeastOneParam = false;

foreach ($method->parameters as $parameter) {
$paramNames[] = $parameter->name;
if (array_key_exists($parameter->name, $parentValue)) {
$hasAtLeastOneParam = true;
}
if (!$parameter->isOptional && !array_key_exists($parameter->name, $parentValue)) {

Check warning on line 148 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ if (array_key_exists($parameter->name, $parentValue)) { $hasAtLeastOneParam = true; } - if (!$parameter->isOptional && !array_key_exists($parameter->name, $parentValue)) { + if ($parameter->isOptional && array_key_exists($parameter->name, $parentValue)) { $hasAllRequired = false; } }
$hasAllRequired = false;
}
}

if ($hasAllRequired && $hasAtLeastOneParam) {
// Extract only the keys needed for this nested object
$nestedValue = [];
foreach ($paramNames as $paramName) {
if (array_key_exists($paramName, $parentValue)) {
$nestedValue[$paramName] = $parentValue[$paramName];
}
}

return [
'value' => $nestedValue,
'consumed_keys' => array_values(array_diff($paramNames, [$key]))

Check warning on line 164 in src/Mapper/Tree/Builder/ShapedArrayNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Mutation tests

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ $nestedValue[$paramName] = $parentValue[$paramName]; } } - return ['value' => $nestedValue, 'consumed_keys' => array_values(array_diff($paramNames, [$key]))]; + return ['value' => $nestedValue, 'consumed_keys' => array_values(array_diff($paramNames, []))]; } } } catch (Throwable) {
];
}
}
} catch (Throwable) {
return ['value' => $parentValue[$key], 'consumed_keys' => []];
}

return ['value' => $parentValue[$key], 'consumed_keys' => []];
}
}
Loading
Loading