2020-09-28 20:29:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Naming\PropertyRenamer;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\VarLikeIdentifier;
|
2020-12-21 03:12:42 +01:00
|
|
|
use Rector\Naming\Contract\Guard\ConflictingGuardInterface;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Rector\Naming\Contract\RenameGuard\RenameGuardInterface;
|
|
|
|
use Rector\Naming\Contract\RenamerInterface;
|
|
|
|
use Rector\Naming\Contract\RenameValueObjectInterface;
|
2020-09-28 20:29:37 +02:00
|
|
|
use Rector\Naming\Guard\DateTimeAtNamingConventionGuard;
|
|
|
|
use Rector\Naming\Guard\HasMagicGetSetGuard;
|
|
|
|
use Rector\Naming\Guard\NotPrivatePropertyGuard;
|
|
|
|
use Rector\Naming\Guard\RamseyUuidInterfaceGuard;
|
|
|
|
use Rector\Naming\RenameGuard\PropertyRenameGuard;
|
|
|
|
use Rector\Naming\ValueObject\PropertyRename;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2021-01-16 22:45:18 +01:00
|
|
|
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
2020-09-28 20:29:37 +02:00
|
|
|
|
2020-10-03 21:18:12 +02:00
|
|
|
abstract class AbstractPropertyRenamer implements RenamerInterface
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var RenameGuardInterface
|
|
|
|
*/
|
|
|
|
protected $propertyRenameGuard;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 03:12:42 +01:00
|
|
|
* @var ConflictingGuardInterface
|
2020-09-28 20:29:37 +02:00
|
|
|
*/
|
|
|
|
protected $conflictingPropertyNameGuard;
|
|
|
|
|
|
|
|
/**
|
2021-01-14 23:44:07 +01:00
|
|
|
* @var PropertyFetchRenamer
|
2020-09-28 20:29:37 +02:00
|
|
|
*/
|
2021-01-14 23:44:07 +01:00
|
|
|
protected $propertyFetchRenamer;
|
2020-09-28 20:29:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var NotPrivatePropertyGuard
|
|
|
|
*/
|
|
|
|
private $notPrivatePropertyGuard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RamseyUuidInterfaceGuard
|
|
|
|
*/
|
|
|
|
private $ramseyUuidInterfaceGuard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DateTimeAtNamingConventionGuard
|
|
|
|
*/
|
|
|
|
private $dateTimeAtNamingConventionGuard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var HasMagicGetSetGuard
|
|
|
|
*/
|
|
|
|
private $hasMagicGetSetGuard;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
|
|
|
public function autowireAbstractPropertyRenamer(
|
2021-01-16 22:45:18 +01:00
|
|
|
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
|
2020-09-28 20:29:37 +02:00
|
|
|
NodeNameResolver $nodeNameResolver,
|
|
|
|
NotPrivatePropertyGuard $notPrivatePropertyGuard,
|
|
|
|
RamseyUuidInterfaceGuard $ramseyUuidInterfaceGuard,
|
|
|
|
DateTimeAtNamingConventionGuard $dateTimeAtNamingConventionGuard,
|
|
|
|
PropertyRenameGuard $propertyRenameGuard,
|
2021-01-14 23:44:07 +01:00
|
|
|
HasMagicGetSetGuard $hasMagicGetSetGuard,
|
|
|
|
PropertyFetchRenamer $propertyFetchRenamer
|
2020-09-28 20:29:37 +02:00
|
|
|
): void {
|
|
|
|
$this->notPrivatePropertyGuard = $notPrivatePropertyGuard;
|
|
|
|
$this->ramseyUuidInterfaceGuard = $ramseyUuidInterfaceGuard;
|
|
|
|
$this->dateTimeAtNamingConventionGuard = $dateTimeAtNamingConventionGuard;
|
|
|
|
$this->propertyRenameGuard = $propertyRenameGuard;
|
|
|
|
$this->hasMagicGetSetGuard = $hasMagicGetSetGuard;
|
2021-01-14 23:44:07 +01:00
|
|
|
$this->propertyFetchRenamer = $propertyFetchRenamer;
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 21:18:12 +02:00
|
|
|
/**
|
|
|
|
* @param PropertyRename $renameValueObject
|
|
|
|
* @return Property|null
|
|
|
|
*/
|
|
|
|
public function rename(RenameValueObjectInterface $renameValueObject): ?Node
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
2020-12-21 03:12:42 +01:00
|
|
|
if (! $this->areNamesDifferent($renameValueObject)) {
|
2020-09-28 20:29:37 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-03 21:18:12 +02:00
|
|
|
if ($this->propertyRenameGuard->shouldSkip($renameValueObject, [
|
2020-09-28 20:29:37 +02:00
|
|
|
$this->notPrivatePropertyGuard,
|
|
|
|
$this->conflictingPropertyNameGuard,
|
|
|
|
$this->ramseyUuidInterfaceGuard,
|
|
|
|
$this->dateTimeAtNamingConventionGuard,
|
|
|
|
$this->hasMagicGetSetGuard,
|
|
|
|
])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-03 21:18:12 +02:00
|
|
|
$onlyPropertyProperty = $renameValueObject->getPropertyProperty();
|
|
|
|
$onlyPropertyProperty->name = new VarLikeIdentifier($renameValueObject->getExpectedName());
|
|
|
|
$this->renamePropertyFetchesInClass($renameValueObject);
|
2020-09-28 20:29:37 +02:00
|
|
|
|
2020-10-03 21:18:12 +02:00
|
|
|
return $renameValueObject->getProperty();
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 02:41:44 +07:00
|
|
|
protected function renamePropertyFetchesInClass(PropertyRename $propertyRename): void
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
2021-01-14 23:44:07 +01:00
|
|
|
$this->propertyFetchRenamer->renamePropertyFetchesInClass(
|
|
|
|
$propertyRename->getClassLike(),
|
|
|
|
$propertyRename->getCurrentName(),
|
|
|
|
$propertyRename->getExpectedName()
|
|
|
|
);
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 23:44:07 +01:00
|
|
|
private function areNamesDifferent(PropertyRename $propertyRename): bool
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
2021-01-14 23:44:07 +01:00
|
|
|
return $propertyRename->getCurrentName() !== $propertyRename->getExpectedName();
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
|
|
|
}
|