From 84edb4aa28280ea1ed73e1f52f761f9a8a0698f2 Mon Sep 17 00:00:00 2001 From: jivanf Date: Thu, 17 Oct 2024 08:46:20 -0700 Subject: [PATCH] Make RemoveDumpDataDeadCodeRector rule configurable (#258) Co-authored-by: Geni Jaho --- 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..6b8a6b23 100644 --- a/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php +++ b/src/Rector/FuncCall/RemoveDumpDataDeadCodeRector.php @@ -8,15 +8,22 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Expression; use PhpParser\NodeTraverser; +use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; 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']); };