Skip to content

Commit b9ec92f

Browse files
authored
fix: handle case where class target has adder and remover AND constructor arguments (#212)
After some attempts, I came to this solution for this issue: #211. Not sure if it's the right thing to do.
2 parents fc6005c + 95b1bcd commit b9ec92f

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99
### Fixed
1010
- [GH#207](https://github.com/jolicode/automapper/pull/207) [GH#208](https://github.com/jolicode/automapper/pull/208) Fix implicity nullable parameter deprecations
11+
- [GH#212](https://github.com/jolicode/automapper/pull/212) Fix cases where class target has adder and remover AND constructor arguments
1112

1213
## [9.2.0] - 2024-11-19
1314
### Added

src/Transformer/AbstractArrayTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
4343
/* Get the transform statements for the source property */
4444
[$output, $itemStatements] = $this->itemTransformer->transform($loopValueVar, $target, $propertyMapping, $uniqueVariableScope, $source);
4545

46-
if ($propertyMapping->target->writeMutator && $propertyMapping->target->writeMutator->type === WriteMutator::TYPE_ADDER_AND_REMOVER) {
46+
if (null === $propertyMapping->target->parameterInConstructor && $propertyMapping->target->writeMutator && $propertyMapping->target->writeMutator->type === WriteMutator::TYPE_ADDER_AND_REMOVER) {
4747
/**
4848
* Use add and remove methods.
4949
*

tests/AutoMapperTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use AutoMapper\Tests\Fixtures\ObjectWithDateTime;
4949
use AutoMapper\Tests\Fixtures\Order;
5050
use AutoMapper\Tests\Fixtures\PetOwner;
51+
use AutoMapper\Tests\Fixtures\PetOwnerWithConstructorArguments;
5152
use AutoMapper\Tests\Fixtures\PrivatePropertyInConstructors\ChildClass;
5253
use AutoMapper\Tests\Fixtures\PrivatePropertyInConstructors\OtherClass;
5354
use AutoMapper\Tests\Fixtures\Provider\CustomProvider;
@@ -1088,6 +1089,26 @@ public function testAdderAndRemoverWithNull(): void
10881089
self::assertCount(0, $petOwnerData->getPets());
10891090
}
10901091

1092+
public function testAdderAndRemoverWithConstructorArguments(): void
1093+
{
1094+
if (!class_exists(ClassDiscriminatorFromClassMetadata::class)) {
1095+
self::markTestSkipped('Symfony Serializer is required to run this test.');
1096+
}
1097+
1098+
$petOwner = [
1099+
'pets' => [
1100+
['type' => 'cat', 'name' => 'Félix'],
1101+
],
1102+
];
1103+
1104+
$petOwnerData = $this->autoMapper->map($petOwner, PetOwnerWithConstructorArguments::class);
1105+
1106+
self::assertIsArray($petOwnerData->getPets());
1107+
self::assertCount(1, $petOwnerData->getPets());
1108+
self::assertSame('Félix', $petOwnerData->getPets()[0]->name);
1109+
self::assertSame('cat', $petOwnerData->getPets()[0]->type);
1110+
}
1111+
10911112
public function testIssueTargetToPopulate(): void
10921113
{
10931114
$source = new Fixtures\IssueTargetToPopulate\VatModel();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AutoMapper\Tests\Fixtures;
6+
7+
class PetOwnerWithConstructorArguments
8+
{
9+
/** @var array<int, Pet> */
10+
private $pets;
11+
12+
public function __construct(array $pets)
13+
{
14+
$this->pets = $pets;
15+
}
16+
17+
/**
18+
* @return Pet[]
19+
*/
20+
public function getPets(): array
21+
{
22+
return $this->pets;
23+
}
24+
25+
public function addPet(Pet $pet): void
26+
{
27+
$this->pets[] = $pet;
28+
}
29+
30+
public function removePet(Pet $pet): void
31+
{
32+
$index = array_search($pet, $this->pets);
33+
34+
if ($index !== false) {
35+
unset($this->pets[$index]);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)