[UpgradeDeprecation] add ReplaceDeprecatedConstantNodeVisitor

This commit is contained in:
TomasVotruba 2017-08-06 18:51:23 +02:00
parent 0ade452fde
commit 2beb152e5f
8 changed files with 115 additions and 7 deletions

View File

@ -1,5 +1,6 @@
includes:
- vendor/symplify/easy-coding-standard/config/psr2-checkers.neon
- vendor/symplify/easy-coding-standard/config/php70-checkers.neon
- vendor/symplify/easy-coding-standard/config/php71-checkers.neon
checkers:

View File

@ -5,7 +5,7 @@ namespace Rector\Application;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use Rector\Printer\CodeStyledPrinter;
use Rector\Printer\FormatPerservingPrinter;
use SplFileInfo;
final class FileProcessor
@ -16,7 +16,7 @@ final class FileProcessor
private $parser;
/**
* @var CodeStyledPrinter
* @var FormatPerservingPrinter
*/
private $codeStyledPrinter;
@ -30,7 +30,7 @@ final class FileProcessor
*/
private $lexer;
public function __construct(Parser $parser, CodeStyledPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser)
public function __construct(Parser $parser, FormatPerservingPrinter $codeStyledPrinter, Lexer $lexer, NodeTraverser $nodeTraverser)
{
$this->parser = $parser;
$this->codeStyledPrinter = $codeStyledPrinter;

View File

@ -0,0 +1,73 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\UpgradeDeprecation;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
final class ReplaceDeprecatedConstantNodeVisitor extends NodeVisitorAbstract
{
// this will be in specific node visitor, now hardcoded
public function getClassName(): string
{
return'ClassWithConstants';
}
public function getOldConstantName(): string
{
return 'OLD_CONSTANT';
}
public function getNewConstantName(): string
{
return 'NEW_CONSTANT';
}
/**
* Return value semantics:
* * null
* => $node stays as-is
* * NodeTraverser::DONT_TRAVERSE_CHILDREN
* => Children of $node are not traversed. $node stays as-is
* * NodeTraverser::STOP_TRAVERSAL
* => Traversal is aborted. $node stays as-is
* * otherwise
* => $node is set to the return value.
*
* @return null|int|Node
*/
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

@ -5,7 +5,7 @@ namespace Rector\Printer;
use PhpParser\PrettyPrinter\Standard;
use SplFileInfo;
final class CodeStyledPrinter
final class FormatPerservingPrinter
{
/**
* @var Standard

View File

@ -6,7 +6,7 @@ use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use Rector\NodeTraverser\StateHolder;
use Rector\Printer\CodeStyledPrinter;
use Rector\Printer\FormatPerservingPrinter;
use SplFileInfo;
final class FileReconstructor
@ -17,7 +17,7 @@ final class FileReconstructor
private $parser;
/**
* @var CodeStyledPrinter
* @var FormatPerservingPrinter
*/
private $codeStyledPrinter;
@ -38,7 +38,7 @@ final class FileReconstructor
public function __construct(
Parser $parser,
CodeStyledPrinter $codeStyledPrinter,
FormatPerservingPrinter $codeStyledPrinter,
Lexer $lexer,
NodeTraverser $nodeTraverser,
StateHolder $stateHolder

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
namespace Rector\Tests\NodeVisitor\ReplaceDeprecatedConstantNodeVisitor\InjectAnnotationToConstructorReconstructor;
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;
final class Test extends AbstractReconstructorTestCase
{
public function test(): void
{
$this->doTestFileMatchesExpectedContent(
__DIR__ . '/wrong/wrong.php.inc',
__DIR__ . '/correct/correct.php.inc'
);
}
}

View File

@ -0,0 +1,9 @@
<?php declare (strict_types=1);
class ClassWithExternalConstant
{
public function getValue()
{
return ClassWithConstants::NEW_CONSTANT;
}
}

View File

@ -0,0 +1,9 @@
<?php declare (strict_types=1);
class ClassWithExternalConstant
{
public function getValue()
{
return ClassWithConstants::OLD_CONSTANT;
}
}