mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
refactor NodeVisitor to Rectors
This commit is contained in:
parent
bcc02f678c
commit
23dde903b7
@ -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();
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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';
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -1,6 +0,0 @@
|
||||
services:
|
||||
_defaults:
|
||||
autowire: true
|
||||
|
||||
# tests
|
||||
Rector\Tests\NodeVisitor\UpgradeDeprecation\ReplaceDeprecatedConstantNodeVisitor\ReplaceOldConstantNodeVisitor: ~
|
Loading…
x
Reference in New Issue
Block a user