Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node->isPrivate()) {
return null;
}

/** @var Class_|Interface_|Trait_|null $classNode */
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null || $classNode instanceof Trait_ || $classNode instanceof Interface_) {
return null;
}

if ($classNode->isAnonymous()) {
return null;
}

if (count($node->props) !== 1) {
if ($this->shouldSkipProperty($node)) {
return null;
}

Expand Down Expand Up @@ -121,4 +107,32 @@ private function resolveUselessAssignNode(array $propertyFetches): array

return $uselessAssigns;
}

private function shouldSkipProperty(Property $property): bool
{
if (! $property->isPrivate()) {
return true;
}

/** @var Class_|Interface_|Trait_|null $classNode */
$classNode = $property->getAttribute(AttributeKey::CLASS_NODE);

if ($classNode === null || $classNode instanceof Trait_ || $classNode instanceof Interface_) {
return true;
}

if ($this->isDoctrineProperty($property)) {
return true;
}

if ($classNode->isAnonymous()) {
return true;
}

if (count($property->props) !== 1) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\DeadCode\Tests\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class SkipDoctrineEntityProperty
{
private $unusedProperty;

/**
* @ORM\Column
*/
private $unusedAnnotatedProperty;
}

?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class SkipDoctrineEntityProperty
{
/**
* @ORM\Column
*/
private $unusedAnnotatedProperty;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function test(): void
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/property_assign.php.inc',
__DIR__ . '/Fixture/with_trait.php.inc',
// skip
__DIR__ . '/Fixture/skip_doctrine_entity_property.php.inc',
__DIR__ . '/Fixture/skip_anonymous_class.php.inc',
__DIR__ . '/Fixture/skip_anonymous_function.php.inc',
__DIR__ . '/Fixture/skip_nested_closure.php.inc',
Expand Down
5 changes: 5 additions & 0 deletions packages/Doctrine/src/AbstarctRector/DoctrineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function autowireDoctrineTrait(DoctrineDocBlockResolver $doctrineDocBlock
$this->doctrineDocBlockResolver = $doctrineDocBlockResolver;
}

protected function isDoctrineProperty(Property $property): bool
{
return $this->doctrineDocBlockResolver->isDoctrineProperty($property);
}

protected function isDoctrineEntityClass(Class_ $class): bool
{
return $this->doctrineDocBlockResolver->isDoctrineEntityClass($class);
Expand Down
18 changes: 18 additions & 0 deletions packages/Doctrine/src/PhpDocParser/DoctrineDocBlockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ public function getDoctrineTableTagValueNode(Class_ $class): ?TableTagValueNode
return $classPhpDocInfo->getDoctrineTableTagValueNode();
}

public function isDoctrineProperty(Property $property): bool
{
$propertyPhpDocInfo = $this->getPhpDocInfo($property);
if ($propertyPhpDocInfo === null) {
return false;
}

if ($propertyPhpDocInfo->getDoctrineColumnTagValueNode()) {
return true;
}

if ($propertyPhpDocInfo->getDoctrineRelationTagValueNode()) {
return true;
}

return false;
}

private function getPhpDocInfo(Node $node): ?PhpDocInfo
{
if ($node->getDocComment() === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Rector\SOLID\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeContainer\ParsedNodesByType;
Expand Down Expand Up @@ -74,7 +73,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isDoctrineEntity($node)) {
if ($this->isDoctrineEntityClass($node)) {
return null;
}

Expand All @@ -88,13 +87,4 @@ public function refactor(Node $node): ?Node

return $node;
}

private function isDoctrineEntity(Node $node): bool
{
if ($node->getDocComment() === null) {
return false;
}

return Strings::contains($node->getDocComment()->getText(), 'Entity');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rector\SOLID\Tests\Rector\Class_\FinalizeClassesWithoutChildrenRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
Expand Down