Skip to content

Commit

Permalink
Make RemoveDumpDataDeadCodeRector rule configurable (#258)
Browse files Browse the repository at this point in the history
Co-authored-by: Geni Jaho <[email protected]>
  • Loading branch information
jivanf and GeniJaho authored Oct 17, 2024
1 parent 3fbbe3f commit 84edb4a
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 @@ -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(
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 84edb4a

Please sign in to comment.