Skip to content

Commit 2ac0cfc

Browse files
authored
Merge pull request #13 from veewee/speed-up-property-name-calcualtion
Speed up property name calculation
2 parents c6c5817 + aec667d commit 2ac0cfc

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/Encoder/ObjectEncoder.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use VeeWee\Reflecta\Lens\Lens;
2121
use function is_array;
2222
use function Psl\Dict\map;
23-
use function Psl\Dict\pull;
23+
use function Psl\Dict\map_with_key;
2424
use function Psl\Dict\reindex;
2525
use function Psl\Iter\any;
2626
use function Psl\Vec\sort_by;
@@ -102,16 +102,16 @@ private function to(Context $context, array $properties, object|array $data): st
102102
XsiTypeDetector::detectFromValue($context, []),
103103
includeXsiTargetNamespace: !$isAnyPropertyQualified,
104104
)),
105-
...map(
105+
...map_with_key(
106106
$properties,
107-
function (Property $property) use ($context, $data, $defaultAction) : Closure {
107+
function (string $normalizePropertyName, Property $property) use ($context, $data, $defaultAction) : Closure {
108108
$type = $property->getType();
109109
$meta = $type->getMeta();
110110
$isAttribute = $meta->isAttribute()->unwrapOr(false);
111111

112112
/** @var mixed $value */
113113
$value = $this->runLens(
114-
property(PhpPropertyNameNormalizer::normalize($property->getName())),
114+
property($normalizePropertyName),
115115
$meta,
116116
$data,
117117
null
@@ -146,7 +146,7 @@ private function from(Context $context, array $properties, Element $data): objec
146146
$objectData = object_data($this->className);
147147

148148
return $objectData->from(
149-
pull(
149+
map(
150150
$properties,
151151
function (Property $property) use ($context, $nodes): mixed {
152152
$type = $property->getType();
@@ -167,7 +167,6 @@ function (Property $property) use ($context, $nodes): mixed {
167167
default => $value !== null ? $this->grabIsoForProperty($context, $property)->from($value) : $defaultValue,
168168
};
169169
},
170-
static fn (Property $property) => PhpPropertyNameNormalizer::normalize($property->getName()),
171170
)
172171
);
173172
}
@@ -178,9 +177,9 @@ function (Property $property) use ($context, $nodes): mixed {
178177
private function grabIsoForProperty(Context $context, Property $property): Iso
179178
{
180179
$propertyContext = $context->withType($property->getType());
181-
$encoder = $context->registry->detectEncoderForContext($propertyContext);
182180

183-
return $encoder->iso($propertyContext);
181+
return $context->registry->detectEncoderForContext($propertyContext)
182+
->iso($propertyContext);
184183
}
185184

186185
private function runLens(Lens $lens, TypeMeta $meta, mixed $data, mixed $default): mixed
@@ -229,7 +228,7 @@ private function detectProperties(Context $context): array
229228
$type->getProperties(),
230229
static fn (Property $property): bool => !$property->getType()->getMeta()->isAttribute()->unwrapOr(false),
231230
),
232-
static fn (Property $property): string => $property->getName(),
231+
static fn (Property $property): string => PhpPropertyNameNormalizer::normalize($property->getName()),
233232
);
234233
}
235234
}

tests/Unit/Encoder/ErrorHandlingEncoderTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public function it_can_fail_decoding(): void
6666
$res = $encoder->iso($context)->from(
6767
'<user><active>true</active><hat><color>not-a-float</color></hat></user>'
6868
);
69-
70-
dd($res);
7169
} catch (EncodingException $e) {
7270
static::assertSame('Failed decoding type string as {https://test:user}. Failed at path "user.hat.color".', $e->getMessage());
7371
static::assertSame(['user', 'hat', 'color'], $e->getPaths());

0 commit comments

Comments
 (0)