Skip to content

Commit b861bd6

Browse files
authored
Parsing nullable constructor parameters (#18)
1 parent 7ff9b9f commit b861bd6

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

src/Objects/ClassBluePrinter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ private function printConstructor(ReflectionClass $reflection, ClassBluePrint $b
4646

4747
// Loop over each parameter and describe it
4848
foreach ($constructor->getParameters() as $param) {
49-
$type = $param->getType()?->getName();
49+
// Using the getName() function omits the question mark for nullable types.
50+
$type = (string) $param->getType();
51+
5052
if ($doc !== null && \in_array($type, [null, 'array', 'iterable'])) {
5153
$type = $doc->getParamType($param->getName());
5254
}

src/Objects/ObjectMapper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ private function createObjectMappingFunction(ClassBluePrint $blueprint, string $
8686

8787
if ($argument['type'] !== null) {
8888
$arg = $this->castInMapperFunction($arg, $argument['type'], $blueprint);
89-
if (\array_key_exists('default', $argument)) {
90-
$arg = $this->wrapDefault($arg, $name, $argument['default']);
91-
}
89+
}
90+
91+
if (\array_key_exists('default', $argument)) {
92+
$arg = $this->wrapDefault($arg, $name, $argument['default']);
9293
}
9394

9495
$args[] = $arg;

tests/MapperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public static function objectValuesDataProvider(): Generator
174174
'name' => 'Jerodev',
175175
],
176176
],
177+
'foo' => null,
177178
],
178179
$dto,
179180
];

tests/Objects/ClassBluePrinterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Generator;
66
use Jerodev\DataMapper\Objects\ClassBluePrinter;
7+
use Jerodev\DataMapper\Tests\_Mocks\Constructor;
78
use Jerodev\DataMapper\Tests\_Mocks\SuitEnum;
89
use Jerodev\DataMapper\Tests\_Mocks\UserDto;
910
use Jerodev\DataMapper\Types\DataType;
@@ -63,5 +64,26 @@ public static function classBlueprintDataProvider(): Generator
6364
],
6465
],
6566
];
67+
68+
yield [
69+
Constructor::class,
70+
[
71+
'users' => [
72+
'type' => new DataTypeCollection([
73+
new DataType('array', false, [
74+
new DataTypeCollection([
75+
new DataType('UserDto', false),
76+
]),
77+
]),
78+
]),
79+
],
80+
'foo' => [
81+
'type' => new DataTypeCollection([
82+
new DataType('bool', true),
83+
]),
84+
'default' => null,
85+
],
86+
],
87+
];
6688
}
6789
}

tests/_Mocks/Constructor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ final class Constructor
99
*/
1010
public function __construct(
1111
public readonly array $users,
12+
public ?bool $foo = null,
1213
) {
1314
}
1415
}

0 commit comments

Comments
 (0)