[TriggerExtractor] add Scope

This commit is contained in:
TomasVotruba 2017-09-07 16:04:20 +02:00
parent 27c9d22d5a
commit 2dd5ef5f7b
4 changed files with 72 additions and 7 deletions

View 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);
}
}
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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!