This commit is contained in:
TomasVotruba 2017-08-07 17:52:18 +02:00
parent 23dde903b7
commit 7b03a0c30e
7 changed files with 25 additions and 11 deletions

View File

@ -11,5 +11,5 @@ interface RectorInterface
/** /**
* @param Node $node * @param Node $node
*/ */
public function refactor($node): void; public function refactor($node): ?Node;
} }

View File

@ -10,10 +10,16 @@ use Rector\Contract\Rector\RectorInterface;
abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
{ {
public function enterNode(Node $node): ?int /**
* @return int|null|Node
*/
public function enterNode(Node $node)
{ {
if ($this->isCandidate($node)) { if ($this->isCandidate($node)) {
$this->refactor($node); if ($newNode = $this->refactor($node)) {
return $newNode;
}
return NodeTraverser::DONT_TRAVERSE_CHILDREN; return NodeTraverser::DONT_TRAVERSE_CHILDREN;
} }

View File

@ -25,6 +25,7 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
public function enterNode(Node $node): ?int public function enterNode(Node $node): ?int
{ {
if ($this->isCandidate($node)) { if ($this->isCandidate($node)) {
return false;
dump($node); // get next node! dump($node); // get next node!
die; die;

View File

@ -56,13 +56,15 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
/** /**
* @param Class_ $classNode * @param Class_ $classNode
*/ */
public function refactor($classNode): void public function refactor($classNode): ?Node
{ {
// remove parent class // remove parent class
$classNode->extends = null; $classNode->extends = null;
$traitUseNode = $this->createTraitUse('Nette\SmartObject'); $traitUseNode = $this->createTraitUse('Nette\SmartObject');
$this->statementGlue->addAsFirstTrait($classNode, $traitUseNode); $this->statementGlue->addAsFirstTrait($classNode, $traitUseNode);
return null;
} }
private function createTraitUse(string $traitName): TraitUse private function createTraitUse(string $traitName): TraitUse

View File

@ -6,13 +6,16 @@ use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ClassConstFetch;
use Rector\Deprecation\SetNames; use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractRector; use Rector\Rector\AbstractRector;
use PhpParser\Node\Scalar\String_;
final class RemoveConfiguratorConstantsRector extends AbstractRector final class RemoveConfiguratorConstantsRector extends AbstractRector
{ {
public function isCandidate(Node $node): bool public function isCandidate(Node $node): bool
{ {
if ($node instanceof ClassConstFetch) { if ($node instanceof ClassConstFetch) {
if ((string) $node->class !== 'Nette\Configurator') { // @todo: check FQN namespace
$className = (string) $node->class;
if (! in_array($className, ['Nette\Configurator', 'Configurator'], true)) {
return false; return false;
} }
@ -29,12 +32,12 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
/** /**
* @param ClassConstFetch $classConstFetchNode * @param ClassConstFetch $classConstFetchNode
*/ */
public function refactor($classConstFetchNode): void public function refactor($classConstFetchNode): ?Node
{ {
dump($classConstFetchNode->name->name); $constantName = (string) $classConstFetchNode->name;
die; $string = strtolower($constantName);
$classConstFetchNode->name->name = $this->getNewConstantName(); return new String_($string);
} }
public function getSetName(): string public function getSetName(): string

View File

@ -4,6 +4,6 @@ class ClassWithExternalConstant
{ {
public function getValue() public function getValue()
{ {
return ClassWithConstants::NEW_CONSTANT; return 'development';
} }
} }

View File

@ -1,9 +1,11 @@
<?php declare (strict_types=1); <?php declare (strict_types=1);
use Nette\Configurator;
class ClassWithExternalConstant class ClassWithExternalConstant
{ {
public function getValue() public function getValue()
{ {
return ClassWithConstants::OLD_CONSTANT; return Configurator::DEVELOPMENT;
} }
} }