Skip to content

Commit

Permalink
Remove removeNode() in various rules (#4080)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 5, 2023
1 parent efc3cf3 commit b743de6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
11 changes: 5 additions & 6 deletions rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeTraverser;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -56,9 +57,9 @@ public function getNodeTypes(): array

/**
* @param Expression $node
* @return Node[]|Node|null
* @return Node[]|Node|null|int
*/
public function refactor(Node $node): array|Node|null
public function refactor(Node $node): array|Node|null|int
{
if ($this->hasGetMagic($node)) {
return null;
Expand Down Expand Up @@ -102,7 +103,7 @@ private function hasGetMagic(Expression $expression): bool
return ! $phpPropertyReflection instanceof PhpPropertyReflection;
}

private function removeNodeAndKeepComments(Expression $expression): ?Node
private function removeNodeAndKeepComments(Expression $expression): int|Node
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);

Expand All @@ -113,8 +114,6 @@ private function removeNodeAndKeepComments(Expression $expression): ?Node
return $nop;
}

$this->removeNode($expression);

return null;
return NodeTraverser::REMOVE_NODE;
}
}
25 changes: 9 additions & 16 deletions rules/Removing/Rector/FuncCall/RemoveFuncCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeTraverser;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -52,15 +53,20 @@ public function getNodeTypes(): array
/**
* @param Expression $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?int
{
$expr = $node->expr;

if (! $expr instanceof FuncCall) {
return null;
}

$this->removeNodeIfNeeded($node, $expr);
foreach ($this->removedFunctions as $removedFunction) {
if (! $this->isName($expr->name, $removedFunction)) {
continue;
}

return NodeTraverser::REMOVE_NODE;
}

return null;
}
Expand All @@ -73,17 +79,4 @@ public function configure(array $configuration): void
Assert::allString($configuration);
$this->removedFunctions = $configuration;
}

private function removeNodeIfNeeded(Expression $expression, FuncCall $funcCall): void
{
foreach ($this->removedFunctions as $removedFunction) {
if (! $this->isName($funcCall->name, $removedFunction)) {
continue;
}

$this->removeNode($expression);

break;
}
}
}

0 comments on commit b743de6

Please sign in to comment.