Skip to content

Commit 56466b1

Browse files
authored
[Next] Deduplicate constrctor arguments (#15)
1 parent 43d02c6 commit 56466b1

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

src/Objects/ClassBluePrint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class ClassBluePrint
99
{
10-
/** @var array<array{name: string, type: DataTypeCollection, default?: mixed}> */
10+
/** @var array<string, array{type: DataTypeCollection, default?: mixed}> */
1111
public array $constructorArguments = [];
1212

1313
/** @var array<string, array{type: DataTypeCollection, default?: mixed}> */

src/Objects/ClassBluePrinter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ private function printConstructor(ReflectionClass $reflection, ClassBluePrint $b
5252
}
5353

5454
$arg = [
55-
'name' => $param->getName(),
5655
'type' => $this->resolveType(
5756
$this->dataTypeFactory->fromString($type),
5857
$reflection->getName(),
@@ -62,7 +61,7 @@ private function printConstructor(ReflectionClass $reflection, ClassBluePrint $b
6261
$arg['default'] = $param->getDefaultValue();
6362
}
6463

65-
$bluePrint->constructorArguments[] = $arg;
64+
$bluePrint->constructorArguments[$param->getName()] = $arg;
6665
}
6766
}
6867

@@ -74,6 +73,11 @@ private function printProperties(ReflectionClass $reflection, ClassBluePrint $bl
7473
continue;
7574
}
7675

76+
// Already mapped through constructor?
77+
if (\array_key_exists($property->getName(), $blueprint->constructorArguments)) {
78+
continue;
79+
}
80+
7781
$type = $property->getType()?->getName();
7882
if (\in_array($type, [null, 'array', 'iterable']) && $property->getDocComment()) {
7983
$doc = $this->docBlockParser->parse($property->getDocComment());

src/Objects/ObjectMapper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ private function createObjectMappingFunction(ClassBluePrint $blueprint, string $
8181

8282
// Instantiate a new object
8383
$args = [];
84-
foreach ($blueprint->constructorArguments as $argument) {
85-
$arg = "\$data['{$argument['name']}']";
84+
foreach ($blueprint->constructorArguments as $name => $argument) {
85+
$arg = "\$data['{$name}']";
8686

8787
if ($argument['type'] !== null) {
8888
$arg = $this->castInMapperFunction($arg, $argument['type'], $blueprint);
8989
if (\array_key_exists('default', $argument)) {
90-
$arg = $this->wrapDefault($arg, $argument['name'], $argument['default']);
90+
$arg = $this->wrapDefault($arg, $name, $argument['default']);
9191
}
9292
}
9393

tests/Objects/ClassBluePrinterTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,13 @@ public static function classBlueprintDataProvider(): Generator
2929
yield [
3030
UserDto::class,
3131
[
32-
[
33-
'name' => 'name',
32+
'name' => [
3433
'type' => new DataTypeCollection([
3534
new DataType('string', false),
3635
]),
3736
],
3837
],
3938
[
40-
'name' => [
41-
'type' => new DataTypeCollection([
42-
new DataType('string', false),
43-
]),
44-
],
4539
'friends' => [
4640
'type' => new DataTypeCollection([
4741
new DataType(

0 commit comments

Comments
 (0)