Skip to content

Commit

Permalink
Merge branch 'fix/#6068-docblock-entity-generation-for-nullable-types'
Browse files Browse the repository at this point in the history
Close #6068
  • Loading branch information
Ocramius committed Nov 26, 2016
2 parents c148059 + f8002ca commit 89a0086
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
35 changes: 26 additions & 9 deletions lib/Doctrine/ORM/Tools/EntityGenerator.php
Expand Up @@ -1168,17 +1168,18 @@ protected function generateEntityStubMethods(ClassMetadataInfo $metadata)
continue;
}

$nullableField = $this->nullableFieldExpression($fieldMapping);

if (( ! isset($fieldMapping['id']) ||
! $fieldMapping['id'] ||
$metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE
) && (! $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;
}
}
Expand All @@ -1205,7 +1206,7 @@ protected function generateEntityStubMethods(ClassMetadataInfo $metadata)
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) {
Expand Down Expand Up @@ -1355,7 +1356,7 @@ protected function generateEntityEmbeddedProperties(ClassMetadataInfo $metadata)
*
* @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);
Expand Down Expand Up @@ -1384,11 +1385,11 @@ protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type,
$replacements = array(
'<description>' => ucfirst($type) . ' ' . $variableName . '.',
'<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType,
'<variableType>' => $variableType . (null !== $defaultValue ? ('|' . $defaultValue) : ''),
'<variableName>' => $variableName,
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null ) ? (' = '.$defaultValue) : '',
'<variableDefault>' => ($defaultValue !== null ) ? (' = ' . $defaultValue) : '',
'<entity>' => $this->getClassName($metadata)
);

Expand Down Expand Up @@ -1627,7 +1628,9 @@ protected function generateFieldMappingPropertyDocBlock(array $fieldMapping, Cla
{
$lines = array();
$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) {
$lines[] = $this->spaces . ' *';
Expand Down Expand Up @@ -1809,6 +1812,20 @@ protected function getIdGeneratorTypeString($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.
*
Expand Down
18 changes: 13 additions & 5 deletions tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php
Expand Up @@ -368,8 +368,8 @@ public function testMethodDocBlockShouldStartWithBackSlash()
$this->assertPhpDocReturnType('boolean', new \ReflectionMethod($book, 'removeComment'));

$this->assertPhpDocVarType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionProperty($book, 'author'));
$this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'getAuthor'));
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor', new \ReflectionMethod($book, 'setAuthor'));
$this->assertPhpDocReturnType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor|null', new \ReflectionMethod($book, 'getAuthor'));
$this->assertPhpDocParamType('\Doctrine\Tests\ORM\Tools\EntityGeneratorAuthor|null', new \ReflectionMethod($book, 'setAuthor'));

$expectedClassName = '\\' . $embeddedMetadata->name;
$this->assertPhpDocVarType($expectedClassName, new \ReflectionProperty($book, 'isbn'));
Expand Down Expand Up @@ -1080,17 +1080,25 @@ public static function someMethod(){
*/
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]);
}

/**
* @param string $type
* @param \ReflectionProperty $method
* @param \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]);
}

Expand Down

0 comments on commit 89a0086

Please sign in to comment.