use Attribute::CONSTANTS

This commit is contained in:
TomasVotruba 2017-09-07 00:22:26 +02:00
parent a9354be0e1
commit b1f63bfbc4
17 changed files with 78 additions and 34 deletions

View File

@ -20,7 +20,7 @@ Anonymous classes are skipped.
You can get `class`
```php
$class = (string) $node->getAttribute('class');
$class = (string) $node->getAttribute(Attribute::CLASS_NAME);
if (Strings::endsWith($class, 'Command')) {
// we are in Command class
@ -29,9 +29,9 @@ if (Strings::endsWith($class, 'Command')) {
// to be sure it's console command
/** @var PhpParser\Node\Name\FullyQualified $fqnName */
$classNode = $node->getAttribute('class_node');
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$fqnName = $classNode->extends->getAttribute('resolvedName');
$fqnName = $classNode->extends->getAttribute(Attribute::RESOLVED_NAME);
if ($fqnName->toString() === 'Symfony\Component\Console\Command') {
// we are sure it's child of Symfony\Console Command class
@ -42,7 +42,7 @@ or `type` attribute:
```php
/** @var string $type */
$type = $node->var->getAttribute('type');
$type = $node->var->getAttribute(Attribute::TYPE);
if ($type === 'Nette\Application\UI\Form') {
// this is Nette\Application\UI\Form variable

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\TypeContext;
/**
@ -85,7 +86,7 @@ final class TypeResolver extends NodeVisitorAbstract
private function getTypeFromNewNode(New_ $newNode): string
{
/** @var FullyQualified $fqnName */
$fqnName = $newNode->class->getAttribute('resolvedName');
$fqnName = $newNode->class->getAttribute(Attribute::RESOLVED_NAME);
return $fqnName->toString();
}
@ -94,7 +95,7 @@ final class TypeResolver extends NodeVisitorAbstract
{
$variableType = null;
$parentNode = $variableNode->getAttribute('parent');
$parentNode = $variableNode->getAttribute(Attribute::PARENT_NODE);
if ($parentNode instanceof Assign) {
if ($parentNode->expr instanceof New_) {
$variableName = $variableNode->name;

View File

@ -6,6 +6,7 @@ use Nette\Utils\Html;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Contract\Parser\ParserInterface;
use Rector\Node\Attribute;
use Rector\NodeTraverser\StandaloneTraverseNodeTraverser;
use Rector\Tests\AbstractContainerAwareTestCase;
@ -42,7 +43,7 @@ final class PropertyTest extends AbstractContainerAwareTestCase
{
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $this->nodes[1]->stmts[1]->stmts[2]->stmts[0]->expr;
$this->assertSame(Html::class, $propertyFetchNode->getAttribute('type'));
$this->assertSame(Html::class, $propertyFetchNode->getAttribute(Attribute::TYPE));
}
/**
@ -50,8 +51,9 @@ final class PropertyTest extends AbstractContainerAwareTestCase
*/
public function testProperty(): void
{
/** @var Node $propertyNode */
$propertyNode = $this->nodes[1]->stmts[1]->stmts[0];
$this->assertSame(Html::class, $propertyNode->getAttribute('type'));
$this->assertSame(Html::class, $propertyNode->getAttribute(Attribute::TYPE));
}
/**
@ -59,7 +61,8 @@ final class PropertyTest extends AbstractContainerAwareTestCase
*/
public function testMethodParameter(): void
{
/** @var Node $constructorVariableNode */
$constructorVariableNode = $this->nodes[1]->stmts[1]->stmts[1]->params[0]->var;
$this->assertSame(Html::class, $constructorVariableNode->getAttribute('type'));
$this->assertSame(Html::class, $constructorVariableNode->getAttribute(Attribute::TYPE));
}
}

View File

@ -6,6 +6,7 @@ use Nette\Utils\Html;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use Rector\Contract\Parser\ParserInterface;
use Rector\Node\Attribute;
use Rector\NodeTraverser\StandaloneTraverseNodeTraverser;
use Rector\Tests\AbstractContainerAwareTestCase;
@ -42,7 +43,7 @@ final class VariableTest extends AbstractContainerAwareTestCase
{
/** @var Variable $htmlVariableNode */
$htmlVariableNode = $this->nodes[1]->stmts[1]->stmts[0]->stmts[0]->expr->var;
$this->assertSame(Html::class, $htmlVariableNode->getAttribute('type'));
$this->assertSame(Html::class, $htmlVariableNode->getAttribute(Attribute::TYPE));
}
/**
@ -52,6 +53,6 @@ final class VariableTest extends AbstractContainerAwareTestCase
{
/** @var Variable $assignedVariableNode */
$assignedVariableNode = $this->nodes[1]->stmts[1]->stmts[0]->stmts[1]->expr->var;
$this->assertSame(Html::class, $assignedVariableNode->getAttribute('type'));
$this->assertSame(Html::class, $assignedVariableNode->getAttribute(Attribute::TYPE));
}
}

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Scalar\MagicConst\Method;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Exception\NotImplementedException;
use Rector\Node\Attribute;
final class TriggerMessageResolver
{
@ -32,12 +33,12 @@ final class TriggerMessageResolver
if ($node instanceof Method) {
$classMethodNode = $this->findParentOfType($node, ClassMethod::class);
return $node->getAttribute('class') . '::' . $classMethodNode->name->name;
return $node->getAttribute(Attribute::CLASS_NAME) . '::' . $classMethodNode->name->name;
}
if ($node instanceof String_) {
$message = $node->value; // complet class to local methods
return $this->completeClassToLocalMethods($message, $node->getAttribute('class'));
return $this->completeClassToLocalMethods($message, (string) $node->getAttribute(Attribute::CLASS_NAME));
}
throw new NotImplementedException(sprintf(
@ -50,10 +51,10 @@ final class TriggerMessageResolver
private function findParentOfType(Node $node, string $type): Node
{
$parentNode = $node->getAttribute('parent');
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
while (! is_a($parentNode, $type, true)) {
$parentNode = $parentNode->getAttribute('parent');
$parentNode = $parentNode->getAttribute(Attribute::PARENT_NODE);
}
return $parentNode;

View File

@ -9,6 +9,31 @@ namespace Rector\Node;
*/
final class Attribute
{
/**
* @var string
*/
public const TYPE = 'type';
/**
* @var string
*/
public const RESOLVED_NAME = 'resolvedName';
/**
* @var string
*/
public const CLASS_NAME = 'class';
/**
* @var string
*/
public const CLASS_NODE = 'class_node';
/**
* @var string
*/
public const PARENT_NODE = 'parent';
/**
* @var string
*/
@ -17,7 +42,7 @@ final class Attribute
/**
* @var string
*/
public const TYPE = 'type';
public const NEXT_NODE = 'next';
private function __construct()
{

View File

@ -5,6 +5,7 @@ namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Node\Attribute;
final class MethodCallAnalyzer
{
@ -72,10 +73,10 @@ final class MethodCallAnalyzer
{
$varNode = $methodCallNode->var;
while ($varNode->getAttribute('type') === null) {
while ($varNode->getAttribute(Attribute::TYPE) === null) {
$varNode = $varNode->var;
}
return (string) $varNode->getAttribute('type');
return (string) $varNode->getAttribute(Attribute::TYPE);
}
}

View File

@ -4,6 +4,7 @@ namespace Rector\NodeVisitor;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
/**
* See https://github.com/nikic/PHP-Parser/blob/master/doc/5_FAQ.markdown#how-can-the-nextprevious-sibling-of-a-node-be-obtained.
@ -32,12 +33,14 @@ final class NodeConnector extends NodeVisitorAbstract
public function enterNode(Node $node): void
{
if (! empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
$node->setAttribute(Attribute::PARENT_NODE, $this->stack[count($this->stack) - 1]);
}
if ($this->prev && $this->prev->getAttribute('parent') === $node->getAttribute('parent')) {
$node->setAttribute('prev', $this->prev);
$this->prev->setAttribute('next', $node);
if ($this->prev &&
$this->prev->getAttribute(Attribute::PARENT_NODE) === $node->getAttribute(Attribute::PARENT_NODE)
) {
$node->setAttribute(Attribute::PREVIOUS_NODE, $this->prev);
$this->prev->setAttribute(Attribute::NEXT_NODE, $node);
}
$this->stack[] = $node;

View File

@ -7,6 +7,7 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\Node\Attribute;
abstract class AbstractChangeMethodNameRector extends AbstractRector
{
@ -65,7 +66,7 @@ abstract class AbstractChangeMethodNameRector extends AbstractRector
}
/** @var string $type */
$type = $node->var->getAttribute('type');
$type = $node->var->getAttribute(Attribute::TYPE);
if (! $this->isTypeRelevant($type)) {
return false;

View File

@ -5,6 +5,7 @@ namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\Node\Attribute;
abstract class AbstractChangeParentClassRector extends AbstractRector
{
@ -15,7 +16,7 @@ abstract class AbstractChangeParentClassRector extends AbstractRector
}
/** @var FullyQualified $fqnName */
$fqnName = $node->extends->getAttribute('resolvedName');
$fqnName = $node->extends->getAttribute(Attribute::RESOLVED_NAME);
return $fqnName->toString() === $this->getOldClassName();
}

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -62,7 +63,7 @@ final class FormSetRequiredRector extends AbstractRector
return false;
}
if ($arg->value->class->getAttribute('type') !== self::FORM_CLASS) {
if ($arg->value->class->getAttribute(Attribute::TYPE) !== self::FORM_CLASS) {
return false;
}

View File

@ -7,6 +7,7 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\Builder\StatementGlue;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -58,7 +59,7 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
}
/** @var FullyQualified $fqnName */
$fqnName = $node->extends->getAttribute('resolvedName');
$fqnName = $node->extends->getAttribute(Attribute::RESOLVED_NAME);
return $fqnName->toString() === self::PARENT_CLASS;
}

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
final class RemoveConfiguratorConstantsRector extends AbstractRector
@ -50,7 +51,7 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
{
/** @var Node\Name\FullyQualified $fqnName */
$fqnName = $classConstFetchNode->class->getAttribute('resolvedName');
$fqnName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
return $fqnName->toString();
}

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -55,7 +56,7 @@ final class ConstraintUrlOptionRector extends AbstractRector
return false;
}
$prevNode = $node->getAttribute('prev');
$prevNode = $node->getAttribute(Attribute::PREVIOUS_NODE);
if (! $prevNode instanceof String_) {
return false;
}

View File

@ -6,6 +6,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\MethodCall;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -44,7 +45,7 @@ final class FormIsValidRector extends AbstractRector
return false;
}
if ($node->var->getAttribute('type') !== 'Symfony\Component\Form\Form') {
if ($node->var->getAttribute(Attribute::TYPE) !== 'Symfony\Component\Form\Form') {
return false;
}
@ -52,7 +53,7 @@ final class FormIsValidRector extends AbstractRector
return false;
}
if ($node->getAttribute('prev') !== null) {
if ($node->getAttribute(Attribute::PREVIOUS_NODE) !== null) {
return false;
}

View File

@ -10,6 +10,7 @@ use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
use Rector\Builder\Naming\NameResolver;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -88,7 +89,7 @@ final class CommandToConstructorInjectionRector extends AbstractRector
public function isCandidate(Node $node): bool
{
$class = (string) $node->getAttribute('class');
$class = (string) $node->getAttribute(Attribute::CLASS_NAME);
if (! Strings::endsWith($class, 'Command')) {
return false;
@ -120,7 +121,7 @@ final class CommandToConstructorInjectionRector extends AbstractRector
$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
$this->classPropertyCollector->addPropertyForClass(
(string) $node->getAttribute('class'),
(string) $node->getAttribute(Attribute::CLASS_NAME),
$serviceType,
$propertyName
);
@ -130,7 +131,7 @@ final class CommandToConstructorInjectionRector extends AbstractRector
private function replaceParentContainerAwareCommandWithCommand(Node $node): void
{
$classNode = $node->getAttribute('class_node');
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode->extends = new FullyQualified('Symfony\Component\Console\Command\Command');
}
}

View File

@ -8,6 +8,7 @@ use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
use Rector\Builder\Naming\NameResolver;
use Rector\Deprecation\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
@ -87,7 +88,7 @@ final class GetterToPropertyRector extends AbstractRector
$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
$this->classPropertyCollector->addPropertyForClass(
(string) $methodCallNode->getAttribute('class'),
(string) $methodCallNode->getAttribute(Attribute::CLASS_NAME),
$serviceType,
$propertyName
);