mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
remove FUNCTION_NODE attribute
This commit is contained in:
parent
de02a85e3f
commit
3746f24cf4
@ -41,7 +41,6 @@ expectedArguments(
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NAME,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::CLOSURE_NODE,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::PARAMETER_POSITION,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::ARGUMENT_POSITION,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::IS_FIRST_LEVEL_STATEMENT,
|
||||
@ -72,7 +71,6 @@ expectedArguments(
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NAME,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::COMMENTS,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::CLOSURE_NODE,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::PARAMETER_POSITION,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::ARGUMENT_POSITION,
|
||||
\Rector\NodeTypeResolver\Node\AttributeKey::IS_FIRST_LEVEL_STATEMENT,
|
||||
|
@ -11,7 +11,6 @@ use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class ParentScopeFinder
|
||||
{
|
||||
@ -30,10 +29,12 @@ final class ParentScopeFinder
|
||||
*/
|
||||
public function find(Node $node): ?Node
|
||||
{
|
||||
return $node->getAttribute(AttributeKey::CLOSURE_NODE) ??
|
||||
$node->getAttribute(AttributeKey::FUNCTION_NODE) ??
|
||||
$node->getAttribute(AttributeKey::METHOD_NODE) ??
|
||||
$node->getAttribute(AttributeKey::CLASS_NODE) ??
|
||||
$this->betterNodeFinder->findParentType($node, Namespace_::class);
|
||||
return $this->betterNodeFinder->findParentTypes($node, [
|
||||
Closure::class,
|
||||
Function_::class,
|
||||
ClassMethod::class,
|
||||
Class_::class,
|
||||
Namespace_::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeTypeResolver\Node;
|
||||
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
@ -47,11 +46,6 @@ final class AttributeKey
|
||||
*/
|
||||
public const METHOD_NODE = 'methodNode';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FUNCTION_NODE = 'functionNode';
|
||||
|
||||
/**
|
||||
* Internal php-parser name.
|
||||
* Do not change this even if you want!
|
||||
@ -170,11 +164,6 @@ final class AttributeKey
|
||||
*/
|
||||
public const DO_NOT_CHANGE = 'do_not_change';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CLOSURE_NODE = Closure::class;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -74,8 +74,6 @@ final class FunctionMethodAndClassNodeVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
$this->processClass($node);
|
||||
$this->processMethod($node);
|
||||
$this->processFunction($node);
|
||||
$this->processClosure($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
@ -123,24 +121,6 @@ final class FunctionMethodAndClassNodeVisitor extends NodeVisitorAbstract
|
||||
$node->setAttribute(AttributeKey::METHOD_NODE, $this->classMethod);
|
||||
}
|
||||
|
||||
private function processFunction(Node $node): void
|
||||
{
|
||||
if ($node instanceof Function_) {
|
||||
$this->function = $node;
|
||||
}
|
||||
|
||||
$node->setAttribute(AttributeKey::FUNCTION_NODE, $this->function);
|
||||
}
|
||||
|
||||
private function processClosure(Node $node): void
|
||||
{
|
||||
if ($node instanceof Closure) {
|
||||
$this->closure = $node;
|
||||
}
|
||||
|
||||
$node->setAttribute(AttributeKey::CLOSURE_NODE, $this->closure);
|
||||
}
|
||||
|
||||
private function setClassNodeAndName(?ClassLike $classLike): void
|
||||
{
|
||||
$this->classLike = $classLike;
|
||||
|
@ -70,8 +70,7 @@ final class InvertedIfFactory
|
||||
|
||||
private function getNextReturnExpr(If_ $if): ?Node
|
||||
{
|
||||
/** @var Closure|null $closure */
|
||||
$closure = $if->getAttribute(AttributeKey::CLOSURE_NODE);
|
||||
$closure = $this->betterNodeFinder->findParentType($if, Closure::class);
|
||||
if ($closure instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Naming\ValueObject\VariableAndCallForeach;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class ForeachMatcher
|
||||
{
|
||||
@ -26,10 +26,19 @@ final class ForeachMatcher
|
||||
*/
|
||||
private $callMatcher;
|
||||
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, CallMatcher $callMatcher)
|
||||
{
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
CallMatcher $callMatcher,
|
||||
BetterNodeFinder $betterNodeFinder
|
||||
) {
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->callMatcher = $callMatcher;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
public function match(Foreach_ $foreach): ?VariableAndCallForeach
|
||||
@ -61,8 +70,9 @@ final class ForeachMatcher
|
||||
*/
|
||||
private function getFunctionLike(Foreach_ $foreach): ?FunctionLike
|
||||
{
|
||||
return $foreach->getAttribute(AttributeKey::CLOSURE_NODE) ??
|
||||
$foreach->getAttribute(AttributeKey::METHOD_NODE) ??
|
||||
$foreach->getAttribute(AttributeKey::FUNCTION_NODE);
|
||||
return $this->betterNodeFinder->findParentTypes(
|
||||
$foreach,
|
||||
[Closure::class, ClassMethod::class, Function_::class]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Naming\ValueObject\VariableAndCallAssign;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class VariableAndCallAssignMatcher
|
||||
{
|
||||
@ -26,10 +26,19 @@ final class VariableAndCallAssignMatcher
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
public function __construct(CallMatcher $callMatcher, NodeNameResolver $nodeNameResolver)
|
||||
{
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(
|
||||
CallMatcher $callMatcher,
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
BetterNodeFinder $betterNodeFinder
|
||||
) {
|
||||
$this->callMatcher = $callMatcher;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
public function match(Assign $assign): ?VariableAndCallAssign
|
||||
@ -61,8 +70,9 @@ final class VariableAndCallAssignMatcher
|
||||
*/
|
||||
private function getFunctionLike(Assign $assign): ?FunctionLike
|
||||
{
|
||||
return $assign->getAttribute(AttributeKey::CLOSURE_NODE) ??
|
||||
$assign->getAttribute(AttributeKey::METHOD_NODE) ??
|
||||
$assign->getAttribute(AttributeKey::FUNCTION_NODE);
|
||||
return $this->betterNodeFinder->findParentTypes(
|
||||
$assign,
|
||||
[Closure::class, ClassMethod::class, Function_::class]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Naming\PhpDoc\VarTagValueNodeRenamer;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -41,16 +42,23 @@ final class VariableRenamer
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(
|
||||
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
VarTagValueNodeRenamer $varTagValueNodeRenamer,
|
||||
PhpDocInfoFactory $phpDocInfoFactory
|
||||
PhpDocInfoFactory $phpDocInfoFactory,
|
||||
BetterNodeFinder $betterNodeFinder
|
||||
) {
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->varTagValueNodeRenamer = $varTagValueNodeRenamer;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,7 +111,7 @@ final class VariableRenamer
|
||||
|
||||
private function isParamInParentFunction(Variable $variable): bool
|
||||
{
|
||||
$closure = $variable->getAttribute(AttributeKey::CLOSURE_NODE);
|
||||
$closure = $this->betterNodeFinder->findParentType($variable, Closure::class);
|
||||
if (! $closure instanceof Closure) {
|
||||
return false;
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNestingScope\ParentScopeFinder;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
@ -19,9 +20,15 @@ final class FunctionLikeFirstLevelStatementResolver
|
||||
*/
|
||||
private $parentScopeFinder;
|
||||
|
||||
public function __construct(ParentScopeFinder $parentScopeFinder)
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(ParentScopeFinder $parentScopeFinder, BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->parentScopeFinder = $parentScopeFinder;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
public function resolveFirstLevelStatement(Node $node): Node
|
||||
@ -57,7 +64,7 @@ final class FunctionLikeFirstLevelStatementResolver
|
||||
*/
|
||||
private function matchMultiplierClosure(Node $node): ?Closure
|
||||
{
|
||||
$closure = $node->getAttribute(AttributeKey::CLOSURE_NODE);
|
||||
$closure = $this->betterNodeFinder->findParentType($node, Closure::class);
|
||||
if (! $closure instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\NodeManipulator\ClassMethodManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
|
||||
use Rector\NodeCollector\StaticAnalyzer;
|
||||
use ReflectionMethod;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
@ -46,7 +47,7 @@ final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector
|
||||
private $reflectionProvider;
|
||||
|
||||
/**
|
||||
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
|
||||
* @var ParentClassScopeResolver
|
||||
*/
|
||||
private $parentClassScopeResolver;
|
||||
|
||||
@ -54,7 +55,7 @@ final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector
|
||||
ClassMethodManipulator $classMethodManipulator,
|
||||
StaticAnalyzer $staticAnalyzer,
|
||||
ReflectionProvider $reflectionProvider,
|
||||
\Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver $parentClassScopeResolver
|
||||
ParentClassScopeResolver $parentClassScopeResolver
|
||||
) {
|
||||
$this->classMethodManipulator = $classMethodManipulator;
|
||||
$this->staticAnalyzer = $staticAnalyzer;
|
||||
|
@ -108,15 +108,17 @@ CODE_SAMPLE
|
||||
return;
|
||||
}
|
||||
|
||||
$classMethodOrFunctionNode = $funcCall->getAttribute(AttributeKey::METHOD_NODE) ?:
|
||||
$funcCall->getAttribute(AttributeKey::FUNCTION_NODE);
|
||||
|
||||
if ($classMethodOrFunctionNode === null) {
|
||||
/** @var ClassMethod|Function_|null $classMethodOrFunction */
|
||||
$classMethodOrFunction = $this->betterNodeFinder->findParentTypes(
|
||||
$funcCall,
|
||||
[ClassMethod::class, Function_::class]
|
||||
);
|
||||
if ($classMethodOrFunction === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// dummy approach, improve when needed
|
||||
$this->replaceGetNameOrGetValue($classMethodOrFunctionNode, $assign->var);
|
||||
$this->replaceGetNameOrGetValue($classMethodOrFunction, $assign->var);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,11 +6,13 @@ namespace Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
|
||||
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\TypeDeclaration\Contract\TypeInferer\ParamTypeInfererInterface;
|
||||
|
||||
final class FunctionLikeDocParamTypeInferer implements ParamTypeInfererInterface
|
||||
@ -25,10 +27,19 @@ final class FunctionLikeDocParamTypeInferer implements ParamTypeInfererInterface
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, PhpDocInfoFactory $phpDocInfoFactory)
|
||||
{
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
PhpDocInfoFactory $phpDocInfoFactory,
|
||||
BetterNodeFinder $betterNodeFinder
|
||||
) {
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
public function inferParam(Param $param): Type
|
||||
@ -50,7 +61,7 @@ final class FunctionLikeDocParamTypeInferer implements ParamTypeInfererInterface
|
||||
|
||||
private function resolveScopeNode(Param $param): ?FunctionLike
|
||||
{
|
||||
return $param->getAttribute(AttributeKey::METHOD_NODE) ?? $param->getAttribute(AttributeKey::FUNCTION_NODE);
|
||||
return $this->betterNodeFinder->findParentTypes($param, [ClassMethod::class, Function_::class]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,12 +6,10 @@ namespace Rector\Visibility\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\Visibility;
|
||||
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
Loading…
x
Reference in New Issue
Block a user