mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-22 16:32:27 +02:00
skip serialized props
This commit is contained in:
parent
73ac67d5ef
commit
2847bf2b8f
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector\Fixture;
|
||||
|
||||
use JMS\Serializer\Annotation as Serializer;
|
||||
|
||||
final class KeepSerialiazableObject
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @Serializer\Type("string")
|
||||
*/
|
||||
private $id;
|
||||
|
||||
public function __construct(string $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ final class RemoveSetterOnlyPropertyAndMethodCallRectorTest extends AbstractRect
|
||||
__DIR__ . '/Fixture/in_constructor.php.inc',
|
||||
__DIR__ . '/Fixture/keep_static_property.php.inc',
|
||||
__DIR__ . '/Fixture/keep_public_property.php.inc',
|
||||
__DIR__ . '/Fixture/keep_serializable_object.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ use PhpParser\Node\Stmt\PropertyProperty;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PhpParser\Node\Stmt\TraitUse;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\PhpParser\Node\Commander\NodeRemovingCommander;
|
||||
use Rector\PhpParser\Node\NodeFactory;
|
||||
@ -58,13 +59,19 @@ final class ClassManipulator
|
||||
*/
|
||||
private $nodeRemovingCommander;
|
||||
|
||||
/**
|
||||
* @var DocBlockManipulator
|
||||
*/
|
||||
private $docBlockManipulator;
|
||||
|
||||
public function __construct(
|
||||
NameResolver $nameResolver,
|
||||
NodeFactory $nodeFactory,
|
||||
ChildAndParentClassManipulator $childAndParentClassManipulator,
|
||||
BetterNodeFinder $betterNodeFinder,
|
||||
CallableNodeTraverser $callableNodeTraverser,
|
||||
NodeRemovingCommander $nodeRemovingCommander
|
||||
NodeRemovingCommander $nodeRemovingCommander,
|
||||
DocBlockManipulator $docBlockManipulator
|
||||
) {
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->nameResolver = $nameResolver;
|
||||
@ -72,6 +79,7 @@ final class ClassManipulator
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->callableNodeTraverser = $callableNodeTraverser;
|
||||
$this->nodeRemovingCommander = $nodeRemovingCommander;
|
||||
$this->docBlockManipulator = $docBlockManipulator;
|
||||
}
|
||||
|
||||
public function addConstructorDependency(Class_ $classNode, VariableInfo $variableInfo): void
|
||||
@ -367,7 +375,10 @@ final class ClassManipulator
|
||||
$propertyNonAssignNames[] = $this->nameResolver->getName($node);
|
||||
});
|
||||
|
||||
return array_diff($privatePropertyNames, $propertyNonAssignNames);
|
||||
// skip serializable properties, because they are probably used in serialization even though assign only
|
||||
$serializablePropertyNames = $this->getSerializablePropertyNames($node);
|
||||
|
||||
return array_diff($privatePropertyNames, $propertyNonAssignNames, $serializablePropertyNames);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -526,4 +537,27 @@ final class ClassManipulator
|
||||
|
||||
return $parentNode instanceof Assign && $parentNode->var === $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getSerializablePropertyNames(Class_ $node): array
|
||||
{
|
||||
$serializablePropertyNames = [];
|
||||
$this->callableNodeTraverser->traverseNodesWithCallable([$node], function (Node $node) use (
|
||||
&$serializablePropertyNames
|
||||
): void {
|
||||
if (! $node instanceof Property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->docBlockManipulator->hasTag($node, 'JMS\Serializer\Annotation\Type')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$serializablePropertyNames[] = $this->nameResolver->getName($node);
|
||||
});
|
||||
|
||||
return $serializablePropertyNames;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user