Skip to content

Commit 0a7abf0

Browse files
committed
fix: constructor with default parameter array does not work with context
1 parent 7585318 commit 0a7abf0

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
14+
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
1415

1516
## [9.1.2] - 2024-09-03
1617
### Fixed

src/Generator/CreateTargetStatementsGenerator.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,15 @@ private function constructorArgument(GeneratorMetadata $metadata, PropertyMetada
206206
}
207207

208208
if ($defaultValueExpr instanceof Expr\Array_) {
209-
// $constructarg_3 = count($values) > 0 ? $values : array();
210-
$argumentAssignedValue = new Expr\Ternary(new Expr\BinaryOp\Greater(new Expr\FuncCall(new Name('count'), [new Arg($output)]), create_scalar_int(0)), $output, $defaultValueExpr);
209+
// $constructarg = count($values) > 0 ? $values : {expression};
210+
$argumentAssignClosure = static fn (Expr $expr) => new Expr\Assign($constructVar, new Expr\Ternary(
211+
new Expr\BinaryOp\Greater(new Expr\FuncCall(new Name('count'), [new Arg($output)]), create_scalar_int(0)),
212+
$output,
213+
$expr,
214+
));
211215
} else {
212-
// $constructarg_0 = $values ?? array();
213-
$argumentAssignedValue = new Expr\BinaryOp\Coalesce($output, $defaultValueExpr);
216+
// $constructarg = $values ?? {expression};
217+
$argumentAssignClosure = static fn (Expr $expr) => new Expr\Assign($constructVar, new Expr\BinaryOp\Coalesce($output, $expr));
214218
}
215219

216220
return [
@@ -221,15 +225,15 @@ private function constructorArgument(GeneratorMetadata $metadata, PropertyMetada
221225
]), [
222226
'stmts' => [
223227
...$propStatements,
224-
new Stmt\Expression(new Expr\Assign($constructVar, new Expr\BinaryOp\Coalesce($output, new Expr\StaticCall(new Name\FullyQualified(MapperContext::class), 'getConstructorArgument', [
228+
new Stmt\Expression($argumentAssignClosure(new Expr\StaticCall(new Name\FullyQualified(MapperContext::class), 'getConstructorArgument', [
225229
new Arg($variableRegistry->getContext()),
226230
new Arg(new Scalar\String_($metadata->mapperMetadata->target)),
227231
new Arg(new Scalar\String_($propertyMetadata->target->property)),
228-
])))),
232+
]))),
229233
],
230234
'else' => new Stmt\Else_([
231235
...$propStatements,
232-
new Stmt\Expression(new Expr\Assign($constructVar, $argumentAssignedValue)),
236+
new Stmt\Expression($argumentAssignClosure($defaultValueExpr)),
233237
]),
234238
]),
235239
new Arg($constructVar, name: new Identifier($parameter->getName())),

tests/AutoMapperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,18 @@ public function testConstructor(): void
469469
self::assertTrue($userDto->getConstructor());
470470
}
471471

472+
public function testConstructorArrayArgumentFromContext(): void
473+
{
474+
$data = ['baz' => 'baz'];
475+
/** @var ConstructorWithDefaultValues $userDto */
476+
$object = $this->autoMapper->map($data, ConstructorWithDefaultValues::class, [MapperContext::CONSTRUCTOR_ARGUMENTS => [
477+
ConstructorWithDefaultValues::class => ['someOtters' => [1]],
478+
]]);
479+
480+
self::assertInstanceOf(ConstructorWithDefaultValues::class, $object);
481+
self::assertSame([1], $object->someOtters);
482+
}
483+
472484
public function testConstructorNotAllowed(): void
473485
{
474486
$this->buildAutoMapper(mapPrivatePropertiesAndMethod: true, constructorStrategy: ConstructorStrategy::NEVER, classPrefix: 'NotAllowedMapper_');

0 commit comments

Comments
 (0)