From e87d79007c9be80276f6807947fee690c65b7c4d Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Wed, 12 Jun 2024 14:57:33 +0200 Subject: [PATCH] Move path detection to static function (less closures) --- src/Encoder/ErrorHandlingEncoder.php | 40 +++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Encoder/ErrorHandlingEncoder.php b/src/Encoder/ErrorHandlingEncoder.php index 2dca5df..95cfa41 100644 --- a/src/Encoder/ErrorHandlingEncoder.php +++ b/src/Encoder/ErrorHandlingEncoder.php @@ -30,45 +30,47 @@ public function __construct( public function iso(Context $context): Iso { $innerIso = $this->encoder->iso($context); - $buildPath = static function () use ($context): ?string { - $meta = $context->type->getMeta(); - $isElement = $meta->isElement()->unwrapOr(false); - $isAttribute = $meta->isAttribute()->unwrapOr(false); - if (!$isElement && !$isAttribute) { - return null; - } - - $path = $context->type->getXmlTargetNodeName(); - if ($isAttribute) { - return '@' . $path; - } - - return $path; - }; return new Iso( /** * @psalm-param TData $value * @psalm-return TXml */ - static function (mixed $value) use ($innerIso, $context, $buildPath): mixed { + static function (mixed $value) use ($innerIso, $context): mixed { try { return $innerIso->to($value); } catch (Throwable $exception) { - throw EncodingException::encodingValue($value, $context->type, $exception, $buildPath()); + throw EncodingException::encodingValue($value, $context->type, $exception, self::buildPath($context)); } }, /** * @psalm-param TXml $value * @psalm-return TData */ - static function (mixed $value) use ($innerIso, $context, $buildPath): mixed { + static function (mixed $value) use ($innerIso, $context): mixed { try { return $innerIso->from($value); } catch (Throwable $exception) { - throw EncodingException::decodingValue($value, $context->type, $exception, $buildPath()); + throw EncodingException::decodingValue($value, $context->type, $exception, self::buildPath($context)); } } ); } + + private static function buildPath(Context $context): ?string + { + $meta = $context->type->getMeta(); + $isElement = $meta->isElement()->unwrapOr(false); + $isAttribute = $meta->isAttribute()->unwrapOr(false); + if (!$isElement && !$isAttribute) { + return null; + } + + $path = $context->type->getXmlTargetNodeName(); + if ($isAttribute) { + return '@' . $path; + } + + return $path; + } }