mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
PseudoNamespaceRector init
This commit is contained in:
parent
ce8e059e44
commit
14cab79628
60
src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php
Normal file
60
src/Rector/Dynamic/PseudoNamespaceToNamespaceRector.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Rector\Rector\Dynamic;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Basically inversion of https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown#example-converting-namespaced-code-to-pseudo-namespaces
|
||||
*
|
||||
*
|
||||
* Requested on SO: https://stackoverflow.com/questions/29014957/converting-pseudo-namespaced-classes-to-use-real-namespace
|
||||
*
|
||||
* @todo might handle current @see phpunit60.yml @see \Rector\Rector\Dynamic\NamespaceReplacerRector
|
||||
*/
|
||||
final class PseudoNamespaceToNamespaceRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $pseudoNamespacePrefixes = [];
|
||||
|
||||
public function __construct(array $pseudoNamespacePrefixes)
|
||||
{
|
||||
$this->pseudoNamespacePrefixes = $pseudoNamespacePrefixes;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->pseudoNamespacePrefixes as $pseudoNamespacePrefix) {
|
||||
if (Strings::startsWith($node->toString(), $pseudoNamespacePrefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Name $nameNode
|
||||
* @return null|Node
|
||||
*/
|
||||
public function refactor(Node $nameNode): ?Node
|
||||
{
|
||||
$oldName = $nameNode->toString();
|
||||
$newName = str_replace('_', '\\', $oldName);
|
||||
$newNameParts = explode('\\', $newName);
|
||||
|
||||
$nameNode->parts = $newNameParts;
|
||||
|
||||
return $nameNode;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Dynamic\PseudoNamespaceToNamespaceRector;
|
||||
|
||||
use Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector;
|
||||
use Rector\Testing\PHPUnit\AbstractConfigurableRectorTestCase;
|
||||
|
||||
final class Test extends AbstractConfigurableRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config/rector.yml';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [PseudoNamespaceToNamespaceRector::class];
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
rectors:
|
||||
Rector\Rector\Dynamic\PseudoNamespaceToNamespaceRector:
|
||||
- 'PHPUnit_'
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace OldNamespace\SubNamespace;
|
||||
|
||||
class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace OldNamespace\SubNamespace;
|
||||
|
||||
class SomeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user