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
14 changes: 13 additions & 1 deletion packages/DeadCode/src/Analyzer/SetterOnlyMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\DeadCode\Doctrine\DoctrineEntityManipulator;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\PhpParser\Node\Resolver\NameResolver;
Expand Down Expand Up @@ -41,16 +42,23 @@ final class SetterOnlyMethodAnalyzer
*/
private $propertiesAndMethodsToRemoveByType = [];

/**
* @var DoctrineEntityManipulator
*/
private $doctrineEntityManipulator;

public function __construct(
ParsedNodesByType $parsedNodesByType,
ClassManipulator $classManipulator,
NameResolver $nameResolver,
CallableNodeTraverser $callableNodeTraverser
CallableNodeTraverser $callableNodeTraverser,
DoctrineEntityManipulator $doctrineEntityManipulator
) {
$this->parsedNodesByType = $parsedNodesByType;
$this->classManipulator = $classManipulator;
$this->nameResolver = $nameResolver;
$this->callableNodeTraverser = $callableNodeTraverser;
$this->doctrineEntityManipulator = $doctrineEntityManipulator;
}

/**
Expand All @@ -68,6 +76,10 @@ public function provideSetterOnlyPropertiesAndMethodsByType(): array

// 1. setter only properties by class
$assignOnlyPrivatePropertyNames = $this->classManipulator->getAssignOnlyPrivatePropertyNames($class);

// filter out ManyToOne entities
$manyToOnePropertyNames = $this->doctrineEntityManipulator->resolveManyToOnePropertyNames($class);
$assignOnlyPrivatePropertyNames = array_diff($assignOnlyPrivatePropertyNames, $manyToOnePropertyNames);
if ($assignOnlyPrivatePropertyNames) {
$this->propertiesAndMethodsToRemoveByType[$type]['properties'] = $assignOnlyPrivatePropertyNames;
}
Expand Down
43 changes: 40 additions & 3 deletions packages/DeadCode/src/Doctrine/DoctrineEntityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\NamespaceAnalyzer;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class DoctrineEntityManipulator
{
Expand All @@ -28,11 +29,16 @@ final class DoctrineEntityManipulator
*/
private const RELATION_ANNOTATIONS = [
'Doctrine\ORM\Mapping\OneToMany',
'Doctrine\ORM\Mapping\ManyToOne',
self::MANY_TO_ONE_ANNOTATION,
'Doctrine\ORM\Mapping\OneToOne',
'Doctrine\ORM\Mapping\ManyToMany',
];

/**
* @var string
*/
private const MANY_TO_ONE_ANNOTATION = 'Doctrine\ORM\Mapping\ManyToOne';

/**
* @var string
*/
Expand All @@ -53,10 +59,19 @@ final class DoctrineEntityManipulator
*/
private $namespaceAnalyzer;

public function __construct(DocBlockManipulator $docBlockManipulator, NamespaceAnalyzer $namespaceAnalyzer)
{
/**
* @var NameResolver
*/
private $nameResolver;

public function __construct(
DocBlockManipulator $docBlockManipulator,
NamespaceAnalyzer $namespaceAnalyzer,
NameResolver $nameResolver
) {
$this->docBlockManipulator = $docBlockManipulator;
$this->namespaceAnalyzer = $namespaceAnalyzer;
$this->nameResolver = $nameResolver;
}

public function resolveTargetClass(Property $property): ?string
Expand Down Expand Up @@ -164,4 +179,26 @@ public function isNullableRelation(Property $property): bool

return false;
}

/**
* @return string[]
*/
public function resolveManyToOnePropertyNames(Class_ $class): array
{
$manyToOnePropertyNames = [];

foreach ($class->stmts as $stmt) {
if (! $stmt instanceof Property) {
continue;
}

if (! $this->docBlockManipulator->hasTag($stmt, self::MANY_TO_ONE_ANNOTATION)) {
continue;
}

$manyToOnePropertyNames[] = $this->nameResolver->getName($stmt);
}

return $manyToOnePropertyNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\Fixture;

use Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\Source\PostTemplate;

use Doctrine\ORM\Mapping as ORM;

class KeepManyToOne
{
/**
* @var PostTemplate
* @ORM\ManyToOne(targetEntity="Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\Source\PostTemplate", inversedBy="postTemplateBuildings", cascade={"persist", "merge"})
*/
private $postTemplate;

public function setPostTemplate(PostTemplate $postTemplate)
{
$this->postTemplate = $postTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function test(): void
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/in_constructor.php.inc',
__DIR__ . '/Fixture/keep_many_to_one.php.inc',
__DIR__ . '/Fixture/keep_static_property.php.inc',
__DIR__ . '/Fixture/keep_public_property.php.inc',
__DIR__ . '/Fixture/keep_serializable_object.php.inc',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\Source;

final class PostTemplate
{

}