1
0
Fork 0
mirror of synced 2025-04-01 12:26:11 +03:00

#DDC-1590: Fix Inheritance in Code-Generation

This commit is contained in:
encoder64 2014-08-03 17:02:33 +03:00
parent a8035f25a2
commit e0ae7634d5
4 changed files with 210 additions and 5 deletions

View file

@ -714,9 +714,9 @@ public function __construct()
*/
protected function hasProperty($property, ClassMetadataInfo $metadata)
{
if ($this->extendsClass()) {
if ($this->extendsClass() || class_exists($metadata->name)) {
// don't generate property if its already on the base class.
$reflClass = new \ReflectionClass($this->getClassToExtend());
$reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name);
if ($reflClass->hasProperty($property)) {
return true;
}
@ -743,15 +743,15 @@ public function __construct()
*/
protected function hasMethod($method, ClassMetadataInfo $metadata)
{
if ($this->extendsClass()) {
if ($this->extendsClass() || class_exists($metadata->name)) {
// don't generate method if its already on the base class.
$reflClass = new \ReflectionClass($this->getClassToExtend());
$reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name);
if ($reflClass->hasMethod($method)) {
return true;
}
}
// check traits for existing method
foreach ($this->getTraits($metadata) as $trait) {
if ($trait->hasMethod($method)) {

View file

@ -0,0 +1,85 @@
<?php
namespace Doctrine\Tests\Models\DDC1590;
/**
* @Entity
* @MappedSuperClass
*/
abstract class DDC1590Entity
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="datetime")
*/
protected $created_at;
/**
* @Column(type="datetime")
*/
protected $updated_at;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return DDC1590User
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return DDC1590User
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Doctrine\Tests\Models\DDC1590;
use Doctrine\Tests\Models\DDC1590\DDC1590Entity;
/**
* @Entity
* @Table(name="users")
*/
class DDC1590User extends DDC1590Entity
{
/**
* @Column(type="string", length=255)
*/
protected $name;
/**
* Set name
*
* @param string $name
*
* @return DDC1590User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}

View file

@ -547,6 +547,85 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$this->assertSame($reflClass->hasMethod('getAddress'), false);
}
/**
* @group DDC-1590
*/
public function testMethodsAndPropertiesAreNotDuplicatedInChildClasses()
{
$cmf = new ClassMetadataFactory();
$em = $this->_getTestEntityManager();
$cmf->setEntityManager($em);
$ns = $this->_namespace;
$nsdir = $this->_tmpDir . '/' . $ns;
$content = str_replace(
'namespace Doctrine\Tests\Models\DDC1590',
'namespace '.$ns,
file_get_contents(__DIR__ . '/../../Models/DDC1590/DDC1590User.php')
);
$fname = $nsdir . "/DDC1590User.php";
file_put_contents($fname, $content);
require $fname;
$metadata = $cmf->getMetadataFor($ns . '\DDC1590User');
$this->_generator->writeEntityClass($metadata, $this->_tmpDir);
// class DDC1590User extends DDC1590Entity { ... }
$source = file_get_contents($fname);
// class _DDC1590User extends DDC1590Entity { ... }
$source2 = str_replace('class DDC1590User', 'class _DDC1590User', $source);
$fname2 = $fname = $nsdir . "/_DDC1590User.php";
file_put_contents($fname2, $source2);
require $fname2;
// class __DDC1590User { ... }
$source3 = str_replace('class DDC1590User extends DDC1590Entity', 'class __DDC1590User', $source);
$fname3 = $fname = $nsdir . "/__DDC1590User.php";
file_put_contents($fname3, $source3);
require $fname3;
// class _DDC1590User extends DDC1590Entity { ... }
$rc2 = new \ReflectionClass($ns.'\_DDC1590User');
$this->assertSame($rc2->hasProperty('name'), true);
$this->assertSame($rc2->hasProperty('id'), true);
$this->assertSame($rc2->hasProperty('created_at'), true);
$this->assertSame($rc2->hasProperty('updated_at'), true);
$this->assertSame($rc2->hasMethod('getName'), true);
$this->assertSame($rc2->hasMethod('setName'), true);
$this->assertSame($rc2->hasMethod('getId'), true);
$this->assertSame($rc2->hasMethod('setId'), false);
$this->assertSame($rc2->hasMethod('getCreatedAt'), true);
$this->assertSame($rc2->hasMethod('setCreatedAt'), true);
$this->assertSame($rc2->hasMethod('getUpdatedAt'), true);
$this->assertSame($rc2->hasMethod('setUpdatedAt'), true);
// class __DDC1590User { ... }
$rc3 = new \ReflectionClass($ns.'\__DDC1590User');
$this->assertSame($rc3->hasProperty('name'), true);
$this->assertSame($rc3->hasProperty('id'), false);
$this->assertSame($rc3->hasProperty('created_at'), false);
$this->assertSame($rc3->hasProperty('updated_at'), false);
$this->assertSame($rc3->hasMethod('getName'), true);
$this->assertSame($rc3->hasMethod('setName'), true);
$this->assertSame($rc3->hasMethod('getId'), false);
$this->assertSame($rc3->hasMethod('setId'), false);
$this->assertSame($rc3->hasMethod('getCreatedAt'), false);
$this->assertSame($rc3->hasMethod('setCreatedAt'), false);
$this->assertSame($rc3->hasMethod('getUpdatedAt'), false);
$this->assertSame($rc3->hasMethod('setUpdatedAt'), false);
}
/**
* @return array
*/