mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
extract AbstractClassAwareRector
This commit is contained in:
parent
f869b168b9
commit
e67a11b587
|
@ -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
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\TriggerExtractor\TriggerError;
|
||||
namespace Rector\TriggerExtractor;
|
||||
|
||||
use Exception;
|
||||
use PhpParser\Node;
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
55
src/Rector/AbstractClassAwareRector.php
Normal file
55
src/Rector/AbstractClassAwareRector.php
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user