add relation property only when target entity has uuid

This commit is contained in:
Tomas Votruba 2019-09-10 15:23:23 +02:00
parent c040e15fb5
commit 62655ea194
5 changed files with 47 additions and 3 deletions

View File

@ -96,7 +96,7 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
*/
private function createMirrorNullable(Property $property): Property
{
$oldProperytName = $this->getName($property);
$oldPropertyName = $this->getName($property);
$propertyWithUuid = clone $property;
@ -105,11 +105,11 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
// name must be changed after the doc comment update, because the reflection annotation needed for update of doc comment
// would miss non existing *Uuid property
$uuidPropertyName = $oldProperytName . 'Uuid';
$uuidPropertyName = $oldPropertyName . 'Uuid';
$newPropertyProperty = new PropertyProperty(new VarLikeIdentifier($uuidPropertyName));
$propertyWithUuid->props = [$newPropertyProperty];
$this->addNewPropertyToCollector($property, $oldProperytName, $uuidPropertyName);
$this->addNewPropertyToCollector($property, $oldPropertyName, $uuidPropertyName);
return $propertyWithUuid;
}
@ -186,6 +186,10 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
return true;
}
if (! property_exists($targetEntity, 'uuid')) {
return true;
}
return false;
}

View File

@ -23,6 +23,7 @@ final class AddUuidMirrorForRelationPropertyRectorTest extends AbstractRectorTes
yield [__DIR__ . '/Fixture/to_one.php.inc'];
yield [__DIR__ . '/Fixture/to_many.php.inc'];
yield [__DIR__ . '/Fixture/skip_already_added.php.inc'];
yield [__DIR__ . '/Fixture/skip_to_many_without_target_entity_uuid.php.inc'];
}
protected function getRectorClass(): string

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="wohoo")
*/
class SkipTooManyWithoutTargetEntityUuid
{
/**
* @ORM\ManyToMany(targetEntity="Rector\Doctrine\Tests\Rector\Class_\AddUuidMirrorForRelationPropertyRector\Fixture\FooEntityWithoutUuid", cascade={"persist", "merge"})
*/
private $amenity;
}
/**
* @ORM\Entity
*/
class FooEntityWithoutUuid
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}

View File

@ -28,6 +28,8 @@ class FooEntity
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $uuid;
}
?>
@ -67,6 +69,8 @@ class FooEntity
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $uuid;
}
?>

View File

@ -28,6 +28,8 @@ class AnotherEntity
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $uuid;
}
?>
@ -67,6 +69,8 @@ class AnotherEntity
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $uuid;
}
?>