Skip to content

Commit 0bf1ce8

Browse files
committed
Use nullable also if property is not required
1 parent dc356f7 commit 0bf1ce8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/ClassGenerator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function generateClasses(
122122
$classBuilder->addProperty(
123123
ClassPropertyBuilder::fromScratch(
124124
$propertyPropertyName,
125-
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
125+
$this->determinePropertyType($propertyType, $propertyClassName)
126126
)
127127
);
128128
break;
@@ -140,7 +140,7 @@ public function generateClasses(
140140
$classBuilder->addProperty(
141141
ClassPropertyBuilder::fromScratch(
142142
$propertyPropertyName,
143-
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
143+
$this->determinePropertyType($propertyType, $propertyClassName)
144144
)
145145
);
146146
$classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName);
@@ -153,7 +153,7 @@ public function generateClasses(
153153
$classBuilder->addProperty(
154154
ClassPropertyBuilder::fromScratch(
155155
$propertyPropertyName,
156-
$propertyType->isNullable() ? ('?' . $propertyClassName) : $propertyClassName
156+
$this->determinePropertyType($propertyType, $propertyClassName)
157157
)
158158
);
159159
break;
@@ -329,4 +329,11 @@ private function isValueObject(ClassBuilder $classBuilder): bool
329329
|| $classBuilder->hasMethod('toFloat')
330330
|| $classBuilder->hasMethod('toBool');
331331
}
332+
333+
private function determinePropertyType(TypeDefinition $typeDefinition, string $className): string
334+
{
335+
return ($typeDefinition->isRequired() === false || $typeDefinition->isNullable() === true)
336+
? ('?' . $className)
337+
: $className;
338+
}
332339
}

0 commit comments

Comments
 (0)