[Rector] NetteObject - add test case for namespaced object

This commit is contained in:
TomasVotruba 2017-08-21 12:21:50 +02:00
parent 0e20f778c6
commit c8473aa0b4
3 changed files with 42 additions and 15 deletions

View File

@ -37,20 +37,17 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
public function isCandidate(Node $node): bool
{
if ($node instanceof Class_) {
if (! $node->extends) {
return false;
}
$parentClassName = (string) $node->extends;
if ($parentClassName !== 'Nette\Object') {
return false;
}
return true;
if (! $node instanceof Class_) {
return false;
}
return false;
if (! $node->extends) {
return false;
}
$parentClassName = $this->getParentClassName($node);
return $parentClassName === $this->getParentClass();
}
/**
@ -58,11 +55,10 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
*/
public function refactor(Node $classNode): ?Node
{
$traitUseNode = $this->createTraitUse('Nette\SmartObject');
$traitUseNode = $this->createTraitUse($this->getTraitName());
$this->statementGlue->addAsFirstTrait($classNode, $traitUseNode);
// remove parent class
$classNode->extends = null;
$this->removeParentClass($classNode);
return $classNode;
}
@ -73,4 +69,29 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
new FullyQualified($traitName),
]);
}
private function getParentClass(): string
{
return 'Nette\Object';
}
private function getTraitName(): string
{
return 'Nette\SmartObject';
}
private function getParentClassName(Class_ $classNode): string
{
$parentClass = $classNode->extends;
/** @var FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');
return $fqnParentClassName->toString();
}
private function removeParentClass(Class_ $classNode): void
{
$classNode->extends = null;
}
}

View File

@ -1,5 +1,7 @@
<?php declare (strict_types=1);
use Nette\Object;
class ClassWithExternalConstant
{
use \Nette\SmartObject;

View File

@ -21,6 +21,10 @@ final class Test extends AbstractRectorTestCase
__DIR__ . '/Wrong/wrong3.php.inc',
__DIR__ . '/Correct/correct3.php.inc'
);
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/Wrong/wrong4.php.inc',
__DIR__ . '/Correct/correct4.php.inc'
);
}
/**