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.
Browse files
Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-8118 and #2 * [Rector] Defined internal rule to remove legacy class aliases * [Tests] Added coverage for the rule to remove class aliases
- Loading branch information
Showing
7 changed files
with
254 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,91 @@ | ||
<?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\NodeTraverser; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @internal This rule is internal, for Ibexa 1st party packages | ||
*/ | ||
final class RemoveLegacyClassAliasRector extends AbstractRector | ||
{ | ||
/** @var array<string> */ | ||
private const array FQCN_PREFIXES = ['eZ\\', 'EzSystems\\', 'Ibexa\\']; | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove legacy eZ Systems & Ibexa class_alias calls', [new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$x = 'something'; | ||
class_alias(Foo::class, 'eZ\Foo'); | ||
class_alias(Foo::class, 'Other\ThirdParty\Foo'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$x = 'something'; | ||
class_alias(Foo::class, 'Other\ThirdParty\Foo'); | ||
CODE_SAMPLE | ||
, | ||
['var_dump'] | ||
)]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Node\Stmt\Expression::class]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Stmt\Expression $node | ||
*/ | ||
public function refactor(Node $node): ?int | ||
{ | ||
$expr = $node->expr; | ||
if (!$expr instanceof Node\Expr\FuncCall) { | ||
return null; | ||
} | ||
|
||
$args = $expr->getArgs(); | ||
if (!$this->isName($expr->name, 'class_alias') || !$this->isLegacyClassAlias($args)) { | ||
return null; | ||
} | ||
|
||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Arg[] $args | ||
*/ | ||
private function isLegacyClassAlias(array $args): bool | ||
{ | ||
if (!isset($args[1]) || !$args[1]->value instanceof Node\Scalar\String_) { | ||
return false; | ||
} | ||
|
||
$value = $args[1]->value->value; | ||
|
||
foreach (self::FQCN_PREFIXES as $prefix) { | ||
if (str_starts_with($value, $prefix)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/Fixture/another_class.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,27 @@ | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyAnotherLegacyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
class_alias(MyAnotherLegacyClass::class, 'eZ\Some\Namespace\MyAnotherLegacyClass'); | ||
|
||
?> | ||
----- | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyAnotherLegacyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/Fixture/bc_ibexa_class.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,27 @@ | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyIbexaClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
class_alias(MyIbexaClass::class, 'Ibexa\Platform\MyAnotherLegacyClass'); | ||
|
||
?> | ||
----- | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyIbexaClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/Fixture/some_class.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,27 @@ | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyLegacyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
class_alias(MyLegacyClass::class, 'EzSystems\Some\Namespace\MyLegacyClass'); | ||
|
||
?> | ||
----- | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class MyLegacyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/Fixture/third_party_class.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,31 @@ | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class ThirdPartyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
// this is not ours, should remain | ||
class_alias(ThirdPartyClass::class, 'ThirdPartyVendor\Accidental\Ibexa\Collsion'); | ||
|
||
?> | ||
----- | ||
<?php /** @noinspection ALL */ | ||
|
||
namespace Ibexa\Rector\Tests\Rule\Internal\RemoveLegacyClassAliasRector\Fixture; | ||
|
||
class ThirdPartyClass | ||
{ | ||
public function foo(): void | ||
{ | ||
} | ||
} | ||
|
||
// this is not ours, should remain | ||
class_alias(ThirdPartyClass::class, 'ThirdPartyVendor\Accidental\Ibexa\Collsion'); | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/RemoveLegacyClassAliasRectorTest.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\RemoveLegacyClassAliasRector; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
/** | ||
* @covers \Ibexa\Rector\Rule\Internal\RemoveLegacyClassAliasRector | ||
*/ | ||
final class RemoveLegacyClassAliasRectorTest 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'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/lib/Rule/Internal/RemoveLegacyClassAliasRector/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,14 @@ | ||
<?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\RemoveLegacyClassAliasRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveLegacyClassAliasRector::class); | ||
}; |