generated from ibexa/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-8224: Added rector rule to remove interface & methods implementat…
…ions
- Loading branch information
Showing
6 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Run code style check (8.3)
|
||
|
||
/** | ||
* @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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/lib/Rule/Internal/RemoveInterfaceWithMethods/Fixture/class_with_extra_methods.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
tests/lib/Rule/Internal/RemoveInterfaceWithMethods/Fixture/class_with_interface.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
tests/lib/Rule/Internal/RemoveInterfaceWithMethods/Fixture/class_with_two_interfaces.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
tests/lib/Rule/Internal/RemoveInterfaceWithMethods/RemoveInterfaceWithMethodsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/lib/Rule/Internal/RemoveInterfaceWithMethods/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
); | ||
}; |