Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ private function generatePropertyHydrateCall(ObjectProperty $property, string $i
$propertyName = $property->name;
$escapedName = var_export($propertyName, true);

if ($property->allowsNull && ! $property->hasDefault) {
return ['$object->' . $propertyName . ' = ' . $inputArrayName . '[' . $escapedName . '] ?? null;'];
if (! $property->hasDefault) {
if ($property->allowsNull) {
return ['$object->' . $propertyName . ' = ' . $inputArrayName . '[' . $escapedName . '] ?? null;'];
}

return [
'if (\\array_key_exists(' . $escapedName . ', ' . $inputArrayName . ')) {',
' $object->' . $propertyName . ' = ' . $inputArrayName . '[' . $escapedName . '];',
'}',
];
}

return [
Expand All @@ -119,6 +127,31 @@ private function generatePropertyHydrateCall(ObjectProperty $property, string $i
];
}

/**
* @return string[]
* @psalm-return list<string>
*/
private function generatePropertyExtractCall(ObjectProperty $property, string $outputArrayName): array
{
$propertyName = $property->name;
$escapedName = var_export($propertyName, true);

if ($property->hasType && ! $property->hasDefault) {
return [
'try {',
' ' . $outputArrayName . '[' . $escapedName . '] = $object->' . $propertyName . ';',
'} catch (\\Error) {',
// @todo Include this or not ?
// " " . $outputArrayName . "[" . $escapedName . "] = null;",
'}',
];
}

return [
$outputArrayName . '[' . $escapedName . '] = $object->' . $propertyName . ';',
];
}

private function replaceConstructor(ClassMethod $method): void
{
$method->params = [];
Expand All @@ -140,8 +173,7 @@ private function replaceConstructor(ClassMethod $method): void
// Extract closures
$bodyParts[] = '$this->extractCallbacks[] = \\Closure::bind(static function ($object, &$values) {';
foreach ($properties as $property) {
$propertyName = $property->name;
$bodyParts[] = " \$values['" . $propertyName . "'] = \$object->" . $propertyName . ';';
$bodyParts = array_merge($bodyParts, $this->generatePropertyExtractCall($property, '$values'));
}

$bodyParts[] = '}, null, ' . var_export($className, true) . ');' . "\n";
Expand Down Expand Up @@ -185,8 +217,7 @@ private function replaceExtract(ClassMethod $method): void
$bodyParts = [];
$bodyParts[] = '$ret = array();';
foreach ($this->visiblePropertyMap as $property) {
$propertyName = $property->name;
$bodyParts[] = "\$ret['" . $propertyName . "'] = \$object->" . $propertyName . ';';
$bodyParts = array_merge($bodyParts, $this->generatePropertyExtractCall($property, '$ret'));
}

$index = 0;
Expand Down
22 changes: 22 additions & 0 deletions tests/GeneratedHydratorTest/Functional/HydratorFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public function testHydratorWillNotRaisedUnitiliazedTypedPropertyAccessError():
], $hydrator->extract($instance));
}

/**
* Ensures that the hydrator will not attempt to read unitialized PHP >= 7.4
* typed property, which would cause "Uncaught Error: Typed property Foo::$a
* must not be accessed before initialization" PHP engine errors.
*/
public function testHydratorWillNotRaisedUnitiliazedTypedPropertyAccessErrorIfPropertyIsntHydrated(): void
{
$instance = new ClassWithTypedProperties();
$hydrator = $this->generateHydrator($instance);

$hydrator->hydrate(['untyped0' => 3], $instance);

self::assertSame([
'property0' => 1, // 'property0' has a default value, it should keep it.
'property1' => 2, // 'property1' has a default value, it should keep it.
'property3' => null, // 'property3' is not required, it should remain null.
'property4' => null, // 'property4' default value is null, it should remain null.
'untyped0' => 3, // 'untyped0' is null by default
'untyped1' => null, // 'untyped1' is null by default
], $hydrator->extract($instance));
}

/** @requires PHP >= 7.4 */
public function testHydratorWillSetAllTypedProperties(): void
{
Expand Down