diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 5a55f0fc9..405f954d2 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -709,6 +709,13 @@ public function __construct() } } + // check traits for existing property + foreach ($this->getTraits($metadata) as $trait) { + if ($trait->hasProperty($property)) { + return true; + } + } + return ( isset($this->staticReflection[$metadata->name]) && in_array($property, $this->staticReflection[$metadata->name]['properties']) @@ -732,12 +739,37 @@ public function __construct() } } + // check traits for existing method + foreach ($this->getTraits($metadata) as $trait) { + if ($trait->hasMethod($method)) { + return true; + } + } + return ( isset($this->staticReflection[$metadata->name]) && in_array($method, $this->staticReflection[$metadata->name]['methods']) ); } + /** + * @param ClassMetadataInfo $metadata + * + * @return array + */ + protected function getTraits(ClassMetadataInfo $metadata) + { + if (PHP_VERSION_ID >= 50400 && ($metadata->reflClass !== null || class_exists($metadata->name))) { + $reflClass = $metadata->reflClass === null + ? new \ReflectionClass($metadata->name) + : $metadata->reflClass; + + return $reflClass->getTraits(); + } + + return array(); + } + /** * @param ClassMetadataInfo $metadata * diff --git a/tests/Doctrine/Tests/Models/DDC2372/DDC2372Address.php b/tests/Doctrine/Tests/Models/DDC2372/DDC2372Address.php new file mode 100644 index 000000000..26144c61d --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC2372/DDC2372Address.php @@ -0,0 +1,45 @@ +id; + } + + public function getStreet() + { + return $this->street; + } + + public function setStreet($street) + { + $this->street = $street; + } + + public function getUser() + { + return $this->user; + } + + public function setUser(User $user) + { + if ($this->user !== $user) { + $this->user = $user; + $user->setAddress($this); + } + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC2372/DDC2372User.php b/tests/Doctrine/Tests/Models/DDC2372/DDC2372User.php new file mode 100644 index 000000000..4df0df6a5 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC2372/DDC2372User.php @@ -0,0 +1,34 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC2372/Traits/DDC2372AddressTrait.php b/tests/Doctrine/Tests/Models/DDC2372/Traits/DDC2372AddressTrait.php new file mode 100644 index 000000000..66ca17446 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC2372/Traits/DDC2372AddressTrait.php @@ -0,0 +1,25 @@ +address; + } + + public function setAddress(Address $address) + { + if ($this->address !== $address) { + $this->address = $address; + $address->setUser($this); + } + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index d8dfa1b56..b49125cdc 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -2,10 +2,12 @@ namespace Doctrine\Tests\ORM\Tools; -use Doctrine\ORM\Tools\SchemaTool, - Doctrine\ORM\Tools\EntityGenerator, - Doctrine\ORM\Tools\Export\ClassMetadataExporter, - Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Tools\SchemaTool; +use Doctrine\ORM\Tools\EntityGenerator; +use Doctrine\ORM\Tools\Export\ClassMetadataExporter; +use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Mapping\ClassMetadataFactory; +use Doctrine\Tests\Models\DDC2372\DDC2372User; require_once __DIR__ . '/../../TestInit.php'; @@ -285,7 +287,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $filename = $this->_tmpDir . DIRECTORY_SEPARATOR . $this->_namespace . DIRECTORY_SEPARATOR . 'DDC1784Entity.php'; - + $this->assertFileExists($filename); require_once $filename; @@ -333,7 +335,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $property = new \ReflectionProperty($metadata->name, 'centroCustos'); $docComment = $property->getDocComment(); - + //joinColumns $this->assertContains('@JoinColumn(name="idorcamento", referencedColumnName="idorcamento"),', $docComment); $this->assertContains('@JoinColumn(name="idunidade", referencedColumnName="idunidade")', $docComment); @@ -439,7 +441,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $entity = new $metadata->name; $reflClass = new \ReflectionClass($metadata->name); - + $type = $field['phpType']; $name = $field['fieldName']; $value = $field['value']; @@ -454,6 +456,36 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals($value, $entity->{$getter}()); } + /** + * @group DDC-2372 + */ + public function testTraitPropertiesAndMethodsAreNotDuplicated() + { + if (PHP_VERSION_ID < 50400) { + $this->markTestSkipped('Traits are not available before php 5.4.'); + } + + $cmf = new ClassMetadataFactory(); + $em = $this->_getTestEntityManager(); + $cmf->setEntityManager($em); + + $user = new DDC2372User(); + $metadata = $cmf->getMetadataFor(get_class($user)); + $metadata->name = $this->_namespace . "\DDC2372User"; + $metadata->namespace = $this->_namespace; + + $this->_generator->writeEntityClass($metadata, $this->_tmpDir); + + $this->assertFileExists($this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php"); + require $this->_tmpDir . "/" . $this->_namespace . "/DDC2372User.php"; + + $reflClass = new \ReflectionClass($metadata->name); + + $this->assertSame($reflClass->hasProperty('address'), false); + $this->assertSame($reflClass->hasMethod('setAddress'), false); + $this->assertSame($reflClass->hasMethod('getAddress'), false); + } + /** * @return array */ @@ -473,43 +505,43 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase 'value' => new \DateTime )), array(array( - 'fieldName' => 'date', + 'fieldName' => 'date', 'phpType' => '\\DateTime', 'dbType' => 'date', 'value' => new \DateTime )), array(array( - 'fieldName' => 'time', + 'fieldName' => 'time', 'phpType' => '\DateTime', 'dbType' => 'time', 'value' => new \DateTime )), array(array( - 'fieldName' => 'object', + 'fieldName' => 'object', 'phpType' => '\stdClass', 'dbType' => 'object', 'value' => new \stdClass() )), array(array( - 'fieldName' => 'bigint', + 'fieldName' => 'bigint', 'phpType' => 'integer', 'dbType' => 'bigint', 'value' => 11 )), array(array( - 'fieldName' => 'smallint', + 'fieldName' => 'smallint', 'phpType' => 'integer', 'dbType' => 'smallint', 'value' => 22 )), array(array( - 'fieldName' => 'text', + 'fieldName' => 'text', 'phpType' => 'string', 'dbType' => 'text', 'value' => 'text' )), array(array( - 'fieldName' => 'blob', + 'fieldName' => 'blob', 'phpType' => 'string', 'dbType' => 'blob', 'value' => 'blob'