Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ parameters:
identifier: rector.registerRector
paths:
- rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php
- rules/Removing/Rector/Class_/RemoveTraitUseRector.php
- rules/Transform/Rector/String_/StringToClassConstantRector.php

# lack of generic array in nikic/php-parser
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

47 changes: 8 additions & 39 deletions rules/Removing/Rector/Class_/RemoveTraitUseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\Node\Stmt\TraitUse;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Removing\Rector\Class_\RemoveTraitUseRector\RemoveTraitUseRectorTest
* @deprecated This rule is deprecated, as too niche, never used and needs project context. Write a custom rule for the exact project case instead.
*/
final class RemoveTraitUseRector extends AbstractRector implements ConfigurableRectorInterface
final class RemoveTraitUseRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var string[]
*/
private array $traitsToRemove = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove specific traits from code', [
Expand Down Expand Up @@ -59,42 +54,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof TraitUse) {
continue;
}

foreach ($stmt->traits as $traitKey => $trait) {
if (! $this->isNames($trait, $this->traitsToRemove)) {
continue;
}

unset($stmt->traits[$traitKey]);
$hasChanged = true;
}

// remove empty trait uses
if ($stmt->traits === []) {
unset($node->stmts[$key]);
}
}

if ($hasChanged) {
return $node;
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead',
self::class
));
}

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

$this->traitsToRemove = $configuration;
}
}
Loading