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

feat: improve object constructors parameters types inferring #552

Merged
merged 3 commits into from
Sep 2, 2024
Merged
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
29 changes: 11 additions & 18 deletions src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use CuyZ\Valinor\Definition\Repository\Reflection\ReflectionFunctionDefinitionRepository;
use CuyZ\Valinor\Mapper\ArgumentsMapper;
use CuyZ\Valinor\Mapper\Object\Factory\CacheObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\CollisionObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\SortingObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\ConstructorObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\DateTimeObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\DateTimeZoneObjectBuilderFactory;
Expand All @@ -37,7 +37,6 @@
use CuyZ\Valinor\Mapper\Tree\Builder\NodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\NullNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ObjectImplementations;
use CuyZ\Valinor\Mapper\Tree\Builder\FilteredObjectNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ScalarNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ShapedArrayNodeBuilder;
Expand Down Expand Up @@ -86,36 +85,36 @@ public function __construct(Settings $settings)
$this->factories = [
TreeMapper::class => fn () => new TypeTreeMapper(
$this->get(TypeParser::class),
$this->get(RootNodeBuilder::class)
$this->get(RootNodeBuilder::class),
$settings,
),

ArgumentsMapper::class => fn () => new TypeArgumentsMapper(
$this->get(FunctionDefinitionRepository::class),
$this->get(RootNodeBuilder::class)
$this->get(RootNodeBuilder::class),
$settings,
),

RootNodeBuilder::class => fn () => new RootNodeBuilder(
$this->get(NodeBuilder::class)
),

NodeBuilder::class => function () use ($settings) {
$listNodeBuilder = new ListNodeBuilder($settings->enableFlexibleCasting);
$arrayNodeBuilder = new ArrayNodeBuilder($settings->enableFlexibleCasting);
$listNodeBuilder = new ListNodeBuilder();
$arrayNodeBuilder = new ArrayNodeBuilder();

$builder = new CasterNodeBuilder([
ListType::class => $listNodeBuilder,
NonEmptyListType::class => $listNodeBuilder,
ArrayType::class => $arrayNodeBuilder,
NonEmptyArrayType::class => $arrayNodeBuilder,
IterableType::class => $arrayNodeBuilder,
ShapedArrayType::class => new ShapedArrayNodeBuilder($settings->allowSuperfluousKeys),
ScalarType::class => new ScalarNodeBuilder($settings->enableFlexibleCasting),
ShapedArrayType::class => new ShapedArrayNodeBuilder(),
ScalarType::class => new ScalarNodeBuilder(),
NullType::class => new NullNodeBuilder(),
ObjectType::class => new ObjectNodeBuilder(
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(FilteredObjectNodeBuilder::class),
$settings->enableFlexibleCasting,
),
]);

Expand All @@ -125,14 +124,10 @@ public function __construct(Settings $settings)
$builder,
$this->get(ObjectImplementations::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(FilteredObjectNodeBuilder::class),
new FunctionsContainer(
$this->get(FunctionDefinitionRepository::class),
$settings->customConstructors
),
$settings->enableFlexibleCasting,
$settings->allowSuperfluousKeys,
);

$builder = new CasterProxyNodeBuilder($builder);
Expand All @@ -148,13 +143,11 @@ public function __construct(Settings $settings)
);
}

$builder = new StrictNodeBuilder($builder, $settings->allowPermissiveTypes, $settings->enableFlexibleCasting);
$builder = new StrictNodeBuilder($builder);

return new ErrorCatcherNodeBuilder($builder, $settings->exceptionFilter);
},

FilteredObjectNodeBuilder::class => fn () => new FilteredObjectNodeBuilder($settings->allowSuperfluousKeys),

ObjectImplementations::class => fn () => new ObjectImplementations(
new FunctionsContainer(
$this->get(FunctionDefinitionRepository::class),
Expand All @@ -173,7 +166,7 @@ public function __construct(Settings $settings)
$factory = new ConstructorObjectBuilderFactory($factory, $settings->nativeConstructors, $constructors);
$factory = new DateTimeZoneObjectBuilderFactory($factory, $this->get(FunctionDefinitionRepository::class));
$factory = new DateTimeObjectBuilderFactory($factory, $settings->supportedDateFormats, $this->get(FunctionDefinitionRepository::class));
$factory = new CollisionObjectBuilderFactory($factory);
$factory = new SortingObjectBuilderFactory($factory);

if (! $settings->allowPermissiveTypes) {
$factory = new StrictTypesObjectBuilderFactory($factory);
Expand Down
35 changes: 22 additions & 13 deletions src/Mapper/Object/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use IteratorAggregate;
use Traversable;

use function array_keys;
use function array_map;
use function array_values;
use function count;

/**
* @internal
Expand All @@ -22,44 +24,51 @@
*/
final class Arguments implements IteratorAggregate, Countable
{
/** @var Argument[] */
private array $arguments;
/** @var array<string, Argument> */
private array $arguments = [];

public function __construct(Argument ...$arguments)
{
$this->arguments = $arguments;
foreach ($arguments as $argument) {
$this->arguments[$argument->name()] = $argument;
}
}

public static function fromParameters(Parameters $parameters): self
{
return new self(...array_map(
fn (ParameterDefinition $parameter) => Argument::fromParameter($parameter),
array_values([...$parameters])
[...$parameters],
));
}

public static function fromProperties(Properties $properties): self
{
return new self(...array_map(
fn (PropertyDefinition $property) => Argument::fromProperty($property),
array_values([...$properties])
[...$properties],
));
}

public function at(int $index): Argument
{
return $this->arguments[$index];
return array_values($this->arguments)[$index];
}

public function has(string $name): bool
/**
* @return list<string>
*/
public function names(): array
{
foreach ($this->arguments as $argument) {
if ($argument->name() === $name) {
return true;
}
}
return array_keys($this->arguments);
}

return false;
/**
* @return array<string, Argument>
*/
public function toArray(): array
{
return $this->arguments;
}

public function count(): int
Expand Down
53 changes: 24 additions & 29 deletions src/Mapper/Object/ArgumentsValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

namespace CuyZ\Valinor\Mapper\Object;

use CuyZ\Valinor\Mapper\Object\Exception\InvalidSource;
use CuyZ\Valinor\Mapper\Tree\Shell;
use CuyZ\Valinor\Type\CompositeTraversableType;
use CuyZ\Valinor\Type\Types\ArrayKeyType;
use IteratorAggregate;
use Traversable;

use function array_filter;
use function array_key_exists;
use function count;
use function is_array;
Expand All @@ -27,6 +26,8 @@ final class ArgumentsValues implements IteratorAggregate

private Arguments $arguments;

private bool $hasInvalidValue = false;

private bool $forInterface = false;

private bool $hadSingleArgument = false;
Expand All @@ -36,26 +37,31 @@ private function __construct(Arguments $arguments)
$this->arguments = $arguments;
}

public static function forInterface(Arguments $arguments, mixed $value, bool $allowSuperfluousKeys): self
public static function forInterface(Arguments $arguments, Shell $shell): self
{
$self = new self($arguments);
$self->forInterface = true;

if (count($arguments) > 0) {
$self = $self->transform($value, $allowSuperfluousKeys);
$self->transform($shell);
}

return $self;
}

public static function forClass(Arguments $arguments, mixed $value, bool $allowSuperfluousKeys): self
public static function forClass(Arguments $arguments, Shell $shell): self
{
$self = new self($arguments);
$self = $self->transform($value, $allowSuperfluousKeys);
$self->transform($shell);

return $self;
}

public function hasInvalidValue(): bool
{
return $this->hasInvalidValue;
}

public function hasValue(string $name): bool
{
return array_key_exists($name, $this->value);
Expand All @@ -66,34 +72,23 @@ public function getValue(string $name): mixed
return $this->value[$name];
}

/**
* @return array<string>
*/
public function superfluousKeys(): array
{
return array_filter(
array_keys($this->value),
fn ($key) => ! $this->arguments->has((string)$key)
);
}

public function hadSingleArgument(): bool
{
return $this->hadSingleArgument;
}

private function transform(mixed $value, bool $allowSuperfluousKeys): self
private function transform(Shell $shell): void
{
$clone = clone $this;

$transformedValue = $this->transformValueForSingleArgument($value, $allowSuperfluousKeys);
$transformedValue = $this->transformValueForSingleArgument($shell);

if (! is_array($transformedValue)) {
throw new InvalidSource($transformedValue, $this->arguments);
$this->hasInvalidValue = true;

return;
}

if ($transformedValue !== $value) {
$clone->hadSingleArgument = true;
if ($transformedValue !== $shell->value()) {
$this->hadSingleArgument = true;
}

foreach ($this->arguments as $argument) {
Expand All @@ -104,13 +99,13 @@ private function transform(mixed $value, bool $allowSuperfluousKeys): self
}
}

$clone->value = $transformedValue;

return $clone;
$this->value = $transformedValue;
}

private function transformValueForSingleArgument(mixed $value, bool $allowSuperfluousKeys): mixed
private function transformValueForSingleArgument(Shell $shell): mixed
{
$value = $shell->value();

if (count($this->arguments) !== 1) {
return $value;
}
Expand All @@ -122,7 +117,7 @@ private function transformValueForSingleArgument(mixed $value, bool $allowSuperf
&& $type->keyType() !== ArrayKeyType::integer();

if (is_array($value) && array_key_exists($name, $value)) {
if ($this->forInterface || ! $isTraversableAndAllowsStringKeys || $allowSuperfluousKeys || count($value) === 1) {
if ($this->forInterface || ! $isTraversableAndAllowsStringKeys || $shell->allowSuperfluousKeys() || count($value) === 1) {
return $value;
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/Mapper/Object/Exception/ObjectBuildersCollision.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@

namespace CuyZ\Valinor\Mapper\Object\Exception;

use CuyZ\Valinor\Definition\ClassDefinition;
use CuyZ\Valinor\Mapper\Object\ObjectBuilder;
use RuntimeException;

use function array_map;
use function implode;

/** @internal */
final class ObjectBuildersCollision extends RuntimeException
{
public function __construct(ClassDefinition $class, ObjectBuilder ...$builders)
public function __construct(ObjectBuilder $builderA, ObjectBuilder $builderB)
{
$constructors = array_map(fn (ObjectBuilder $builder) => $builder->signature(), $builders);
$constructors = implode('`, `', $constructors);

parent::__construct(
"A collision was detected between the following constructors of the class `{$class->type->toString()}`: `$constructors`.",
"A type collision was detected between the constructors `{$builderA->signature()}` and `{$builderB->signature()}`.",
1654955787
);
}
Expand Down
24 changes: 0 additions & 24 deletions src/Mapper/Object/Exception/SeveralObjectBuildersFound.php

This file was deleted.

Loading
Loading