Skip to content

Commit

Permalink
[DeadCode] Remove previous and parent attribute usage on RemoveNullPr…
Browse files Browse the repository at this point in the history
…opertyInitializationRector (#3541)
  • Loading branch information
samsonasik committed Mar 30, 2023
1 parent fd2e1a1 commit 58d862b
Showing 1 changed file with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use function strtolower;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -50,42 +47,41 @@ class SunshineCommand extends ParentClassWithNewConstructor
*/
public function getNodeTypes(): array
{
return [PropertyProperty::class];
return [Property::class];
}

/**
* @param PropertyProperty $node
* @param Property $node
*/
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

// skip typed properties
if ($parentNode instanceof Property && $parentNode->type !== null) {
if ($node->type instanceof Node) {
return null;
}

$defaultValueNode = $node->default;
if (! $defaultValueNode instanceof Expr) {
return null;
}

if (! $defaultValueNode instanceof ConstFetch) {
return null;
}
$hasChanged = false;
foreach ($node->props as $prop) {
$defaultValueNode = $prop->default;
if (! $defaultValueNode instanceof Expr) {
continue;
}

if (strtolower((string) $defaultValueNode->name) !== 'null') {
return null;
}
if (! $defaultValueNode instanceof ConstFetch) {
continue;
}

$nodeNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if (strtolower((string) $defaultValueNode->name) !== 'null') {
continue;
}

if ($nodeNode instanceof NullableType) {
return null;
$prop->default = null;
$hasChanged = true;
}

$node->default = null;
if ($hasChanged) {
return $node;
}

return $node;
return null;
}
}

0 comments on commit 58d862b

Please sign in to comment.