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 608aa94
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

/**
* @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 RectorPrefix202405\Webmozart\Assert\Assert;

Check failure on line 17 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Referencing prefixed Rector class: RectorPrefix202405\Webmozart\Assert\Assert.
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 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\Expression $node
*/
public function refactor(Node $node): ?int
{
/** @var $node Class_ */
if ($node->implements === []) {

Check failure on line 75 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Access to an undefined property PhpParser\Node\Stmt\Expression::$implements.
return null;
}

foreach ($node->implements as $implement) {
if ($this->isNames($implement, $this->interfacesToRemove)) {
foreach ($this->interfacesToRemove as $interface) {
$ref = new ReflectionClass($interface);

Check failure on line 82 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Parameter #1 $objectOrClass of class ReflectionClass constructor expects class-string<T of object>|T of object, string given.
$methods = array_map(static fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->getName(), $ref->getMethods());
foreach ($methods as $methodToRemove) {
foreach ($node->stmts as $key => $stmt) {

Check failure on line 85 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Access to an undefined property PhpParser\Node\Stmt\Expression::$stmts.
if ($stmt instanceof ClassMethod && $this->isName($stmt, $methodToRemove)) {
unset($node->stmts[$key]);
}
}
}
}
}
}

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

Check failure on line 95 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Parameter #1 $node of method Rector\Removing\Rector\Class_\RemoveInterfacesRector::refactor() expects PhpParser\Node\Stmt\Class_, PhpParser\Node\Stmt\Expression given.

return null;
}

public function configure(array $configuration): void
{
Assert::allString($configuration);

Check failure on line 102 in src/lib/Rule/Internal/RemoveInterfaceWithMethods.php

View workflow job for this annotation

GitHub Actions / Tests (8.3)

Referencing prefixed Rector class: RectorPrefix202405\Webmozart\Assert\Assert.
/** @var string[] $configuration */
$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 608aa94

Please sign in to comment.