[PseudoNamespaceToNamespaceRector] fix class rename

This commit is contained in:
TomasVotruba 2017-11-18 03:26:25 +01:00
parent 79d665decf
commit d55c97be88
4 changed files with 36 additions and 8 deletions

View File

@ -13,11 +13,6 @@ use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
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
*/
final class PseudoNamespaceToNamespaceRector extends AbstractRector
{
/**
@ -81,6 +76,11 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
$parentNode = $nameOrIdentifierNode->getAttribute(Attribute::PARENT_NODE);
$lastNewNamePart = $newNameParts[count($newNameParts) - 1];
// do not rename classes
if ($parentNode instanceof Class_) {
return null;
}
if ($nameOrIdentifierNode instanceof Name) {
if ($parentNode instanceof UseUse) {
$this->oldToNewUseStatements[$oldName] = $lastNewNamePart;
@ -93,7 +93,7 @@ final class PseudoNamespaceToNamespaceRector extends AbstractRector
return $nameOrIdentifierNode;
}
if ($nameOrIdentifierNode instanceof Identifier && $parentNode instanceof Class_) {
if ($nameOrIdentifierNode instanceof Identifier) {
$namespaceParts = $newNameParts;
array_pop($namespaceParts);

View File

@ -13,16 +13,18 @@ final class Test extends AbstractConfigurableRectorTestCase
__DIR__ . '/wrong/wrong.php.inc',
__DIR__ . '/correct/correct.php.inc'
);
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/wrong/wrong2.php.inc',
__DIR__ . '/correct/correct2.php.inc'
);
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/wrong/wrong3.php.inc',
__DIR__ . '/correct/correct3.php.inc'
);
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/wrong/wrong4.php.inc',
__DIR__ . '/correct/correct4.php.inc'
);
}
protected function provideConfig(): string

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
class PHPUnit_TestCase
{
/**
* @return Synapse|\PHPUnit_Framework_MockObject_MockObject
*/
public function getSynapseMock(int $output = 2): \PHPUnit_Framework_MockObject_MockObject
{
}
}

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
class PHPUnit_TestCase
{
/**
* @return Synapse|\PHPUnit_Framework_MockObject_MockObject
*/
public function getSynapseMock(int $output = 2): \PHPUnit_Framework_MockObject_MockObject
{
}
}