From 575b4aa6ead463d0d4244e0b02ba6abedb4d7ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Wed, 19 Feb 2025 21:12:00 +0100 Subject: [PATCH] FieldContext: Replace redundant Type lazy getter with one from the CallContext --- CHANGELOG.md | 2 ++ src/Callbacks/Context/FieldContext.php | 24 ++++++++---------------- src/Processing/DefaultProcessor.php | 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 841e383..6a90356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `castEmptyString` option trims empty characters from start and end of the string - `StringRule` - `notEmpty` detects more empty values +- `FieldContext` + - Replace redundant `Type` lazy getter with one from the `CallContext` ### Fixed diff --git a/src/Callbacks/Context/FieldContext.php b/src/Callbacks/Context/FieldContext.php index f603625..cc7a81d 100644 --- a/src/Callbacks/Context/FieldContext.php +++ b/src/Callbacks/Context/FieldContext.php @@ -2,8 +2,9 @@ namespace Orisai\ObjectMapper\Callbacks\Context; -use Closure; +use Orisai\ObjectMapper\MappedObject; use Orisai\ObjectMapper\Processing\Context\DynamicContext; +use Orisai\ObjectMapper\Processing\Context\ProcessorCallContext; use Orisai\ObjectMapper\Processing\Context\PropertyContext; use Orisai\ObjectMapper\Processing\Context\ServicesContext; use Orisai\ObjectMapper\Types\Type; @@ -13,36 +14,27 @@ final class FieldContext extends CallbackBaseContext private PropertyContext $property; - /** @var Closure(): Type */ - private Closure $typeCreator; - - private ?Type $type = null; + /** @var ProcessorCallContext */ + private ProcessorCallContext $call; /** - * @param Closure(): Type $typeCreator + * @param ProcessorCallContext $call */ public function __construct( ServicesContext $services, DynamicContext $dynamic, PropertyContext $property, - Closure $typeCreator + ProcessorCallContext $call ) { parent::__construct($services, $dynamic); $this->property = $property; - $this->typeCreator = $typeCreator; + $this->call = $call; } public function getType(): Type { - if ($this->type !== null) { - return $this->type; - } - - $type = ($this->typeCreator)(); - unset($this->typeCreator); - - return $this->type = $type; + return $this->call->getType()->getField($this->property->getFieldName()); } public function getPropertyName(): string diff --git a/src/Processing/DefaultProcessor.php b/src/Processing/DefaultProcessor.php index 18d8a7d..8d395c4 100644 --- a/src/Processing/DefaultProcessor.php +++ b/src/Processing/DefaultProcessor.php @@ -422,7 +422,7 @@ private function processProperty( $this->services, $dynamic, $property, - static fn (): Type => $call->getType()->getField($property->getFieldName()), + $call, ); $value = $this->applyCallbacks($value, $callbackContext, $call, $meta, BeforeCallback::class);