mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[TriggerExtractor] add Scope
This commit is contained in:
parent
27c9d22d5a
commit
2dd5ef5f7b
51
packages/NodeTypeResolver/src/NodeVisitor/ScopeResolver.php
Normal file
51
packages/NodeTypeResolver/src/NodeVisitor/ScopeResolver.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeTypeResolver\NodeVisitor;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Node\Attribute;
|
||||
|
||||
/**
|
||||
* In class, in interface, in trait, in method or in function?
|
||||
*/
|
||||
final class ScopeResolver extends NodeVisitorAbstract
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $currentScope;
|
||||
|
||||
public function beforeTraverse(array $nodes): void
|
||||
{
|
||||
$this->currentScope = null;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): void
|
||||
{
|
||||
if ($node instanceof Function_) {
|
||||
$this->currentScope = 'scope_function';
|
||||
}
|
||||
|
||||
if (($node instanceof Class_ && $node->isAnonymous()) || $node instanceof Property) {
|
||||
$this->currentScope = 'scope_class';
|
||||
}
|
||||
|
||||
if ($node instanceof Interface_) {
|
||||
$this->currentScope = 'scope_interface';
|
||||
}
|
||||
|
||||
if ($node instanceof ClassMethod) {
|
||||
$this->currentScope = 'scope_class_method';
|
||||
}
|
||||
|
||||
if ($this->currentScope) {
|
||||
$node->setAttribute(Attribute::SCOPE, $this->currentScope);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,18 +24,25 @@ final class DeprecationFactory
|
|||
* Probably resolve by recursion, similar too
|
||||
* @see \Rector\NodeTypeResolver\NodeVisitor\TypeResolver::__construct()
|
||||
*/
|
||||
public function createFromNode(Node $node): DeprecationInterface
|
||||
public function createFromNode(Node $node, string $scope): DeprecationInterface
|
||||
{
|
||||
if ($scope === 'scope_class_method') {
|
||||
// new class method deprecation
|
||||
|
||||
dump('tada');
|
||||
die;
|
||||
}
|
||||
|
||||
$message = '';
|
||||
if ($node instanceof Concat) {
|
||||
$message .= $this->processConcatNode($node->left);
|
||||
$message .= $this->processConcatNode($node->right);
|
||||
}
|
||||
|
||||
return $this->createFromMesssage($message);
|
||||
return $this->createFromMesssage($message, $scope);
|
||||
}
|
||||
|
||||
private function processConcatNode(Node $node): string
|
||||
private function processConcatNode(Node $node, string $scope): string
|
||||
{
|
||||
if ($node instanceof Method) {
|
||||
$classMethodNode = $this->findParentOfType($node, ClassMethod::class);
|
||||
|
|
|
@ -40,12 +40,10 @@ final class DeprecationDetector extends NodeVisitorAbstract
|
|||
return;
|
||||
}
|
||||
|
||||
// current scopde would be great
|
||||
dump($node->getAttribute(Attribute::PARENT_NODE)->getParent);
|
||||
die;
|
||||
$scope = $node->getAttribute(Attribute::SCOPE);
|
||||
|
||||
/** @var FuncCall $node */
|
||||
$deprecation = $this->deprecationFactory->createFromNode($node->args[0]->value);
|
||||
$deprecation = $this->deprecationFactory->createFromNode($node->args[0]->value, $scope);
|
||||
|
||||
$this->deprecationCollector->addDeprecation($deprecation);
|
||||
}
|
||||
|
|
|
@ -10,10 +10,19 @@ namespace Rector\Node;
|
|||
final class Attribute
|
||||
{
|
||||
/**
|
||||
* Class, interface or trait FQN type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
/**
|
||||
* In class, in interface, in trait, in method or in function
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const SCOPE = 'scope';
|
||||
|
||||
/**
|
||||
* System name to be found in @see \PhpParser\NodeVisitor\NameResolver
|
||||
* Do not change this even if you want!
|
||||
|
|
Loading…
Reference in New Issue
Block a user