Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMS Plugin namespace/use change #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/sets/sylius/cms/cms-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Sylius\SyliusRector\Rector\Namespace\ChangeNamespaceRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ChangeNamespaceRector::class, [
'BitBag\SyliusCmsPlugin' => 'Sylius\CmsPlugin',
]);
};
82 changes: 82 additions & 0 deletions src/Rector/Namespace/ChangeNamespaceRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Sylius\SyliusRector\Rector\Namespace;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ChangeNamespaceRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<string, string> Array where the key is the old namespace, and the value is the new namespace
*/
private array $namespaceChanges = [];

public function configure(array $configuration): void
{
$this->namespaceChanges = $configuration;
}

public function getNodeTypes(): array
{
return [Namespace_::class, Use_::class];
}

public function refactor(Node $node): ?Node
{
if ($node instanceof Namespace_) {
$namespaceName = $this->getName($node);
if ($namespaceName === null) {
return null;
}

foreach ($this->namespaceChanges as $oldNamespace => $newNamespace) {
if (str_starts_with($namespaceName, $oldNamespace)) {
$node->name = new Name(
str_replace($oldNamespace, $newNamespace, $namespaceName)
);

break;
}
}

return $node;
}

// Handle "use" statements
if ($node instanceof Use_) {
/** @var UseUse $use */
foreach ($node->uses as $use) {
$useName = $this->getName($use->name);

foreach ($this->namespaceChanges as $oldNamespace => $newNamespace) {
if (str_starts_with($useName, $oldNamespace)) {
$use->name = new Name(
str_replace($oldNamespace, $newNamespace, $useName)
);
}
}
}

return $node;
}

return null;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change specified namespaces and use statements',
[]
);
}
}
10 changes: 10 additions & 0 deletions src/Set/SyliusCms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sylius\SyliusRector\Set;

final class SyliusCms
{
public const CMS_PLUGIN = __DIR__ . '/../../config/sets/sylius/cms/cms-plugin.php';
}
32 changes: 32 additions & 0 deletions tests/Set/CmsPlugin/CmsPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Sylius\SyliusRector\Tests\Set\CmsPlugin;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CmsPluginTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

/**
* @return Iterator<string>
*/
public function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
25 changes: 25 additions & 0 deletions tests/Set/CmsPlugin/Fixture/class_namespace_change.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace BitBag\SyliusCmsPlugin\Some\Namespace;

use BitBag\SyliusCmsPlugin\Another\Namespace;

class SomeClass
{

}

?>
-----
<?php

namespace Sylius\CmsPlugin\Some\Namespace;

use Sylius\CmsPlugin\Another\Namespace;

class SomeClass
{

}

?>
11 changes: 11 additions & 0 deletions tests/Set/CmsPlugin/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Sylius\SyliusRector\Set\SyliusCms;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../config/config.php');
$rectorConfig->sets([SyliusCms::CMS_PLUGIN]);
};
Loading