1
0
Fork 0

Prevent duplicate references

pull/52/head
Felix Becker 2016-10-11 17:48:44 +02:00
parent a92d25535a
commit 3a4fd9b1dd
1 changed files with 25 additions and 25 deletions

View File

@ -325,33 +325,31 @@ class PhpDocument
*/ */
public function getReferencedFqn(Node $node) public function getReferencedFqn(Node $node)
{ {
if ($node instanceof Node\Name) { $parent = $node->getAttribute('parentNode');
$nameNode = $node;
$node = $node->getAttribute('parentNode');
}
if ( if (
($node instanceof Node\Stmt\ClassLike $node instanceof Node\Name && (
|| $node instanceof Node\Param $parent instanceof Node\Stmt\ClassLike
|| $node instanceof Node\Stmt\Function_) || $parent instanceof Node\Param
&& isset($nameNode) || $parent instanceof Node\Stmt\Function_
)
) { ) {
// For extends, implements and type hints use the name directly // For extends, implements and type hints use the name directly
$name = (string)$nameNode; $name = (string)$node;
// Only the name node should be considered a reference, not the UseUse node itself // Only the name node should be considered a reference, not the UseUse node itself
} else if ($node instanceof Node\Stmt\UseUse && isset($nameNode)) { } else if ($parent instanceof Node\Stmt\UseUse) {
$name = (string)$node->name; $name = (string)$parent->name;
$parent = $node->getAttribute('parentNode'); $grandParent = $parent->getAttribute('parentNode');
if ($parent instanceof Node\Stmt\GroupUse) { if ($grandParent instanceof Node\Stmt\GroupUse) {
$name = $parent->prefix . '\\' . $name; $name = $grandParent->prefix . '\\' . $name;
} }
// Only the name node should be considered a reference, not the New_ node itself // Only the name node should be considered a reference, not the New_ node itself
} else if ($node instanceof Node\Expr\New_ && isset($nameNode)) { } else if ($parent instanceof Node\Expr\New_) {
if (!($node->class instanceof Node\Name)) { if (!($parent->class instanceof Node\Name)) {
// Cannot get definition of dynamic calls // Cannot get definition of dynamic calls
return null; return null;
} }
$name = (string)$node->class; $name = (string)$parent->class;
} else if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { } else if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) {
if ($node->name instanceof Node\Expr || !($node->var instanceof Node\Expr\Variable)) { if ($node->name instanceof Node\Expr || !($node->var instanceof Node\Expr\Variable)) {
// Cannot get definition of dynamic calls // Cannot get definition of dynamic calls
@ -383,13 +381,13 @@ class PhpDocument
return null; return null;
} }
$name .= '::' . (string)$node->name; $name .= '::' . (string)$node->name;
} else if ($node instanceof Node\Expr\FuncCall) { } else if ($parent instanceof Node\Expr\FuncCall) {
if ($node->name instanceof Node\Expr) { if ($parent->name instanceof Node\Expr) {
return null; return null;
} }
$name = (string)$node->name; $name = (string)$parent->name;
} else if ($node instanceof Node\Expr\ConstFetch) { } else if ($parent instanceof Node\Expr\ConstFetch) {
$name = (string)$node->name; $name = (string)$parent->name;
} else if ( } else if (
$node instanceof Node\Expr\ClassConstFetch $node instanceof Node\Expr\ClassConstFetch
|| $node instanceof Node\Expr\StaticPropertyFetch || $node instanceof Node\Expr\StaticPropertyFetch
@ -400,11 +398,13 @@ class PhpDocument
return null; return null;
} }
$name = (string)$node->class . '::' . $node->name; $name = (string)$node->class . '::' . $node->name;
} else {
return null;
} }
if ( if (
$node instanceof Node\Expr\MethodCall $node instanceof Node\Expr\MethodCall
|| $node instanceof Node\Expr\FuncCall
|| $node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\StaticCall
|| $parent instanceof Node\Expr\FuncCall
) { ) {
$name .= '()'; $name .= '()';
} }
@ -414,9 +414,9 @@ class PhpDocument
// If the node is a function or constant, it could be namespaced, but PHP falls back to global // If the node is a function or constant, it could be namespaced, but PHP falls back to global
// The NameResolver therefor does not resolve these to namespaced names // The NameResolver therefor does not resolve these to namespaced names
// http://php.net/manual/en/language.namespaces.fallback.php // http://php.net/manual/en/language.namespaces.fallback.php
if ($node instanceof Node\Expr\FuncCall || $node instanceof Node\Expr\ConstFetch) { if ($parent instanceof Node\Expr\FuncCall || $parent instanceof Node\Expr\ConstFetch) {
// Find and try with namespace // Find and try with namespace
$n = $node; $n = $parent;
while (isset($n)) { while (isset($n)) {
$n = $n->getAttribute('parentNode'); $n = $n->getAttribute('parentNode');
if ($n instanceof Node\Stmt\Namespace_) { if ($n instanceof Node\Stmt\Namespace_) {