Skip to content

Commit

Permalink
Make RemoveDumpDataDeadCodeRector rule configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jivanf committed Oct 13, 2024
1 parent a4fb100 commit eda0be1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,8 @@ refactors calls with the pre Laravel 11 methods for blueprint geometry columns

It will removes the dump data just like dd or dump functions from the code.`

:wrench: **configure it!**

- class: [`RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector`](../src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php)

```diff
Expand Down
21 changes: 19 additions & 2 deletions src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveDumpDataDeadCodeRector\RemoveDumpDataDeadCodeRectorTest
*/
final class RemoveDumpDataDeadCodeRector extends AbstractRector
final class RemoveDumpDataDeadCodeRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string[]
*/
private array $dumpFunctionNames = ['dd', 'dump'];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -76,10 +83,20 @@ public function refactor(Node $node): int|Node|array|null
return null;
}

if (! $this->isNames($node->expr->name, ['dd', 'dump'])) {
if (! $this->isNames($node->expr->name, $this->dumpFunctionNames)) {
return null;
}

return NodeTraverser::REMOVE_NODE;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allString($configuration);

$this->dumpFunctionNames = $configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RemoveDumpDataDeadCodeRector::class);
$rectorConfig->ruleWithConfiguration(RemoveDumpDataDeadCodeRector::class, ['dd', 'dump']);
};

0 comments on commit eda0be1

Please sign in to comment.