From eda0be149a2fe109f3ba27ad99c9092ac5ce74d3 Mon Sep 17 00:00:00 2001 From: Jivan Flores Date: Sat, 12 Oct 2024 21:12:44 -0700 Subject: [PATCH] Make RemoveDumpDataDeadCodeRector rule configurable --- docs/rector_rules_overview.md | 2 ++ .../FuncCall/RemoveDumpDataDeadCodeRector.php | 21 +++++++++++++++++-- .../config/configured_rule.php | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 4acd4725..24aba2a5 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -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 diff --git a/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php b/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php index b90bc78c..66205335 100644 --- a/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php +++ b/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php @@ -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( @@ -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; + } } diff --git a/tests/Rector/FuncCall/RemoveDumpDataDeadCodeRector/config/configured_rule.php b/tests/Rector/FuncCall/RemoveDumpDataDeadCodeRector/config/configured_rule.php index 444b262c..e81376d9 100644 --- a/tests/Rector/FuncCall/RemoveDumpDataDeadCodeRector/config/configured_rule.php +++ b/tests/Rector/FuncCall/RemoveDumpDataDeadCodeRector/config/configured_rule.php @@ -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']); };