decouple metadata attributes to MetadataAttributes for better NodeTypeResolver decoupling

This commit is contained in:
Tomas Votruba 2018-08-10 13:30:41 +02:00
parent 0acd7d3a75
commit b38c7b5272
33 changed files with 161 additions and 172 deletions

View File

@ -5,7 +5,7 @@ namespace Rector\BetterPhpDocParser\NodeAnalyzer;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Use_;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Php\TypeAnalyzer;
final class NamespaceAnalyzer
@ -22,7 +22,10 @@ final class NamespaceAnalyzer
public function resolveTypeToFullyQualified(string $type, Node $node): string
{
$useStatementMatch = $this->matchUseStatements($type, (array) $node->getAttribute(Attribute::USE_NODES));
$useStatementMatch = $this->matchUseStatements(
$type,
(array) $node->getAttribute(MetadataAttribute::USE_NODES)
);
if ($useStatementMatch) {
return $useStatementMatch;
}
@ -36,7 +39,7 @@ final class NamespaceAnalyzer
return ltrim($type, '\\');
}
$namespace = $node->getAttribute(Attribute::NAMESPACE_NAME);
$namespace = $node->getAttribute(MetadataAttribute::NAMESPACE_NAME);
return ($namespace ? $namespace . '\\' : '') . $type;
}

View File

@ -53,16 +53,16 @@ These attributes are always available anywhere inside the Node tree. That means
```php
<?php declare(strict_types=1);
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
// string name of current namespace
$namespaceName = $node->setAttribute(Attribute::NAMESPACE_NAME, $this->namespaceName);
$namespaceName = $node->setAttribute(MetadataAttribute::NAMESPACE_NAME, $this->namespaceName);
// instance of "PhpParser\Node\Stmt\Namespace_"
$namespaceNode = $node->setAttribute(Attribute::NAMESPACE_NODE, $this->namespaceNode);
$namespaceNode = $node->setAttribute(MetadataAttribute::NAMESPACE_NODE, $this->namespaceNode);
// instances of "PhpParser\Node\Stmt\Use_"
$useNodes = $node->setAttribute(Attribute::USE_NODES, $this->useNodes);
$useNodes = $node->setAttribute(MetadataAttribute::USE_NODES, $this->useNodes);
```
#### Classes
@ -70,16 +70,16 @@ $useNodes = $node->setAttribute(Attribute::USE_NODES, $this->useNodes);
```php
<?php declare(strict_types=1);
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
// string name of current class
$className = $node->getAttribute(Attribute::CLASS_NAME);
$className = $node->getAttribute(MetadataAttribute::CLASS_NAME);
// instance of "PhpParser\Node\Stmt\Class_"
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode = $node->getAttribute(MetadataAttribute::CLASS_NODE);
// string name of current class
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
```
#### Methods
@ -87,19 +87,18 @@ $parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
```php
<?php declare(strict_types=1);
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
// string name of current method
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
$methodName = $node->getAttribute(MetadataAttribute::METHOD_NAME);
// instance of "PhpParser\Node\Stmt\ClassMethod"
$methodNode = $node->getAttribute(Attribute::METHOD_NODE);
$methodNode = $node->getAttribute(MetadataAttribute::METHOD_NODE);
// string name of current method call ($this->get => "get")
$methodCallName = $node->getAttribute(Attribute::METHOD_NAME);
$methodCallName = $node->getAttribute(MetadataAttribute::METHOD_NAME);
```
#### Setup
1. Import `services.yml` in your config
@ -117,7 +116,9 @@ imports:
services:
YourApp\NodeTraverser:
calls:
// @todo depends on NameResolver from PhpParser
// @todo depends on NameResolver from PhpParser - or resolve instead of it?
// @todo maybe add MetadataNodeVisitor and decouple these 2 into services, that would fix
// the previous issue
- ['addNodeVisitor', ['@Rector\NodeTypeResolver\NodeVisitor\ClassAndMethodResolver']]
- ['addNodeVisitor', ['@Rector\NodeTypeResolver\NodeVisitor\NamespaceResolver']]
```
@ -172,12 +173,17 @@ final class NodeTraverserFactory
```php
<?php declare(strict_types=1);
use Rector\NodeTypeResolver\Node\MetadataAttribute;
/** @var PhpParser\NodeTraverser $nodeTraverser */
$nodeTraverser = ...; // from DI container or manually created
$nodes = $this->parser->parseFile(...);
$nodes = $nodeTraverser->traverse($nodes);
/** @var \PhpParser\Node $node */
/** @var PhpParser\Node $node */
foreach ($nodes as $node) {
$className = $node->getAttribute(Attributes::CLASS_NAME);
$className = $node->getAttribute(MetadataAttribute::CLASS_NAME);
var_dump($className);
}
```

View File

@ -0,0 +1,51 @@
<?php declare(strict_types=1);
namespace Rector\NodeTypeResolver\Node;
final class MetadataAttribute
{
/**
* @var string
*/
public const NAMESPACE_NAME = 'namespace';
/**
* @var string
*/
public const NAMESPACE_NODE = 'namespaceNode';
/**
* @var string
*/
public const USE_NODES = 'useNodes';
/**
* @var string
*/
public const CLASS_NAME = 'className';
/**
* @var string
*/
public const CLASS_NODE = 'classNode';
/**
* @var string
*/
public const PARENT_CLASS_NAME = 'parentClassName';
/**
* @var string
*/
public const METHOD_NAME = 'methodName';
/**
* @var string
*/
public const METHOD_NODE = 'methodNode';
/**
* @var string
*/
public const METHOD_CALL_NAME = 'methodCallName';
}

View File

@ -12,19 +12,11 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
/**
* @todo should be checked by CS for the "NodeVisitor" suffix
* it's confusing otherwise
*
* Adds attributes to all nested nodes:
*
* - @see Attribute::CLASS_NAME with current class name
* - @see Attribute::CLASS_NODE with current class node
* - @see Attribute::PARENT_CLASS_NAME with current class node
* - @see Attribute::METHOD_NAME with current method name
* - @see Attribute::METHOD_NODE with current method node
* - @see Attribute::METHOD_CALL_NAME with current method call
*/
final class ClassAndMethodResolver extends NodeVisitorAbstract
{
@ -89,8 +81,8 @@ final class ClassAndMethodResolver extends NodeVisitorAbstract
$this->className = $node->namespacedName->toString();
}
$node->setAttribute(Attribute::CLASS_NODE, $this->classNode);
$node->setAttribute(Attribute::CLASS_NAME, $this->className);
$node->setAttribute(MetadataAttribute::CLASS_NODE, $this->classNode);
$node->setAttribute(MetadataAttribute::CLASS_NAME, $this->className);
if ($this->classNode instanceof Class_) {
$this->setParentClassName($this->classNode, $node);
@ -104,12 +96,11 @@ final class ClassAndMethodResolver extends NodeVisitorAbstract
}
$parentClassResolvedName = $classNode->extends->getAttribute(Attribute::RESOLVED_NAME);
if ($parentClassResolvedName instanceof FullyQualified) {
$parentClassResolvedName = $parentClassResolvedName->toString();
}
$node->setAttribute(Attribute::PARENT_CLASS_NAME, $parentClassResolvedName);
$node->setAttribute(MetadataAttribute::PARENT_CLASS_NAME, $parentClassResolvedName);
}
private function processMethod(Node $node): void
@ -123,8 +114,8 @@ final class ClassAndMethodResolver extends NodeVisitorAbstract
$this->methodCallName = $node->name->toString();
}
$node->setAttribute(Attribute::METHOD_NAME, $this->methodName);
$node->setAttribute(Attribute::METHOD_NODE, $this->methodNode);
$node->setAttribute(Attribute::METHOD_CALL_NAME, $this->methodCallName);
$node->setAttribute(MetadataAttribute::METHOD_NAME, $this->methodName);
$node->setAttribute(MetadataAttribute::METHOD_NODE, $this->methodNode);
$node->setAttribute(MetadataAttribute::METHOD_CALL_NAME, $this->methodCallName);
}
}

View File

@ -6,8 +6,8 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
use Rector\NodeTraverserQueue\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
final class NamespaceResolver extends NodeVisitorAbstract
{
@ -54,8 +54,8 @@ final class NamespaceResolver extends NodeVisitorAbstract
$this->useNodes = $this->betterNodeFinder->findInstanceOf($node, Use_::class);
}
$node->setAttribute(Attribute::NAMESPACE_NAME, $this->namespaceName);
$node->setAttribute(Attribute::NAMESPACE_NODE, $this->namespaceNode);
$node->setAttribute(Attribute::USE_NODES, $this->useNodes);
$node->setAttribute(MetadataAttribute::NAMESPACE_NAME, $this->namespaceName);
$node->setAttribute(MetadataAttribute::NAMESPACE_NODE, $this->namespaceNode);
$node->setAttribute(MetadataAttribute::USE_NODES, $this->useNodes);
}
}

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Name\FullyQualified;
use PHPStan\Broker\Broker;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Contract\PerNodeTypeResolver\PerNodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\Reflection\ClassReflectionTypesResolver;
final class NameTypeResolver implements PerNodeTypeResolverInterface
@ -43,7 +44,7 @@ final class NameTypeResolver implements PerNodeTypeResolverInterface
public function resolve(Node $nameNode): array
{
if ($nameNode->toString() === 'parent') {
return [$nameNode->getAttribute(Attribute::PARENT_CLASS_NAME)];
return [$nameNode->getAttribute(MetadataAttribute::PARENT_CLASS_NAME)];
}
$fullyQualifiedName = $this->resolveFullyQualifiedName($nameNode, $nameNode->toString());
@ -56,7 +57,7 @@ final class NameTypeResolver implements PerNodeTypeResolverInterface
private function resolveFullyQualifiedName(Node $nameNode, string $name): string
{
if (in_array($name, ['self', 'static', 'this'], true)) {
return $nameNode->getAttribute(Attribute::CLASS_NAME);
return $nameNode->getAttribute(MetadataAttribute::CLASS_NAME);
}
/** @var Name|null $name */

View File

@ -5,8 +5,8 @@ namespace Rector\PhpParser\Rector;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Stmt\Return_;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -57,7 +57,7 @@ CODE_SAMPLE
return false;
}
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
$methodName = $node->getAttribute(MetadataAttribute::METHOD_NAME);
if ($methodName !== 'leaveNode') {
return false;
}

View File

@ -5,14 +5,14 @@ namespace Rector\Sensio\Helper;
use Nette\Utils\Strings;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Exception\ShouldNotHappenException;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
final class TemplateGuesser
{
public function resolveFromClassMethodNode(ClassMethod $classMethodNode, int $version = 5): string
{
$namespace = (string) $classMethodNode->getAttribute(Attribute::NAMESPACE_NAME);
$class = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME);
$namespace = (string) $classMethodNode->getAttribute(MetadataAttribute::NAMESPACE_NAME);
$class = (string) $classMethodNode->getAttribute(MetadataAttribute::CLASS_NAME);
$method = (string) $classMethodNode->name;
if ($version === 3) {

View File

@ -6,7 +6,7 @@ use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
final class ControllerMethodAnalyzer
{
@ -19,7 +19,7 @@ final class ControllerMethodAnalyzer
return false;
}
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if (! Strings::endsWith($parentClassName, 'Controller')) {
return false;

View File

@ -4,9 +4,9 @@ namespace Rector\Symfony\Rector\Controller;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\ChainMethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -67,7 +67,7 @@ CODE_SAMPLE
public function isCandidate(Node $node): bool
{
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if ($parentClassName !== $this->controllerClass) {
return false;
}

View File

@ -4,10 +4,10 @@ namespace Rector\Symfony\Rector\Controller;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -58,7 +58,7 @@ final class RedirectToRouteRector extends AbstractRector
public function isCandidate(Node $node): bool
{
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if ($parentClassName !== $this->controllerClass) {
return false;
}

View File

@ -4,8 +4,8 @@ namespace Rector\Symfony\Rector\Form;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -91,12 +91,12 @@ final class FormTypeGetParentRector extends AbstractRector
private function isParentTypeAndMethod(Node $node, string $type, string $method): bool
{
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if ($parentClassName !== $type) {
return false;
}
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
$methodName = $node->getAttribute(MetadataAttribute::METHOD_NAME);
return $methodName === $method;
}

View File

@ -4,8 +4,8 @@ namespace Rector\Symfony\Rector\Form;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -58,7 +58,7 @@ final class StringFormTypeToClassRector extends AbstractRector
return false;
}
return (string) $node->getAttribute(Attribute::METHOD_CALL_NAME) === 'add';
return (string) $node->getAttribute(MetadataAttribute::METHOD_CALL_NAME) === 'add';
}
/**

View File

@ -13,6 +13,7 @@ use Rector\Naming\PropertyNaming;
use Rector\Node\Attribute;
use Rector\Node\PropertyFetchNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
abstract class AbstractToConstructorInjectionRector extends AbstractRector
@ -73,7 +74,7 @@ abstract class AbstractToConstructorInjectionRector extends AbstractRector
$propertyName = $this->propertyNaming->fqnToVariableName($serviceType);
$this->classPropertyCollector->addPropertyForClass(
(string) $methodCallNode->getAttribute(Attribute::CLASS_NAME),
(string) $methodCallNode->getAttribute(MetadataAttribute::CLASS_NAME),
[$serviceType],
$propertyName
);

View File

@ -3,7 +3,7 @@
namespace Rector\Symfony\Rector\FrameworkBundle;
use PhpParser\Node;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -77,7 +77,7 @@ CODE_SAMPLE
return false;
}
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
return in_array($parentClassName, $this->containerAwareParentTypes, true);
}

View File

@ -7,9 +7,9 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Naming\PropertyNaming;
use Rector\Node\Attribute;
use Rector\Node\PropertyFetchNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -112,7 +112,7 @@ CODE_SAMPLE
$propertyName = $this->propertyNaming->underscoreToName($parameterName);
$this->classPropertyCollector->addPropertyForClass(
(string) $methodCallNode->getAttribute(Attribute::CLASS_NAME),
(string) $methodCallNode->getAttribute(MetadataAttribute::CLASS_NAME),
['string'], // @todo: resolve type from container provider? see parameter autowire compiler pass
$propertyName
);

View File

@ -5,10 +5,10 @@ namespace Rector\Symfony\Rector\HttpKernel;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTraverserQueue\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -122,7 +122,7 @@ CODE_SAMPLE
return false;
}
$methodNode = $node->getAttribute(Attribute::METHOD_NODE);
$methodNode = $node->getAttribute(MetadataAttribute::METHOD_NODE);
return $this->controllerMethodAnalyzer->isAction($methodNode);
}

View File

@ -6,8 +6,6 @@ namespace Rector\Node;
* List of attributes by constants, to prevent any typos.
*
* Because typo can causing return "null" instaed of real value - impossible to spot.
*
* @todo decouple so it's usable in NodeTypeResolver package
*/
final class Attribute
{
@ -27,26 +25,6 @@ final class Attribute
*/
public const RESOLVED_NAME = 'resolvedName';
/**
* @var string
*/
public const CLASS_NAME = 'className';
/**
* @var string
*/
public const METHOD_NAME = 'methodName';
/**
* @var string
*/
public const PARENT_CLASS_NAME = 'parentClassName';
/**
* @var string
*/
public const CLASS_NODE = 'classNode';
/**
* @var string
*/
@ -62,54 +40,11 @@ final class Attribute
*/
public const NEXT_NODE = 'nextNode';
/**
* @var string
*/
public const USE_STATEMENTS = 'useStatements';
/**
* @var string
*/
public const NAMESPACE_NAME = 'namespace';
/**
* @var string
*/
public const METHOD_CALL_NAME = 'methodCallName';
/**
* @var string
*/
public const METHOD_NODE = 'methodNode';
/**
* @var string
*/
public const NAMESPACE_NODE = 'namespaceNode';
/**
* @var string
*/
public const USE_NODES = 'useNodes';
/**
* @var string
*/
public const RETURN_TYPES = 'returnTypes';
/**
* @var string
*/
public const COMMENTS = 'comments';
/**
* Internal php-parser name.
* Do not change this even if you want!
*
* @var string
*/
public const NAMESPACED_NAME = 'namespacedName';
/**
* PHPStan-based type scope.
*

View File

@ -5,7 +5,7 @@ namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
/**
@ -45,7 +45,7 @@ final class ClassMethodAnalyzer
return false;
}
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode = $node->getAttribute(MetadataAttribute::CLASS_NODE);
$nodeTypes = $this->nodeTypeResolver->resolve($classNode);
return in_array($type, $nodeTypes, true);

View File

@ -4,7 +4,7 @@ namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
/**
* Read-only utils for MethodCall Node:
@ -21,7 +21,7 @@ final class MethodNameAnalyzer
return false;
}
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if (! $parentClassName) {
return false;
}

View File

@ -8,7 +8,7 @@ use PhpParser\NodeVisitorAbstract;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\PropertyBuilder;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
final class PropertyAddingNodeVisitor extends NodeVisitorAbstract
{
@ -48,7 +48,7 @@ final class PropertyAddingNodeVisitor extends NodeVisitorAbstract
private function processClassNode(Class_ $classNode): Class_
{
$className = (string) $classNode->getAttribute(Attribute::CLASS_NAME);
$className = (string) $classNode->getAttribute(MetadataAttribute::CLASS_NAME);
$propertiesForClass = $this->classPropertyCollector->getPropertiesForClass($className);
if (! count($propertiesForClass)) {

View File

@ -5,7 +5,7 @@ namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Exception\ShouldNotHappenException;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
abstract class AbstractPHPUnitRector extends AbstractRector
@ -28,12 +28,12 @@ abstract class AbstractPHPUnitRector extends AbstractRector
protected function isInTestClass(Node $node): bool
{
/** @var Class_|null $classNode */
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode = $node->getAttribute(MetadataAttribute::CLASS_NODE);
if ($classNode === null) {
throw new ShouldNotHappenException(sprintf(
'"%s" should be set in "%s"',
Attribute::CLASS_NODE,
MetadataAttribute::CLASS_NODE,
__METHOD__
));
}

View File

@ -9,7 +9,7 @@ use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
use Rector\BetterPhpDocParser\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
@ -129,7 +129,7 @@ CODE_SAMPLE
$propertyName = $varLikeIdentifierNode->name;
$this->classPropertyCollector->addPropertyForClass(
(string) $propertyNode->getAttribute(Attribute::CLASS_NAME),
(string) $propertyNode->getAttribute(MetadataAttribute::CLASS_NAME),
$propertyTypes,
$propertyName
);

View File

@ -8,8 +8,8 @@ use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Builder\Class_\VariableInfo;
use Rector\Configuration\Rector\Architecture\DependencyInjection\VariablesToPropertyFetchCollection;
use Rector\Node\Attribute;
use Rector\Node\PropertyFetchNodeFactory;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
@ -134,12 +134,12 @@ CODE_SAMPLE
private function isInControllerActionMethod(Node $node): bool
{
if (! Strings::endsWith((string) $node->getAttribute(Attribute::CLASS_NAME), 'Controller')) {
if (! Strings::endsWith((string) $node->getAttribute(MetadataAttribute::CLASS_NAME), 'Controller')) {
return false;
}
/** @var ClassMethod|null $methodNode */
$methodNode = $node->getAttribute(Attribute::METHOD_NODE);
$methodNode = $node->getAttribute(MetadataAttribute::METHOD_NODE);
if ($methodNode === null) {
return false;
}

View File

@ -13,8 +13,8 @@ use Rector\Builder\Class_\VariableInfoFactory;
use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\PropertyBuilder;
use Rector\Exception\Bridge\RectorProviderException;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -132,12 +132,12 @@ CODE_SAMPLE
return false;
}
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if ($parentClassName !== $this->entityRepositoryClass) {
return false;
}
$className = $node->getAttribute(Attribute::CLASS_NAME);
$className = $node->getAttribute(MetadataAttribute::CLASS_NAME);
return Strings::endsWith($className, 'Repository');
}
@ -173,7 +173,7 @@ CODE_SAMPLE
*/
private function createRepositoryAssign(Class_ $classNode): Expression
{
$repositoryClassName = (string) $classNode->getAttribute(Attribute::CLASS_NAME);
$repositoryClassName = (string) $classNode->getAttribute(MetadataAttribute::CLASS_NAME);
$entityClassName = $this->doctrineEntityAndRepositoryMapper->mapRepositoryToEntity($repositoryClassName);
if ($entityClassName === null) {

View File

@ -16,6 +16,7 @@ use Rector\Naming\PropertyNaming;
use Rector\Node\Attribute;
use Rector\Node\PropertyFetchNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -109,8 +110,7 @@ CODE_SAMPLE
return false;
}
$className = $node->getAttribute(Attribute::CLASS_NAME);
$className = $node->getAttribute(MetadataAttribute::CLASS_NAME);
if ($className === null) {
return false;
}
@ -143,7 +143,7 @@ CODE_SAMPLE
$repositoryFqn = $this->repositoryFqn($node);
$this->classPropertyCollector->addPropertyForClass(
(string) $node->getAttribute(Attribute::CLASS_NAME),
(string) $node->getAttribute(MetadataAttribute::CLASS_NAME),
[$repositoryFqn],
$this->propertyNaming->fqnToVariableName($repositoryFqn)
);

View File

@ -6,9 +6,9 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Return_;
use Rector\Node\Attribute;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -110,8 +110,8 @@ CODE_SAMPLE
if ($node instanceof Return_) {
$this->removeNode = true;
$className = $node->getAttribute(Attribute::CLASS_NAME);
$methodName = $node->getAttribute(Attribute::METHOD_NAME);
$className = $node->getAttribute(MetadataAttribute::CLASS_NAME);
$methodName = $node->getAttribute(MetadataAttribute::METHOD_NAME);
$this->relatedTypesAndMethods[$className][] = $methodName;

View File

@ -12,6 +12,7 @@ use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeAnalyzer\MethodNameAnalyzer;
use Rector\NodeAnalyzer\StaticMethodCallAnalyzer;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -218,7 +219,7 @@ CODE_SAMPLE
}
/** @var Identifier $node */
$parentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$parentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
/** @var Identifier $node */
if (! isset($this->perClassOldToNewMethods[$parentClassName][$node->name])) {

View File

@ -11,7 +11,7 @@ use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
@ -102,7 +102,7 @@ CODE_SAMPLE
}
/** @var ClassLike $classNode */
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode = $node->getAttribute(MetadataAttribute::CLASS_NODE);
$classNodeTypes = $this->nodeTypeResolver->resolve($classNode);
return $this->isTypeMatch($classNodeTypes);
@ -114,7 +114,7 @@ CODE_SAMPLE
public function refactor(Node $classMethodNode): ?Node
{
/** @var Class_ $classMethodNode */
$classNode = $classMethodNode->getAttribute(Attribute::CLASS_NODE);
$classNode = $classMethodNode->getAttribute(MetadataAttribute::CLASS_NODE);
$classNodeTypes = $this->nodeTypeResolver->resolve($classNode);
$matchingTypes = $this->getMatchingTypesForClassNode($classNodeTypes);

View File

@ -9,7 +9,7 @@ use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Php\TypeAnalyzer;
use Rector\Rector\AbstractRector;
@ -87,7 +87,7 @@ CODE_SAMPLE
}
/** @var ClassLike $classNode */
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
$classNode = $node->getAttribute(MetadataAttribute::CLASS_NODE);
$classNodeTypes = $this->nodeTypeResolver->resolve($classNode);
if (! $classNodeTypes) {
return false;
@ -102,7 +102,7 @@ CODE_SAMPLE
public function refactor(Node $classMethodNode): ?Node
{
/** @var Class_ $classMethodNode */
$classNode = $classMethodNode->getAttribute(Attribute::CLASS_NODE);
$classNode = $classMethodNode->getAttribute(MetadataAttribute::CLASS_NODE);
$classNodeTypes = $this->nodeTypeResolver->resolve($classNode);
$matchingTypes = $this->getMatchingTypesForClassNode($classNodeTypes);

View File

@ -4,8 +4,8 @@ namespace Rector\Rector\Visibility;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use Rector\Node\Attribute;
use Rector\NodeModifier\VisibilityModifier;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -79,11 +79,11 @@ CODE_SAMPLE
}
// doesn't have a parent class
if (! $node->hasAttribute(Attribute::PARENT_CLASS_NAME)) {
if (! $node->hasAttribute(MetadataAttribute::PARENT_CLASS_NAME)) {
return false;
}
$nodeParentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if (! isset($this->constantToVisibilityByClass[$nodeParentClassName])) {
return false;
}
@ -108,7 +108,7 @@ CODE_SAMPLE
private function resolveNewVisibilityForNode(ClassConst $classConstantNode): string
{
$nodeParentClassName = $classConstantNode->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $classConstantNode->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
$constantName = $classConstantNode->consts[0]->name->toString();
return $this->constantToVisibilityByClass[$nodeParentClassName][$constantName];

View File

@ -4,8 +4,8 @@ namespace Rector\Rector\Visibility;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Node\Attribute;
use Rector\NodeModifier\VisibilityModifier;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -86,11 +86,11 @@ CODE_SAMPLE
}
// doesn't have a parent class
if (! $node->hasAttribute(Attribute::PARENT_CLASS_NAME)) {
if (! $node->hasAttribute(MetadataAttribute::PARENT_CLASS_NAME)) {
return false;
}
$nodeParentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if (! isset($this->methodToVisibilityByClass[$nodeParentClassName])) {
return false;
}
@ -117,7 +117,7 @@ CODE_SAMPLE
private function resolveNewVisibilityForNode(ClassMethod $classMethodNode): string
{
$methodName = $classMethodNode->name->toString();
$nodeParentClassName = $classMethodNode->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $classMethodNode->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
return $this->methodToVisibilityByClass[$nodeParentClassName][$methodName];
}

View File

@ -4,8 +4,8 @@ namespace Rector\Rector\Visibility;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use Rector\Node\Attribute;
use Rector\NodeModifier\VisibilityModifier;
use Rector\NodeTypeResolver\Node\MetadataAttribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -78,12 +78,12 @@ CODE_SAMPLE
}
// doesn't have a parent class
if (! $node->hasAttribute(Attribute::PARENT_CLASS_NAME)) {
if (! $node->hasAttribute(MetadataAttribute::PARENT_CLASS_NAME)) {
return false;
}
// @todo or better types?
$nodeParentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $node->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
if (! isset($this->propertyToVisibilityByClass[$nodeParentClassName])) {
return false;
}
@ -109,7 +109,7 @@ CODE_SAMPLE
private function resolveNewVisibilityForNode(Property $propertyNode): string
{
$nodeParentClassName = $propertyNode->getAttribute(Attribute::PARENT_CLASS_NAME);
$nodeParentClassName = $propertyNode->getAttribute(MetadataAttribute::PARENT_CLASS_NAME);
$propertyName = $propertyNode->props[0]->name->toString();
return $this->propertyToVisibilityByClass[$nodeParentClassName][$propertyName];