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: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ parameters:
- '#Class "Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\Transform\\Rector\\Class_\\AddInterfaceByTraitRector" is missing @see annotation with test case class reference#'

# @todo fix in phpstan-rules
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

49 changes: 8 additions & 41 deletions rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
namespace Rector\Transform\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStan\ScopeFetcher;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @api used in rector-doctrine
* @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByTraitRector\AddInterfaceByTraitRectorTest
* @deprecated This rule is deprecated, as too niche. Pairing a trait with an interface is specific to a single project or package, and the extra conditions it needs in practice are easier to express in a custom rule.
*/
final class AddInterfaceByTraitRector extends AbstractRector implements ConfigurableRectorInterface
final class AddInterfaceByTraitRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, string>
*/
private array $interfaceByTrait = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add interface by used trait', [
Expand Down Expand Up @@ -64,41 +56,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

$hasChanged = false;
foreach ($this->interfaceByTrait as $traitName => $interfaceName) {
if (! $classReflection->hasTraitUse($traitName)) {
continue;
}

if ($classReflection->implementsInterface($interfaceName)) {
continue;
}

$node->implements[] = new FullyQualified($interfaceName);
$hasChanged = true;
}

if (! $hasChanged) {
return null;
}

return $node;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche; use a custom rule scoped to your own trait and interface pair instead',
self::class
));
}

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

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