mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 01:41:00 +01:00
add parent construct call to uuid init
This commit is contained in:
parent
798b94b943
commit
8047b4c10d
@ -12,11 +12,14 @@ use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Doctrine\PhpDocParser\Ast\PhpDoc\PhpDocTagNodeFactory;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
|
||||
|
||||
final class EntityUuidNodeFactory
|
||||
@ -78,11 +81,17 @@ final class EntityUuidNodeFactory
|
||||
* $this->uid = \Ramsey\Uuid\Uuid::uuid4();
|
||||
* }
|
||||
*/
|
||||
public function createConstructorWithUuidPropertyDefaultValueAssign(): ClassMethod
|
||||
public function createConstructorWithUuidInitialization(Class_ $class): ClassMethod
|
||||
{
|
||||
$classMethodBuilder = $this->builderFactory->method('__construct');
|
||||
$classMethodBuilder->makePublic();
|
||||
|
||||
// keep parent constructor call
|
||||
if ($this->hasParentClassConstructor($class)) {
|
||||
$parentClassCall = $this->createParentConstructCall();
|
||||
$classMethodBuilder->addStmt($parentClassCall);
|
||||
}
|
||||
|
||||
$assign = $this->createUuidPropertyDefaultValueAssign();
|
||||
$classMethodBuilder->addStmt($assign);
|
||||
|
||||
@ -139,4 +148,19 @@ final class EntityUuidNodeFactory
|
||||
|
||||
$node->setDocComment(new Doc($stringTypeText));
|
||||
}
|
||||
|
||||
private function hasParentClassConstructor(Class_ $class): bool
|
||||
{
|
||||
$parentClassName = $class->getAttribute(AttributeKey::PARENT_CLASS_NAME);
|
||||
if ($parentClassName === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return method_exists($parentClassName, '__construct');
|
||||
}
|
||||
|
||||
private function createParentConstructCall(): StaticCall
|
||||
{
|
||||
return new StaticCall(new Name('parent'), '__construct');
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ final class AddUuidToEntityWhereMissingRector extends AbstractRector
|
||||
$assignExpression = $this->entityUuidNodeFactory->createUuidPropertyDefaultValueAssign();
|
||||
$constructClassMethod->stmts = array_merge([$assignExpression], (array) $constructClassMethod->stmts);
|
||||
} else {
|
||||
$constructClassMethod = $this->entityUuidNodeFactory->createConstructorWithUuidPropertyDefaultValueAssign();
|
||||
$constructClassMethod = $this->entityUuidNodeFactory->createConstructorWithUuidInitialization($node);
|
||||
$node->stmts = array_merge([$constructClassMethod], $node->stmts);
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ final class AddUuidToEntityWhereMissingRectorTest extends AbstractRectorTestCase
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/already_has_constructor.php.inc',
|
||||
__DIR__ . '/Fixture/process_string_id.php.inc',
|
||||
__DIR__ . '/Fixture/with_parent_constructor.php.inc',
|
||||
// skip
|
||||
__DIR__ . '/Fixture/skip_id_with_uuid_type.php.inc',
|
||||
__DIR__ . '/Fixture/skip_id_with_uuid_binary_type.php.inc',
|
||||
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Source\BaseEntityWithConstructor;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class WithParentConstructor extends BaseEntityWithConstructor
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="string")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
private $id;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Source\BaseEntityWithConstructor;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class WithParentConstructor extends BaseEntityWithConstructor
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->uuid = \Ramsey\Uuid\Uuid::uuid4();
|
||||
}
|
||||
/**
|
||||
* @var \Ramsey\Uuid\UuidInterface
|
||||
* @ORM\Column (type="uuid_binary", unique=true, nullable=true)
|
||||
*/
|
||||
private $uuid;
|
||||
/**
|
||||
* @var int
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="string")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
private $id;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Doctrine\Tests\Rector\Class_\AddUuidToEntityWhereMissingRector\Source;
|
||||
|
||||
class BaseEntityWithConstructor
|
||||
{
|
||||
private $items;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->items = [];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user