2019-12-19 22:07:06 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Transform\Rector\Class_;
|
2019-12-19 22:07:06 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-09-02 21:04:18 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Webmozart\Assert\Assert;
|
2019-12-19 22:07:06 +00:00
|
|
|
/**
|
2023-06-08 18:28:01 +00:00
|
|
|
* @api used in rector-doctrine
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByTraitRector\AddInterfaceByTraitRectorTest
|
2019-12-19 22:07:06 +00:00
|
|
|
*/
|
2022-09-02 21:04:18 +00:00
|
|
|
final class AddInterfaceByTraitRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
|
2019-12-19 22:07:06 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-03-07 21:07:50 +00:00
|
|
|
* @var array<string, string>
|
2019-12-19 22:07:06 +00:00
|
|
|
*/
|
|
|
|
private $interfaceByTrait = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-12-19 22:07:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +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;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-12-19 22:07:06 +00:00
|
|
|
class SomeClass implements SomeInterface
|
|
|
|
{
|
|
|
|
use SomeTrait;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['SomeTrait' => 'SomeInterface'])]);
|
2019-12-19 22:07:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-12-19 22:07:06 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-12-19 22:07:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2019-12-19 22:07:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2019-12-19 22:07:06 +00:00
|
|
|
*/
|
2022-09-02 21:04:18 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2019-12-19 22:07:06 +00:00
|
|
|
{
|
2022-09-02 21:04:18 +00:00
|
|
|
$classReflection = $scope->getClassReflection();
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
2019-12-19 22:07:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \false;
|
2021-03-07 21:07:50 +00:00
|
|
|
foreach ($this->interfaceByTrait as $traitName => $interfaceName) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$classReflection->hasTraitUse($traitName)) {
|
2020-05-03 14:16:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-02 21:04:18 +00:00
|
|
|
if ($classReflection->implementsInterface($interfaceName)) {
|
|
|
|
continue;
|
2019-12-19 22:07:06 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->implements[] = new FullyQualified($interfaceName);
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
2019-12-19 22:07:06 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2021-03-07 21:07:50 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-03-07 21:07:50 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString(\array_keys($configuration));
|
|
|
|
Assert::allString($configuration);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->interfaceByTrait = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2019-12-19 22:07:06 +00:00
|
|
|
}
|