Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RefuseToMapException, thrown by property mapper if it needs to leave the target property unmapped #258

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="php-cs-fixer" version="^3.64.0" installed="3.64.0" location="./tools/php-cs-fixer" copy="false"/>
<phar name="php-cs-fixer" version="^3.68.0" installed="3.68.0" location="./tools/php-cs-fixer" copy="false"/>
</phive>
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# CHANGELOG

## 1.13.6
## 1.14.0

* chore: rector run
* fix: check file existence before unlinking, suppresses warning
* feat: `RefuseToMapException`, thrown by property mapper if it needs to leave
the target property unmapped

## 1.13.5

Expand Down
16 changes: 16 additions & 0 deletions src/Exception/RefuseToMapException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Exception;

class RefuseToMapException extends \RuntimeException implements ExceptionInterface {}
2 changes: 1 addition & 1 deletion src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function __construct(
private readonly ?NormalizerInterface $normalizer = null,
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly CacheItemPoolInterface $propertyInfoExtractorCache = new ArrayAdapter(),
private readonly LoggerInterface $logger = new NullLogger()
private readonly LoggerInterface $logger = new NullLogger(),
) {}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Transformer/Processor/ObjectProcessor/ObjectProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rekalogika\Mapper\Context\ExtraTargetValues;
use Rekalogika\Mapper\Context\MapperOptions;
use Rekalogika\Mapper\Exception\ExceptionInterface;
use Rekalogika\Mapper\Exception\RefuseToMapException;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\MainTransformer\MainTransformerInterface;
use Rekalogika\Mapper\ObjectCache\ObjectCache;
Expand Down Expand Up @@ -418,7 +419,7 @@ private function generateConstructorArguments(
$constructorArguments->addUnsetSourceProperty($sourceProperty);

continue;
} catch (UnsupportedPropertyMappingException) {
} catch (UnsupportedPropertyMappingException | RefuseToMapException) {
continue;
}
}
Expand Down Expand Up @@ -581,7 +582,7 @@ private function readSourcePropertyAndWriteTargetProperty(
mandatory: false,
context: $context,
);
} catch (UninitializedSourcePropertyException | UnsupportedPropertyMappingException) {
} catch (UninitializedSourcePropertyException | UnsupportedPropertyMappingException | RefuseToMapException) {
return $target;
}

Expand Down
26 changes: 16 additions & 10 deletions tests/config/rekalogika-mapper/generated-mappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,65 +785,71 @@
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 244
// tests/src/IntegrationTest/PropertyMappingTest.php on line 246
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\Bar::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\Baz::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 199
// tests/src/IntegrationTest/PropertyMappingTest.php on line 201
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ChildOfSomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ChildOfSomeObjectDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 182
// tests/src/IntegrationTest/PropertyMappingTest.php on line 184
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ChildOfSomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 241
// tests/src/IntegrationTest/PropertyMappingTest.php on line 243
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\Foo::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\Baz::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 216
// tests/src/IntegrationTest/PropertyMappingTest.php on line 218
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ChildOfSomeObjectDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 271
// tests/src/IntegrationTest/PropertyMappingTest.php on line 273
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ObjectWithChild1::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 284
// tests/src/IntegrationTest/PropertyMappingTest.php on line 286
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\ObjectWithChild2::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 165
// tests/src/IntegrationTest/PropertyMappingTest.php on line 167
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 233
// tests/src/IntegrationTest/PropertyMappingTest.php on line 235
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithConstructorDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 297
// tests/src/IntegrationTest/PropertyMappingTest.php on line 299
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObject::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithDateTimeImmutableDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/PropertyMappingTest.php on line 309
source: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariable::class,
target: \Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariableDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/ReadOnlyTest.php on line 27
source: \Rekalogika\Mapper\Tests\Fixtures\ReadOnly\Source::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyMapper;

/** @psalm-suppress MissingConstructor */
class SomeObjectWithUninitializedVariable
{
public string $propertyA;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\PropertyMapper;

/** @psalm-suppress MissingConstructor */
class SomeObjectWithUninitializedVariableDto
{
public string $propertyA;
}
10 changes: 10 additions & 0 deletions tests/src/IntegrationTest/PropertyMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectDto;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithConstructorDto;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithDateTimeImmutableDto;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariable;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariableDto;
use Rekalogika\Mapper\Tests\Services\PropertyMapper\PropertyMapperWithClassAttribute;
use Rekalogika\Mapper\Tests\Services\PropertyMapper\PropertyMapperWithClassAttributeWithoutExplicitProperty;
use Rekalogika\Mapper\Tests\Services\PropertyMapper\PropertyMapperWithConstructorWithClassAttribute;
Expand Down Expand Up @@ -298,6 +300,14 @@ public function testExistingUnalterableObject(): void

$this->assertInstanceOf(SomeObjectWithDateTimeImmutableDto::class, $result);
$this->assertNotSame($originalTargetValue, $result->getProperty());
}

public function testFromUninitializedVariable(): void
{
$source = new SomeObjectWithUninitializedVariable();
$target = new SomeObjectWithUninitializedVariableDto();
$result = $this->mapper->map($source, $target);

$this->assertFalse(isset($result->propertyA));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Services\PropertyMapper;

use Rekalogika\Mapper\Attribute\AsPropertyMapper;
use Rekalogika\Mapper\Exception\RefuseToMapException;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariable;
use Rekalogika\Mapper\Tests\Fixtures\PropertyMapper\SomeObjectWithUninitializedVariableDto;

#[AsPropertyMapper(
targetClass: SomeObjectWithUninitializedVariableDto::class,
)]
class PropertyMapperFromUnitializedVariable
{
#[AsPropertyMapper('propertyA')]
public function mapPropertyA(SomeObjectWithUninitializedVariable $object): string
{
try {
return $object->propertyA;
} catch (\Error $e) { // @phpstan-ignore-line
if (str_contains($e->getMessage(), 'must not be accessed before initialization')) {
throw new RefuseToMapException();
}

throw $e;
}
}
}
Loading