Skip to content

Commit

Permalink
FieldContext: Replace redundant Type lazy getter with one from the Ca…
Browse files Browse the repository at this point in the history
…llContext
  • Loading branch information
mabar committed Feb 19, 2025
1 parent 8b6561c commit 575b4aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 8 additions & 16 deletions src/Callbacks/Context/FieldContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,36 +14,27 @@ final class FieldContext extends CallbackBaseContext

private PropertyContext $property;

/** @var Closure(): Type */
private Closure $typeCreator;

private ?Type $type = null;
/** @var ProcessorCallContext<MappedObject> */
private ProcessorCallContext $call;

/**
* @param Closure(): Type $typeCreator
* @param ProcessorCallContext<MappedObject> $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
Expand Down
2 changes: 1 addition & 1 deletion src/Processing/DefaultProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 575b4aa

Please sign in to comment.