Skip to content

Commit

Permalink
[Php81] Skip ReadOnlyPropertyRector on Entity id (#1379)
Browse files Browse the repository at this point in the history
* [Php81] Skip ReadOnlyPropertyRector on Entity id

* update fixture

* Fixed 🎉

* clean up
  • Loading branch information
samsonasik committed Dec 4, 2021
1 parent df1391d commit 8547bdb
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

use Ramsey\Uuid\UuidInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* News
*
* @ORM\Table(name="news")
* @ORM\Entity(repositoryClass="DoctrineFixtureDemo\Repository\NewsRepository")
*/
class News
{
/**
* @var integer
*
* @ORM\Id()
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
private UuidInterface $id;

/**
* @var string
*
* @ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;

/**
* Get id.
*
* @return string
*/
public function getId()
{
return (string) $this->id;
}

/**
* Set title.
*
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

use Ramsey\Uuid\UuidInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* News
*/
#[ORM\Table(name: 'news')]
#[ORM\Entity(repositoryClass: 'DoctrineFixtureDemo\Repository\NewsRepository')]
class News2
{
/**
* @var UuidInterface
*/
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'Ramsey\Uuid\Doctrine\UuidGenerator')]
private UuidInterface $id;

/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 50, nullable: false)]
private $title;

/**
* Get id.
*
* @return string
*/
public function getId()
{
return (string) $this->id;
}

/**
* Set title.
*
* @param string $title
*
* @return self
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
28 changes: 27 additions & 1 deletion src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
Expand All @@ -27,6 +28,7 @@
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\ReadWrite\Guard\VariableToConstantGuard;
use Rector\ReadWrite\NodeAnalyzer\ReadWritePropertyAnalyzer;
use Symplify\PackageBuilder\Php\TypeChecker;
Expand All @@ -37,6 +39,14 @@
*/
final class PropertyManipulator
{
/**
* @var string[]
*/
private const ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES = [
'Doctrine\ORM\Mapping\Entity',
'Doctrine\ORM\Mapping\Table',
];

public function __construct(
private AssignManipulator $assignManipulator,
private BetterNodeFinder $betterNodeFinder,
Expand All @@ -46,7 +56,8 @@ public function __construct(
private TypeChecker $typeChecker,
private PropertyFetchFinder $propertyFetchFinder,
private ReflectionResolver $reflectionResolver,
private NodeNameResolver $nodeNameResolver
private NodeNameResolver $nodeNameResolver,
private PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

Expand Down Expand Up @@ -96,6 +107,21 @@ public function isPropertyUsedInReadContext(Property | Param $propertyOrPromoted

public function isPropertyChangeableExceptConstructor(Property | Param $propertyOrParam): bool
{
$class = $this->betterNodeFinder->findParentType($propertyOrParam, Class_::class);
if ($class instanceof Class_) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
if ($phpDocInfo->hasByAnnotationClasses(self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES)) {
return true;
}

if ($this->phpAttributeAnalyzer->hasPhpAttributes(
$class,
self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES
)) {
return true;
}
}

$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($propertyOrParam);

foreach ($propertyFetches as $propertyFetch) {
Expand Down

0 comments on commit 8547bdb

Please sign in to comment.