1
0
mirror of synced 2025-02-10 01:09:26 +03:00

Merge branch 'fix/#6068-docblock-entity-generation-for-nullable-types'

Close #6068
This commit is contained in:
Marco Pivetta 2016-11-26 06:35:33 +01:00
commit 89a00860e4
2 changed files with 39 additions and 14 deletions

View File

@ -1168,17 +1168,18 @@ public function __construct(<params>)
continue; continue;
} }
$nullableField = $this->nullableFieldExpression($fieldMapping);
if (( ! isset($fieldMapping['id']) || if (( ! isset($fieldMapping['id']) ||
! $fieldMapping['id'] || ! $fieldMapping['id'] ||
$metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE
) && (! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable) ) && (! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable)
&& $code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'], $nullableField)
) { ) {
if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) { $methods[] = $code;
$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; $methods[] = $code;
} }
} }
@ -1205,7 +1206,7 @@ public function __construct(<params>)
if ($code = $this->generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) { if ($code = $this->generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) {
$methods[] = $code; $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; $methods[] = $code;
} }
} elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) { } elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
@ -1355,7 +1356,7 @@ public function __construct(<params>)
* *
* @return string * @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); $methodName = $type . Inflector::classify($fieldName);
$variableName = Inflector::camelize($fieldName); $variableName = Inflector::camelize($fieldName);
@ -1384,11 +1385,11 @@ public function __construct(<params>)
$replacements = array( $replacements = array(
'<description>' => ucfirst($type) . ' ' . $variableName . '.', '<description>' => ucfirst($type) . ' ' . $variableName . '.',
'<methodTypeHint>' => $methodTypeHint, '<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType, '<variableType>' => $variableType . (null !== $defaultValue ? ('|' . $defaultValue) : ''),
'<variableName>' => $variableName, '<variableName>' => $variableName,
'<methodName>' => $methodName, '<methodName>' => $methodName,
'<fieldName>' => $fieldName, '<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null ) ? (' = '.$defaultValue) : '', '<variableDefault>' => ($defaultValue !== null ) ? (' = ' . $defaultValue) : '',
'<entity>' => $this->getClassName($metadata) '<entity>' => $this->getClassName($metadata)
); );
@ -1627,7 +1628,9 @@ public function __construct(<params>)
{ {
$lines = array(); $lines = array();
$lines[] = $this->spaces . '/**'; $lines[] = $this->spaces . '/**';
$lines[] = $this->spaces . ' * @var ' . $this->getType($fieldMapping['type']); $lines[] = $this->spaces . ' * @var '
. $this->getType($fieldMapping['type'])
. ($this->nullableFieldExpression($fieldMapping) ? '|null' : '');
if ($this->generateAnnotations) { if ($this->generateAnnotations) {
$lines[] = $this->spaces . ' *'; $lines[] = $this->spaces . ' *';
@ -1809,6 +1812,20 @@ public function __construct(<params>)
return static::$generatorStrategyMap[$type]; return static::$generatorStrategyMap[$type];
} }
/**
* @param array $fieldMapping
*
* @return string|null
*/
private function nullableFieldExpression(array $fieldMapping)
{
if (isset($fieldMapping['nullable']) && true === $fieldMapping['nullable']) {
return 'null';
}
return null;
}
/** /**
* Exports (nested) option elements. * Exports (nested) option elements.
* *

View File

@ -368,8 +368,8 @@ class EntityGeneratorTest extends OrmTestCase
$this->assertPhpDocReturnType('boolean', new \ReflectionMethod($book, 'removeComment')); $this->assertPhpDocReturnType('boolean', new \ReflectionMethod($book, 'removeComment'));
$this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author')); $this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author'));
$this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'getAuthor')); $this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor|null', new \ReflectionMethod($book, 'getAuthor'));
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'setAuthor')); $this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor|null', new \ReflectionMethod($book, 'setAuthor'));
$expectedClassName = '\\' . $embeddedMetadata->name; $expectedClassName = '\\' . $embeddedMetadata->name;
$this->assertPhpDocVarType($expectedClassName, new \ReflectionProperty($book, 'isbn')); $this->assertPhpDocVarType($expectedClassName, new \ReflectionProperty($book, 'isbn'));
@ -1080,17 +1080,25 @@ class
*/ */
private function assertPhpDocVarType($type, \ReflectionProperty $property) private function assertPhpDocVarType($type, \ReflectionProperty $property)
{ {
$this->assertEquals(1, preg_match('/@var\s+([^\s]+)/',$property->getDocComment(), $matches)); $docComment = $property->getDocComment();
$regex = '/@var\s+([\S]+)$/m';
$this->assertRegExp($regex, $docComment);
$this->assertEquals(1, preg_match($regex, $docComment, $matches));
$this->assertEquals($type, $matches[1]); $this->assertEquals($type, $matches[1]);
} }
/** /**
* @param string $type * @param string $type
* @param \ReflectionProperty $method * @param \ReflectionMethod $method
*/ */
private function assertPhpDocReturnType($type, \ReflectionMethod $method) private function assertPhpDocReturnType($type, \ReflectionMethod $method)
{ {
$this->assertEquals(1, preg_match('/@return\s+([^\s]+)/', $method->getDocComment(), $matches)); $docComment = $method->getDocComment();
$regex = '/@return\s+([\S]+)(\s+.*)$/m';
$this->assertRegExp($regex, $docComment);
$this->assertEquals(1, preg_match($regex, $docComment, $matches));
$this->assertEquals($type, $matches[1]); $this->assertEquals($type, $matches[1]);
} }