diff --git a/src/ClassGenerator.php b/src/ClassGenerator.php index 327928a..207e3c7 100644 --- a/src/ClassGenerator.php +++ b/src/ClassGenerator.php @@ -122,7 +122,7 @@ public function generateClasses( $classBuilder->addProperty( ClassPropertyBuilder::fromScratch( $propertyPropertyName, - $propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName + $this->determinePropertyType($propertyType, $propertyClassName) ) ); break; @@ -140,7 +140,7 @@ public function generateClasses( $classBuilder->addProperty( ClassPropertyBuilder::fromScratch( $propertyPropertyName, - $propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName + $this->determinePropertyType($propertyType, $propertyClassName) ) ); $classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName); @@ -153,7 +153,7 @@ public function generateClasses( $classBuilder->addProperty( ClassPropertyBuilder::fromScratch( $propertyPropertyName, - $propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName + $this->determinePropertyType($propertyType, $propertyClassName) ) ); break; @@ -329,4 +329,11 @@ private function isValueObject(ClassBuilder $classBuilder): bool || $classBuilder->hasMethod('toFloat') || $classBuilder->hasMethod('toBool'); } + + private function determinePropertyType(TypeDefinition $typeDefinition, string $className): string + { + return ($typeDefinition->isRequired() === false || $typeDefinition->isNullable() === true) + ? ('?' . $className) + : $className; + } }