Skip to content

Commit

Permalink
IBX-8224: Added rector rule to remove interface & methods implementat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
ViniTou committed Jun 17, 2024
1 parent a79fea7 commit 28ff635
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/lib/Rule/Internal/RemoveInterfaceWithMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

Check warning on line 1 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Run code style check (8.3)

Found violation(s) of type: phpdoc_no_empty_return

Check warning on line 1 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Run code style check (8.3)

Found violation(s) of type: no_unused_imports

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rector\Rule\Internal;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use ReflectionClass;
use ReflectionMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class RemoveInterfaceWithMethods extends AbstractRector implements ConfigurableRectorInterface
{
private RemoveInterfacesRector $removeInterfacesRector;

/** @var class-string[] */
private array $interfacesToRemove;

public function __construct(RemoveInterfacesRector $removeInterfacesRector)
{
$this->removeInterfacesRector = $removeInterfacesRector;
}

/**
* @throws \Exception
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove Interface implementation with all its methods', [new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClassWithInterface implements InterfaceToRemove
{
public function interfaceMethod(): array
{
return ['smth'];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClassWithInterface
{
}
CODE_SAMPLE
,
['var_dump']
)]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return $this->removeInterfacesRector->getNodeTypes();
}

/**
* @param \PhpParser\Node\Stmt\Class_ $node
*/
public function refactor(Node $node): ?int
{
if ($node->implements === []) {
return null;
}

foreach ($node->implements as $implement) {
if ($this->isNames($implement, $this->interfacesToRemove)) {
foreach ($this->interfacesToRemove as $interface) {
$ref = new ReflectionClass($interface);
$methods = array_map(static fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->getName(), $ref->getMethods());
foreach ($methods as $methodToRemove) {
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof ClassMethod && $this->isName($stmt, $methodToRemove)) {
unset($node->stmts[$key]);
}
}
}
}
}
}

$this->removeInterfacesRector->refactor($node);

return null;
}

/**
* @param class-string[] $configuration
* @return void
*/
public function configure(array $configuration): void
{
$this->interfacesToRemove = $configuration;

$this->removeInterfacesRector->configure($configuration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;
use Symfony\Component\Console\Command\Command;

class SomeClass implements BackwardCompatibleCommand
{
public function firstMethod(): void
{

}

public function getDeprecatedAliases(): array
{
return ['ezplatform:some_old:command_with_interface'];
}

public function thirdMethod(): void
{

}
}

?>
-----
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;
use Symfony\Component\Console\Command\Command;

class SomeClass
{
public function firstMethod(): void
{

}
public function thirdMethod(): void
{

}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;

class SomeClass implements BackwardCompatibleCommand
{
public function getDeprecatedAliases(): array
{
return ['ezplatform:some_old:command_with_interface'];
}
}

?>
-----
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;

class SomeClass
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;
use OtherInterface;

class SomeClass implements BackwardCompatibleCommand, OtherInterface
{
public function firstMethod()
{

}

public function getDeprecatedAliases(): array
{
return ['ezplatform:some_old:command_with_interface'];
}
}

?>
-----
<?php /** @noinspection ALL */

use Ibexa\Bundle\Core\Command\BackwardCompatibleCommand;
use OtherInterface;

class SomeClass implements OtherInterface
{
public function firstMethod()
{

}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rector\Tests\Rule\Internal\RemoveInterfaceWithMethods;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @covers \Ibexa\Rector\Rule\Internal\RemoveLegacyClassAliasRector
*/
final class RemoveInterfaceWithMethodsTest extends AbstractRectorTestCase
{
/**
* @throws \Rector\Exception\ShouldNotHappenException
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

use Ibexa\Rector\Rule\Internal\RemoveInterfaceWithMethods;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(
RemoveInterfaceWithMethods::class,
['Ibexa\Bundle\Core\Command\BackwardCompatibleCommand']
);
};

0 comments on commit 28ff635

Please sign in to comment.