From eab102440cdd767336d723cdaf0a361a91b80e38 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 15 Jan 2024 13:07:01 +0700 Subject: [PATCH] fix(`MainTransformer`): Fix possible bug involving an existing target. --- CHANGELOG.md | 1 + src/MainTransformer/MainTransformer.php | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7014cc..1cee8ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * test: Mapping test first version. * refactor: rename `RefuseToHandleException` to `RefuseToTransformException`. * perf(`CopyTransformer`): Move identity check to the beginning. +* fix(`MainTransformer`): Fix possible bug involving an existing target. ## 0.5.10 diff --git a/src/MainTransformer/MainTransformer.php b/src/MainTransformer/MainTransformer.php index de7cc6d..d9f8e51 100644 --- a/src/MainTransformer/MainTransformer.php +++ b/src/MainTransformer/MainTransformer.php @@ -76,14 +76,15 @@ public function transform( Context $context, string $path = null, ): mixed { - // if targettype is not provided, guess it from target - // if the target is also missing then the target is mixed - - if (count($targetTypes) === 0) { - if ($target === null) { + // if target is provided, guess the type from it. + // if target is not provided, use the provided target type. if it is + // also not provided, then the target type is mixed. + + if ($target !== null) { + $targetTypes = [$this->typeResolver->guessTypeFromVariable($target)]; + } else { + if (count($targetTypes) === 0) { $targetTypes = [MixedType::instance()]; - } else { - $targetTypes = [$this->typeResolver->guessTypeFromVariable($target)]; } }