Combine class/non-class name resolution into single method

This commit is contained in:
Nikita Popov 2017-04-28 17:15:24 +02:00
parent 56b810e91d
commit 7f6477ed83
2 changed files with 31 additions and 59 deletions

View File

@ -2,6 +2,7 @@
namespace PhpParser;
use PhpParser\Builder\Use_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
@ -91,15 +92,17 @@ class NameContext {
}
/**
* Get resolved class name.
* Get resolved name.
*
* @param Name $name Class ame to resolve
* @param Name $name Name to resolve
* @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
*
* @return Name Resolved name
* @return null|Name Resolved name, or null if static resolution is not possible
*/
public function getResolvedClassName(Name $name) {
public function getResolvedName(Name $name, $type) {
// don't resolve special class names
if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
if ($type === Stmt\Use_::TYPE_NORMAL
&& in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
if (!$name->isUnqualified()) {
$this->errorHandler->handleError(new Error(
sprintf("'\\%s' is an invalid class name", $name->toString()),
@ -114,35 +117,12 @@ class NameContext {
return $name;
}
// Try to resolve aliases
if (null !== $resolvedName = $this->resolveAlias($name, Stmt\Use_::TYPE_NORMAL)) {
return $resolvedName;
}
// if no alias exists prepend current namespace
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
}
/**
* Get resolved function or constant name.
*
* @param Name $name Function or constant name to resolve
* @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
*
* @return null|Name Resolved name, or null if static resolution is not possible
*/
public function getResolvedOtherName(Name $name, $type) {
// fully qualified names are already resolved
if ($name->isFullyQualified()) {
return $name;
}
// Try to resolve aliases
if (null !== $resolvedName = $this->resolveAlias($name, $type)) {
return $resolvedName;
}
if ($name->isUnqualified()) {
if ($type !== Stmt\Use_::TYPE_NORMAL && $name->isUnqualified()) {
if (null === $this->namespace) {
// outside of a namespace unaliased unqualified is same as fully qualified
return new FullyQualified($name, $name->getAttributes());
@ -156,6 +136,17 @@ class NameContext {
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
}
/**
* Get resolved class name.
*
* @param Name $name Class ame to resolve
*
* @return Name Resolved name
*/
public function getResolvedClassName(Name $name) {
return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL);
}
/**
* Get possible ways of writing a fully qualified name (e.g., by making use of aliases)
*

View File

@ -113,10 +113,10 @@ class NameResolver extends NodeVisitorAbstract
}
} elseif ($node instanceof Expr\FuncCall) {
if ($node->name instanceof Name) {
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION);
$node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION);
}
} elseif ($node instanceof Expr\ConstFetch) {
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_CONSTANT);
$node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT);
} elseif ($node instanceof Stmt\TraitUse) {
foreach ($node->traits as &$trait) {
$trait = $this->resolveClassName($trait);
@ -167,39 +167,16 @@ class NameResolver extends NodeVisitorAbstract
}
/**
* Resolve class name, according to name resolver options.
*
* @param Name $name Class ame to resolve
*
* @return Name Resolved name, or original name with attribute
*/
protected function resolveClassName(Name $name) {
if (!$this->replaceNodes) {
$name->setAttribute('resolvedName', $this->nameContext->getResolvedClassName($name));
return $name;
}
if ($this->preserveOriginalNames) {
// Save the original name
$originalName = $name;
$name = clone $originalName;
$name->setAttribute('originalName', $originalName);
}
return $this->nameContext->getResolvedClassName($name);
}
/**
* Resolve function or constant name, according to name resolver options.
* Resolve name, according to name resolver options.
*
* @param Name $name Function or constant name to resolve
* @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
* @param int $type One of Stmt\Use_::TYPE_*
*
* @return Name Resolved name, or original name with attribute
*/
protected function resolveOtherName(Name $name, $type) {
protected function resolveName(Name $name, $type) {
if (!$this->replaceNodes) {
$resolvedName = $this->nameContext->getResolvedOtherName($name, $type);
$resolvedName = $this->nameContext->getResolvedName($name, $type);
if (null !== $resolvedName) {
$name->setAttribute('resolvedName', $resolvedName);
} else {
@ -216,7 +193,7 @@ class NameResolver extends NodeVisitorAbstract
$name->setAttribute('originalName', $originalName);
}
$resolvedName = $this->nameContext->getResolvedOtherName($name, $type);
$resolvedName = $this->nameContext->getResolvedName($name, $type);
if (null !== $resolvedName) {
return $resolvedName;
}
@ -228,6 +205,10 @@ class NameResolver extends NodeVisitorAbstract
return $name;
}
protected function resolveClassName(Name $name) {
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
}
protected function addNamespacedName(Node $node) {
$node->namespacedName = Name::concat(
$this->nameContext->getNamespace(), (string) $node->name);