Skip to content

Commit 0f83be5

Browse files
committed
chore: use memoization for arguments' conversion to shaped array
1 parent f476132 commit 0f83be5

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Mapper/Object/Arguments.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class Arguments implements IteratorAggregate, Countable
3030
/** @var array<string, Argument> */
3131
private readonly array $arguments;
3232

33+
private ShapedArrayType $shapedArray;
34+
3335
public function __construct(Argument ...$arguments)
3436
{
3537
$args = [];
@@ -78,17 +80,16 @@ public function merge(self $other): self
7880

7981
public function toShapedArray(): ShapedArrayType
8082
{
81-
$shapedArrayElements = [];
82-
foreach ($this->arguments as $i => $argument) {
83-
$shapedArrayElements[$i] = new ShapedArrayElement(
84-
key: new StringValueType($argument->name()),
85-
type: $argument->type(),
86-
optional: ! $argument->isRequired(),
87-
attributes: $argument->attributes(),
88-
);
89-
}
90-
return new ShapedArrayType(
91-
elements: $shapedArrayElements,
83+
return $this->shapedArray ??= new ShapedArrayType(
84+
elements: array_map(
85+
static fn (Argument $argument) => new ShapedArrayElement(
86+
key: new StringValueType($argument->name()),
87+
type: $argument->type(),
88+
optional: ! $argument->isRequired(),
89+
attributes: $argument->attributes(),
90+
),
91+
$this->arguments,
92+
),
9293
isUnsealed: false,
9394
unsealedType: null,
9495
);

0 commit comments

Comments
 (0)