From d3c604567b20d01a2112307611ebf910d7847ede Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 4 Oct 2016 11:01:53 -0300 Subject: [PATCH] Update docblock generation for nullable fields --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index e48e8535b..b2bf1880e 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -1168,17 +1168,18 @@ public function __construct() continue; } + $nullableField = $this->getNullableField($fieldMapping); if (( ! isset($fieldMapping['id']) || ! $fieldMapping['id'] || $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE ) && (! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable) ) { - if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) { + if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'], $nullableField)) { $methods[] = $code; } } - if ($code = $this->generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'])) { + if ($code = $this->generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'], $nullableField)) { $methods[] = $code; } } @@ -1205,7 +1206,7 @@ public function __construct() if ($code = $this->generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) { $methods[] = $code; } - if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) { + if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) { $methods[] = $code; } } elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) { @@ -1355,7 +1356,7 @@ public function __construct() * * @return string */ - protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) + protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); $variableName = Inflector::camelize($fieldName); @@ -1384,7 +1385,7 @@ public function __construct() $replacements = array( '' => ucfirst($type) . ' ' . $variableName . '.', '' => $methodTypeHint, - '' => $variableType, + '' => $variableType.(null !== $defaultValue ? ('|'.$defaultValue) : ''), '' => $variableName, '' => $methodName, '' => $fieldName, @@ -1627,7 +1628,7 @@ public function __construct() { $lines = array(); $lines[] = $this->spaces . '/**'; - $lines[] = $this->spaces . ' * @var ' . $this->getType($fieldMapping['type']); + $lines[] = $this->spaces . ' * @var ' . $this->getType($fieldMapping['type']) . ($this->getNullableField($fieldMapping) ? '|' . $this->getNullableField($fieldMapping) : '');; if ($this->generateAnnotations) { $lines[] = $this->spaces . ' *'; @@ -1809,6 +1810,18 @@ public function __construct() return static::$generatorStrategyMap[$type]; } + /** + * @param array $fieldMapping + * + * @return string|null + */ + protected function getNullableField(array $fieldMapping) + { + if (isset($fieldMapping['nullable']) && true === $fieldMapping['nullable']) { + return 'null'; + } + } + /** * Exports (nested) option elements. *