Skip to content

Commit

Permalink
Use nullable also if property is not required
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Dec 10, 2020
1 parent dc356f7 commit 0bf1ce8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function generateClasses(
$classBuilder->addProperty(
ClassPropertyBuilder::fromScratch(
$propertyPropertyName,
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
$this->determinePropertyType($propertyType, $propertyClassName)
)
);
break;
Expand All @@ -140,7 +140,7 @@ public function generateClasses(
$classBuilder->addProperty(
ClassPropertyBuilder::fromScratch(
$propertyPropertyName,
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
$this->determinePropertyType($propertyType, $propertyClassName)
)
);
$classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName);
Expand All @@ -153,7 +153,7 @@ public function generateClasses(
$classBuilder->addProperty(
ClassPropertyBuilder::fromScratch(
$propertyPropertyName,
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
$this->determinePropertyType($propertyType, $propertyClassName)
)
);
break;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0bf1ce8

Please sign in to comment.