mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[Transform] Remove AddInterfaceByParentRector as never used (#1934)
This commit is contained in:
parent
117af55f96
commit
a2422d7937
|
@ -1,4 +1,4 @@
|
|||
# 506 Rules Overview
|
||||
# 505 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -86,7 +86,7 @@
|
|||
|
||||
- [Strict](#strict) (5)
|
||||
|
||||
- [Transform](#transform) (36)
|
||||
- [Transform](#transform) (35)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (23)
|
||||
|
||||
|
@ -9893,40 +9893,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
<br>
|
||||
|
||||
### AddInterfaceByParentRector
|
||||
|
||||
Add interface by parent
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\Transform\Rector\Class_\AddInterfaceByParentRector`](../rules/Transform/Rector/Class_/AddInterfaceByParentRector.php)
|
||||
|
||||
```php
|
||||
use Rector\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(AddInterfaceByParentRector::class)
|
||||
->configure([
|
||||
'SomeParent' => 'SomeInterface',
|
||||
]);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
-class SomeClass extends SomeParent
|
||||
+class SomeClass extends SomeParent implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### AddInterfaceByTraitRector
|
||||
|
||||
Add interface by used trait
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class AddInterfaceByParentRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SomeClass extends SomeParent
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SomeClass extends SomeParent implements \Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface;
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
|
||||
class SkipExisting extends SomeParent implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Fixture;
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParentChild;
|
||||
|
||||
class SkipIndirectChild extends SomeParentChild
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
class SomeParent
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
|
||||
|
||||
class SomeParentChild extends SomeParent
|
||||
{
|
||||
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeInterface;
|
||||
use Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source\SomeParent;
|
||||
use Rector\Transform\Rector\Class_\AddInterfaceByParentRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(AddInterfaceByParentRector::class)
|
||||
->configure([
|
||||
SomeParent::class => SomeInterface::class,
|
||||
]);
|
||||
};
|
|
@ -1,119 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Transform\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\AddInterfaceByParentRectorTest
|
||||
*/
|
||||
final class AddInterfaceByParentRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private array $interfaceByParent = [];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add interface by parent', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends SomeParent
|
||||
{
|
||||
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass extends SomeParent implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
'SomeParent' => 'SomeInterface',
|
||||
]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$parentClassReflection = $this->resolveParentClassReflection($node);
|
||||
|
||||
if (! $parentClassReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hasChanged = false;
|
||||
foreach ($this->interfaceByParent as $parentName => $interfaceName) {
|
||||
if ($parentName !== $parentClassReflection->getName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($node->implements as $implement) {
|
||||
if ($this->isName($implement, $interfaceName)) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$node->implements[] = new FullyQualified($interfaceName);
|
||||
$hasChanged = true;
|
||||
}
|
||||
|
||||
if (! $hasChanged) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
*/
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
Assert::allString(array_keys($configuration));
|
||||
Assert::allString($configuration);
|
||||
|
||||
$this->interfaceByParent = $configuration;
|
||||
}
|
||||
|
||||
private function resolveParentClassReflection(Class_ $class): ?ClassReflection
|
||||
{
|
||||
/** @var Scope $scope */
|
||||
$scope = $class->getAttribute(AttributeKey::SCOPE);
|
||||
|
||||
$classReflection = $scope->getClassReflection();
|
||||
if (! $classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $classReflection->getParentClass();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user