extract AbstractClassAwareRector

This commit is contained in:
TomasVotruba 2017-09-06 17:01:12 +02:00
parent f869b168b9
commit e67a11b587
9 changed files with 66 additions and 58 deletions

View File

@ -9,7 +9,7 @@ use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\NodeVisitorAbstract;
use Rector\TriggerExtractor\Deprecation\DeprecationCollector;
use Rector\TriggerExtractor\TriggerError\TriggerMessageResolver;
use Rector\TriggerExtractor\TriggerMessageResolver;
final class DeprecationDetector extends NodeVisitorAbstract // @todo use : class aware node visitor
{

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\TriggerExtractor\TriggerError;
namespace Rector\TriggerExtractor;
use Exception;
use PhpParser\Node;

View File

@ -33,9 +33,9 @@ final class TriggerExtractorTest extends AbstractContainerAwareTestCase
$setClassToSetFacoryDeprecation = $deprecations[0];
// @todo: complete class in here!
$this->assertSame(
'setClass() second parameter $args is deprecated, use setFactory()',
'Nette\DI\Definition::setClass() second parameter $args is deprecated,'
. ' use Nette\DI\Definition::setFactory()',
$setClassToSetFacoryDeprecation
);
}

View File

@ -6,7 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
abstract class AbstractChangeParentClassRector extends AbstractRector
abstract class AbstractChangeParentClassRector extends AbstractClassAwareRector
{
public function isCandidate(Node $node): bool
{

View File

@ -0,0 +1,55 @@
<?php declare(strict_types=1);
namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
abstract class AbstractClassAwareRector extends AbstractRector
{
/**
* @var Class_|null
*/
protected $classNode;
/**
* @param Node[] $nodes
* @return null|Node[]
*/
public function beforeTraverse(array $nodes): ?array
{
$this->classNode = null;
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->classNode = $node;
break;
}
}
return null;
}
protected function getClassName(): string
{
if ($this->classNode === null) {
return '';
}
return $this->classNode->namespacedName->toString();
}
protected function getParentClassName(): string
{
if ($this->classNode === null) {
return '';
}
$parentClass = $this->classNode->extends;
/** @var Node\Name\FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');
return $fqnParentClassName->toString();
}
}

View File

@ -3,7 +3,6 @@
namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Deprecation\DeprecationInterface;
@ -11,29 +10,6 @@ use Rector\Contract\Rector\RectorInterface;
abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
{
/**
* @var Class_|null
*/
protected $classNode;
/**
* @param Node[] $nodes
* @return null|Node[]
*/
public function beforeTraverse(array $nodes): ?array
{
$this->classNode = null;
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->classNode = $node;
break;
}
}
return null;
}
/**
* @return null|int|Node
*/
@ -49,27 +25,4 @@ abstract class AbstractRector extends NodeVisitorAbstract implements Deprecation
return null;
}
protected function getClassName(): string
{
if ($this->classNode === null) {
return '';
}
return $this->classNode->namespacedName->toString();
}
protected function getParentClassName(): string
{
if ($this->classNode === null) {
return '';
}
$parentClass = $this->classNode->extends;
/** @var Node\Name\FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');
return $fqnParentClassName->toString();
}
}

View File

@ -7,12 +7,12 @@ use PhpParser\Node\Stmt\Class_;
use Rector\Builder\StatementGlue;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractClassAwareRector;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
*/
final class NetteObjectToSmartTraitRector extends AbstractRector
final class NetteObjectToSmartTraitRector extends AbstractClassAwareRector
{
/**
* @var string

View File

@ -12,7 +12,7 @@ use Rector\Builder\Naming\NameResolver;
use Rector\Deprecation\SetNames;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractClassAwareRector;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
/**
@ -35,7 +35,7 @@ use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\Local
*
* $this->someService
*/
final class CommandToConstructorInjectionRector extends AbstractRector
final class CommandToConstructorInjectionRector extends AbstractClassAwareRector
{
/**
* @var ServiceFromKernelResolver

View File

@ -10,7 +10,7 @@ use Rector\Builder\Naming\NameResolver;
use Rector\Deprecation\SetNames;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractClassAwareRector;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
/**
@ -20,7 +20,7 @@ use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\Local
* into:
* $this->someService # where "someService" is type of the service
*/
final class GetterToPropertyRector extends AbstractRector
final class GetterToPropertyRector extends AbstractClassAwareRector
{
/**
* @var NameResolver