diff --git a/src/GeneratedHydrator/CodeGenerator/Visitor/HydratorMethodsVisitor.php b/src/GeneratedHydrator/CodeGenerator/Visitor/HydratorMethodsVisitor.php index 00c9a8be..92774733 100644 --- a/src/GeneratedHydrator/CodeGenerator/Visitor/HydratorMethodsVisitor.php +++ b/src/GeneratedHydrator/CodeGenerator/Visitor/HydratorMethodsVisitor.php @@ -124,6 +124,28 @@ private function generatePropertyHydrateCall(ObjectProperty $property, string $i ]; } + /** + * @return string[] + * + * @psalm-return list + */ + private function generatePropertyExtractCall(ObjectProperty $property): array + { + $propertyName = $property->name; + $assignmentStatement = "\$values['" . $propertyName . "'] = \$object->" . $propertyName . ';'; + $requiresGuard = $property->hasType && !($property->hasDefault || $property->allowsNull); + + if (!$requiresGuard) { + return [' ' . $assignmentStatement]; + } + + return [ + ' if (isset($object->' . $propertyName . ')) {', + ' ' . $assignmentStatement, + ' }' + ]; + } + private function replaceConstructor(ClassMethod $method): void { $method->params = []; @@ -145,19 +167,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; - $requiresGuard = $property->hasType && ! ($property->hasDefault || $property->allowsNull); - $indent = $requiresGuard ? ' ' : ' '; - - if ($requiresGuard) { - $bodyParts[] = ' if (isset($object->' . $propertyName . ')) {'; - } - - $bodyParts[] = $indent . "\$values['" . $propertyName . "'] = \$object->" . $propertyName . ';'; - - if ($requiresGuard) { - $bodyParts[] = ' }'; - } + $bodyParts = array_merge($bodyParts, $this->generatePropertyExtractCall($property)); } $bodyParts[] = '}, null, ' . var_export($className, true) . ');' . "\n";