Skip to content

Commit

Permalink
Speed up property name calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Jun 14, 2024
1 parent c6c5817 commit aec667d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 8 additions & 9 deletions src/Encoder/ObjectEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use VeeWee\Reflecta\Lens\Lens;
use function is_array;
use function Psl\Dict\map;
use function Psl\Dict\pull;
use function Psl\Dict\map_with_key;
use function Psl\Dict\reindex;
use function Psl\Iter\any;
use function Psl\Vec\sort_by;
Expand Down Expand Up @@ -102,16 +102,16 @@ private function to(Context $context, array $properties, object|array $data): st
XsiTypeDetector::detectFromValue($context, []),
includeXsiTargetNamespace: !$isAnyPropertyQualified,
)),
...map(
...map_with_key(
$properties,
function (Property $property) use ($context, $data, $defaultAction) : Closure {
function (string $normalizePropertyName, Property $property) use ($context, $data, $defaultAction) : Closure {
$type = $property->getType();
$meta = $type->getMeta();
$isAttribute = $meta->isAttribute()->unwrapOr(false);

/** @var mixed $value */
$value = $this->runLens(
property(PhpPropertyNameNormalizer::normalize($property->getName())),
property($normalizePropertyName),
$meta,
$data,
null
Expand Down Expand Up @@ -146,7 +146,7 @@ private function from(Context $context, array $properties, Element $data): objec
$objectData = object_data($this->className);

return $objectData->from(
pull(
map(
$properties,
function (Property $property) use ($context, $nodes): mixed {
$type = $property->getType();
Expand All @@ -167,7 +167,6 @@ function (Property $property) use ($context, $nodes): mixed {
default => $value !== null ? $this->grabIsoForProperty($context, $property)->from($value) : $defaultValue,
};
},
static fn (Property $property) => PhpPropertyNameNormalizer::normalize($property->getName()),
)
);
}
Expand All @@ -178,9 +177,9 @@ function (Property $property) use ($context, $nodes): mixed {
private function grabIsoForProperty(Context $context, Property $property): Iso
{
$propertyContext = $context->withType($property->getType());
$encoder = $context->registry->detectEncoderForContext($propertyContext);

return $encoder->iso($propertyContext);
return $context->registry->detectEncoderForContext($propertyContext)
->iso($propertyContext);
}

private function runLens(Lens $lens, TypeMeta $meta, mixed $data, mixed $default): mixed
Expand Down Expand Up @@ -229,7 +228,7 @@ private function detectProperties(Context $context): array
$type->getProperties(),
static fn (Property $property): bool => !$property->getType()->getMeta()->isAttribute()->unwrapOr(false),
),
static fn (Property $property): string => $property->getName(),
static fn (Property $property): string => PhpPropertyNameNormalizer::normalize($property->getName()),
);
}
}
2 changes: 0 additions & 2 deletions tests/Unit/Encoder/ErrorHandlingEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function it_can_fail_decoding(): void
$res = $encoder->iso($context)->from(
'<user><active>true</active><hat><color>not-a-float</color></hat></user>'
);

dd($res);
} catch (EncodingException $e) {
static::assertSame('Failed decoding type string as {https://test:user}. Failed at path "user.hat.color".', $e->getMessage());
static::assertSame(['user', 'hat', 'color'], $e->getPaths());
Expand Down

0 comments on commit aec667d

Please sign in to comment.