Skip to content

Commit 9d7523d

Browse files
authored
fix: source and context not passed to callable transformer (#192)
Fixes #191
2 parents 6ab60f8 + b6c484a commit 9d7523d

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
1616
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
1717
- [GH#187](https://github.com/jolicode/automapper/pull/187) Fix regression after [GH#184](https://github.com/jolicode/automapper/pull/184)
18+
- [GH#192](https://github.com/jolicode/automapper/pull/192) Fix source and context not passed to callable transformer
1819

1920
## [9.1.2] - 2024-09-03
2021
### Fixed

src/Transformer/CallableTransformer.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,20 @@ public function __construct(
2424

2525
public function transform(Expr $input, Expr $target, PropertyMetadata $propertyMapping, UniqueVariableScope $uniqueVariableScope, Expr\Variable $source): array
2626
{
27-
if ($this->callableIsMethodFromSource) {
28-
$newInput = new Expr\MethodCall(
29-
$source,
27+
if ($this->callableIsMethodFromSource || $this->callableIsMethodFromTarget) {
28+
return [new Expr\MethodCall(
29+
$this->callableIsMethodFromSource ? $source : new Expr\Variable('result'),
3030
$this->callable,
31-
[
32-
new Arg($input),
33-
]
34-
);
35-
36-
return [$newInput, []];
37-
}
38-
39-
if ($this->callableIsMethodFromTarget) {
40-
$newInput = new Expr\MethodCall(
41-
new Expr\Variable('result'),
42-
$this->callable,
43-
[
44-
new Arg($input),
45-
]
46-
);
47-
48-
return [$newInput, []];
31+
[new Arg($input), new Arg($source), new Arg(new Expr\Variable('context'))],
32+
), []];
4933
}
5034

51-
$newInput = new Expr\FuncCall(
35+
return [new Expr\FuncCall(
5236
new Scalar\String_($this->callable),
53-
[
54-
new Arg($input),
55-
]
56-
);
57-
58-
return [$newInput, []];
37+
// Internal functions throws ArgumentCountError when too many arguments are passed
38+
\function_exists($this->callable) && (new \ReflectionFunction($this->callable))->isInternal()
39+
? [new Arg($input)]
40+
: [new Arg($input), new Arg($source), new Arg(new Expr\Variable('context'))]
41+
), []];
5942
}
6043
}

tests/Fixtures/MapTo/Bar.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Bar
2020
public function __construct(
2121
public string $bar,
2222
public string $baz,
23-
#[MapFrom(property: 'foo')]
23+
#[MapFrom(property: 'foo', transformer: 'htmlspecialchars')]
2424
public string $from,
2525
) {
2626
}
@@ -36,12 +36,12 @@ public function getB(): string
3636
return $this->b;
3737
}
3838

39-
public function transformC(string $c): string
39+
public function transformC(string $c, array $source, array $context): string
4040
{
4141
return 'transformC_' . $c;
4242
}
4343

44-
public static function transformDStatic(string $c): string
44+
public static function transformDStatic(string $c, array $source, array $context): string
4545
{
4646
return 'transformDStatic_' . $c;
4747
}

0 commit comments

Comments
 (0)