[Transform] Remove AddInterfaceByParentRector as never used (#1934)

This commit is contained in:
Tomas Votruba 2022-03-15 16:56:36 +01:00 committed by GitHub
parent 117af55f96
commit a2422d7937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2 additions and 280 deletions

View File

@ -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

View File

@ -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';
}
}

View File

@ -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
{
}
?>

View File

@ -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
{
}

View File

@ -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
{
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
interface SomeInterface
{
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
class SomeParent
{
}

View File

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Transform\Rector\Class_\AddInterfaceByParentRector\Source;
class SomeParentChild extends SomeParent
{
}

View File

@ -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,
]);
};

View File

@ -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();
}
}