Skip to content

Commit

Permalink
fix: turn NewInstanceReturnedButCannotBeSetOnTargetException into a…
Browse files Browse the repository at this point in the history
… warning
  • Loading branch information
priyadi committed Oct 23, 2024
1 parent 006c984 commit 8e769a8
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 371 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 @@

* feat: `Map` now accepts false, for the more expressive `#[Map(false)]`
* refactor: remove property processor for easier future optimization
* fix: turn `NewInstanceReturnedButCannotBeSetOnTargetException` into a warning

## 1.13.1

Expand Down
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
'$subMapperFactory' => service('rekalogika.mapper.sub_mapper.factory'),
'$proxyFactory' => service('rekalogika.mapper.proxy.factory'),
'$propertyAccessor' => service(PropertyAccessorInterface::class),
'$logger' => service(LoggerInterface::class),
]);

# sub mapper
Expand Down
10 changes: 9 additions & 1 deletion src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Ramsey\Uuid\UuidInterface;
use Rekalogika\Mapper\Command\MappingCommand;
use Rekalogika\Mapper\Command\TryCommand;
Expand Down Expand Up @@ -209,6 +211,8 @@ class MapperFactory

private ?Application $application = null;

private LoggerInterface $logger;

/**
* @param array<string,TransformerInterface> $additionalTransformers
*/
Expand All @@ -219,7 +223,10 @@ public function __construct(
private readonly ?NormalizerInterface $normalizer = null,
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly CacheItemPoolInterface $propertyInfoExtractorCache = new ArrayAdapter(),
) {}
?LoggerInterface $logger = null,
) {
$this->logger = $logger ?? new NullLogger();
}

/**
* @param class-string $sourceClass
Expand Down Expand Up @@ -907,6 +914,7 @@ protected function getObjectProcessorFactory(): ObjectProcessorFactoryInterface
subMapperFactory: $this->getSubMapperFactory(),
proxyFactory: $this->getProxyFactory(),
propertyAccessor: $this->getPropertyAccessor(),
logger: $this->logger,
);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Mapper\Transformer\Processor\ObjectProcessor;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Rekalogika\Mapper\Proxy\ProxyFactoryInterface;
use Rekalogika\Mapper\SubMapper\SubMapperFactoryInterface;
use Rekalogika\Mapper\Transformer\MainTransformerAwareTrait;
Expand All @@ -34,6 +35,7 @@ public function __construct(
private readonly SubMapperFactoryInterface $subMapperFactory,
private readonly ProxyFactoryInterface $proxyFactory,
private readonly PropertyAccessorInterface $propertyAccessor,
private readonly LoggerInterface $logger,
) {}

public function getObjectProcessor(
Expand All @@ -46,6 +48,7 @@ public function getObjectProcessor(
subMapperFactory: $this->subMapperFactory,
proxyFactory: $this->proxyFactory,
propertyAccessor: $this->propertyAccessor,
logger: $this->logger,
);
}
}
19 changes: 11 additions & 8 deletions src/Transformer/Processor/ObjectProcessor/ObjectProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Mapper\Transformer\Processor\ObjectProcessor;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Context\ExtraTargetValues;
use Rekalogika\Mapper\Context\MapperOptions;
Expand All @@ -26,7 +27,6 @@
use Rekalogika\Mapper\Transformer\Exception\ClassNotInstantiableException;
use Rekalogika\Mapper\Transformer\Exception\ExtraTargetPropertyNotFoundException;
use Rekalogika\Mapper\Transformer\Exception\InstantiationFailureException;
use Rekalogika\Mapper\Transformer\Exception\NewInstanceReturnedButCannotBeSetOnTargetException;
use Rekalogika\Mapper\Transformer\Exception\UnableToReadException;
use Rekalogika\Mapper\Transformer\Exception\UnableToWriteException;
use Rekalogika\Mapper\Transformer\Exception\UninitializedSourcePropertyException;
Expand Down Expand Up @@ -56,6 +56,7 @@ public function __construct(
private SubMapperFactoryInterface $subMapperFactory,
private ProxyFactoryInterface $proxyFactory,
private PropertyAccessorInterface $propertyAccessor,
private LoggerInterface $logger,
) {}

public function transform(
Expand Down Expand Up @@ -989,15 +990,17 @@ public function writeTargetProperty(
$visibility !== Visibility::Public
|| $writeMode === WriteMode::None
) {
if ($silentOnError) {
return $target;
if (!$silentOnError) {
$this->logger->warning(
'Transformation of property "{property}" on target class "{class}" results in a different object instance from the original instance, but the new instance cannot be set on the target object. To fix the problem, you may 1. make the property public, 2. add a setter method for the property, or 3. add "#[Map(false)]" attribute on the property to skip the mapping.',
[
'property' => $metadata->getTargetProperty(),
'class' => get_debug_type($target),
],
);
}

throw new NewInstanceReturnedButCannotBeSetOnTargetException(
target: $target,
propertyName: $metadata->getTargetProperty(),
context: $context,
);
return $target;
}

if ($accessorName === null) {
Expand Down
Loading

0 comments on commit 8e769a8

Please sign in to comment.