mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[Rector] NetteObject - add test case for namespaced object
This commit is contained in:
parent
0e20f778c6
commit
c8473aa0b4
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
use Nette\Object;
|
||||
|
||||
class ClassWithExternalConstant
|
||||
{
|
||||
use \Nette\SmartObject;
|
||||
|
|
|
@ -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'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user