Skip to content

Commit

Permalink
IBX-8118: Implemented RemoveLegacyClassAliasRector rule (#2)
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
alongosz authored May 10, 2024
1 parent 303e0c6 commit b1c7fdb
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/lib/Rule/Internal/RemoveLegacyClassAliasRector.php
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;
}
}
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
{
}
}

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

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

?>
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');

?>
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';
}
}
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);
};

0 comments on commit b1c7fdb

Please sign in to comment.