Skip to content

Commit

Permalink
fix: no longer throws exception if the target has no setter (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Oct 1, 2024
1 parent 0956aaf commit 6020855
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* style: remove unused immutability check
* refactor: separate caching
* fix: no longer throws exception if the target has no setter

## 1.9.1

Expand Down
15 changes: 11 additions & 4 deletions src/Transformer/Util/ReaderWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Rekalogika\Mapper\Transformer\Util;

use Psr\Log\LoggerInterface;
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\Transformer\Exception\NewInstanceReturnedButCannotBeSetOnTargetException;
Expand All @@ -28,11 +29,13 @@

/**
* @internal
* @todo inject logger
*/
final readonly class ReaderWriter
{
public function __construct(
private PropertyAccessorInterface $propertyAccessor,
private ?LoggerInterface $logger = null,
) {}

/**
Expand Down Expand Up @@ -207,11 +210,15 @@ public function writeTargetProperty(
if (
$visibility !== Visibility::Public || $writeMode === WriteMode::None
) {
throw new NewInstanceReturnedButCannotBeSetOnTargetException(
$target,
$propertyMapping->getTargetProperty(),
context: $context,
$this->logger?->debug(
'Transformation of property {property} on object type {object_type} results in a different object instance from the original instance, but the new instance cannot be set on the target object. You may wish to add a setter method to the target class.',
[
'property' => $propertyMapping->getTargetProperty(),
'object_type' => get_debug_type($target),
],
);

return $target;
}

if ($accessorName === null) {
Expand Down
5 changes: 4 additions & 1 deletion tests/src/IntegrationTest/WitherMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ public function testWither(): void
$this->assertNotSame($originalObject, $resultObject);
}

/**
* @todo no longer throws exception, test the warning instead
*/
public function testChildImmutableSetterWithoutSetterOnParent(): void
{
$this->expectException(NewInstanceReturnedButCannotBeSetOnTargetException::class);
// $this->expectException(NewInstanceReturnedButCannotBeSetOnTargetException::class);

$source = new ParentObject();
$target = new ParentObjectWithoutSetterDto();
Expand Down

0 comments on commit 6020855

Please sign in to comment.