rector/rules/Transform/Rector/Class_/AddInterfaceByTraitRector.php

95 lines
2.8 KiB
PHP
Raw Normal View History

2019-12-19 22:07:06 +00:00
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\Class_;
2019-12-19 22:07:06 +00:00
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use RectorPrefix20220607\Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use RectorPrefix20220607\Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20220607\Webmozart\Assert\Assert;
2019-12-19 22:07:06 +00:00
/**
* @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByTraitRector\AddInterfaceByTraitRectorTest
2019-12-19 22:07:06 +00:00
*/
final class AddInterfaceByTraitRector extends AbstractRector implements ConfigurableRectorInterface
2019-12-19 22:07:06 +00:00
{
/**
* @var array<string, string>
2019-12-19 22:07:06 +00:00
*/
private $interfaceByTrait = [];
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(ReflectionResolver $reflectionResolver)
{
$this->reflectionResolver = $reflectionResolver;
}
public function getRuleDefinition() : RuleDefinition
2019-12-19 22:07:06 +00:00
{
return new RuleDefinition('Add interface by used trait', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
2019-12-19 22:07:06 +00:00
class SomeClass
{
use SomeTrait;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2019-12-19 22:07:06 +00:00
class SomeClass implements SomeInterface
{
use SomeTrait;
}
CODE_SAMPLE
, ['SomeTrait' => 'SomeInterface'])]);
2019-12-19 22:07:06 +00:00
}
/**
* @return array<class-string<Node>>
2019-12-19 22:07:06 +00:00
*/
public function getNodeTypes() : array
2019-12-19 22:07:06 +00:00
{
return [Class_::class];
2019-12-19 22:07:06 +00:00
}
/**
* @param Class_ $node
2019-12-19 22:07:06 +00:00
*/
public function refactor(Node $node) : ?Node
2019-12-19 22:07:06 +00:00
{
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (!$classReflection instanceof ClassReflection) {
2019-12-19 22:07:06 +00:00
return null;
}
$hasChanged = \false;
foreach ($this->interfaceByTrait as $traitName => $interfaceName) {
if (!$classReflection->hasTraitUse($traitName)) {
2020-05-03 14:16:10 +00:00
continue;
}
foreach ($node->implements as $implement) {
if ($this->isName($implement, $interfaceName)) {
continue 2;
}
2019-12-19 22:07:06 +00:00
}
$node->implements[] = new FullyQualified($interfaceName);
$hasChanged = \true;
}
if (!$hasChanged) {
return null;
2019-12-19 22:07:06 +00:00
}
return $node;
}
/**
* @todo complex configuration, introduce value object!
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 23:39:41 +00:00
{
Assert::allString(\array_keys($configuration));
Assert::allString($configuration);
$this->interfaceByTrait = $configuration;
2020-07-29 23:39:41 +00:00
}
2019-12-19 22:07:06 +00:00
}