refactor NodeVisitor to Rectors

This commit is contained in:
TomasVotruba 2017-08-07 17:41:58 +02:00
parent bcc02f678c
commit 23dde903b7
9 changed files with 52 additions and 81 deletions

View File

@ -1,49 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\UpgradeDeprecation;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
abstract class AbstraceReplaceDeprecatedConstantNodeVisitor extends NodeVisitorAbstract
{
abstract public function getClassName(): string;
abstract public function getOldConstantName(): string;
abstract public function getNewConstantName(): string;
public function enterNode(Node $node): ?int
{
if ($this->isCandidate($node)) {
$this->refactor($node);
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
return null;
}
private function isCandidate(Node $node): bool
{
if ($node instanceof ClassConstFetch) {
if ((string) $node->class !== $this->getClassName()) {
return false;
}
if ((string) $node->name !== $this->getOldConstantName()) {
return false;
}
return true;
}
return false;
}
private function refactor(ClassConstFetch $classConstFetchNode): void
{
$classConstFetchNode->name->name = $this->getNewConstantName();
}
}

View File

@ -11,7 +11,7 @@ use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractRector;
/**
* Reflects @link https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject
*/
final class NetteObjectToSmartTraitRector extends AbstractRector
{

View File

@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractRector;
final class RemoveConfiguratorConstantsRector extends AbstractRector
{
public function isCandidate(Node $node): bool
{
if ($node instanceof ClassConstFetch) {
if ((string) $node->class !== 'Nette\Configurator') {
return false;
}
if (! in_array((string) $node->name, ['DEVELOPMENT', 'PRODUCTION'], true)) {
return false;
}
return true;
}
return false;
}
/**
* @param ClassConstFetch $classConstFetchNode
*/
public function refactor($classConstFetchNode): void
{
dump($classConstFetchNode->name->name);
die;
$classConstFetchNode->name->name = $this->getNewConstantName();
}
public function getSetName(): string
{
return SetNames::NETTE;
}
public function sinceVersion(): float
{
return 2.3;
}
}

View File

@ -22,7 +22,7 @@ abstract class AbstractReconstructorTestCase extends TestCase
protected function setUp(): void
{
$this->container = (new ContainerFactory)->createWithConfig(__DIR__ . '/../../../tests/config/services.yml');
$this->container = (new ContainerFactory)->create();
$this->fileReconstructor = $this->container->get(FileReconstructor::class);
}

View File

@ -1,23 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Tests\NodeVisitor\UpgradeDeprecation\ReplaceDeprecatedConstantNodeVisitor;
use Rector\NodeVisitor\UpgradeDeprecation\AbstraceReplaceDeprecatedConstantNodeVisitor;
final class ReplaceOldConstantNodeVisitor extends AbstraceReplaceDeprecatedConstantNodeVisitor
{
public function getClassName(): string
{
return 'ClassWithConstants';
}
public function getOldConstantName(): string
{
return 'OLD_CONSTANT';
}
public function getNewConstantName(): string
{
return 'NEW_CONSTANT';
}
}

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Tests\NodeVisitor\UpgradeDeprecation\ReplaceDeprecatedConstantNodeVisitor;
namespace Rector\Tests\Rector\Contrib\Nette\RemoveConfiguratorConstantsRector;
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;

View File

@ -1,6 +0,0 @@
services:
_defaults:
autowire: true
# tests
Rector\Tests\NodeVisitor\UpgradeDeprecation\ReplaceDeprecatedConstantNodeVisitor\ReplaceOldConstantNodeVisitor: ~