2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var null|PHPParser_Node_Name Current namespace
|
|
|
|
*/
|
|
|
|
protected $namespace;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Currently defined namespace and class aliases
|
|
|
|
*/
|
|
|
|
protected $aliases;
|
|
|
|
|
|
|
|
public function beforeTraverse(array $nodes) {
|
|
|
|
$this->namespace = null;
|
|
|
|
$this->aliases = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enterNode(PHPParser_Node $node) {
|
|
|
|
if ($node instanceof PHPParser_Node_Stmt_Namespace) {
|
|
|
|
$this->namespace = $node->name;
|
|
|
|
$this->aliases = array();
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_UseUse) {
|
|
|
|
if (isset($this->aliases[$node->alias])) {
|
|
|
|
throw new PHPParser_Error(
|
|
|
|
sprintf(
|
|
|
|
'Cannot use %s as %s because the name is already in use',
|
|
|
|
$node->name, $node->alias
|
|
|
|
),
|
|
|
|
$node->getLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->aliases[$node->alias] = $node->name;
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_Class) {
|
|
|
|
if (null !== $node->extends) {
|
|
|
|
$node->extends = $this->resolveClassName($node->extends);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->implements as &$interface) {
|
|
|
|
$interface = $this->resolveClassName($interface);
|
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
|
|
|
$this->addNamespacedName($node);
|
2011-09-28 20:14:27 +02:00
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_Interface) {
|
|
|
|
foreach ($node->extends as &$interface) {
|
|
|
|
$interface = $this->resolveClassName($interface);
|
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2011-11-12 18:29:50 +01:00
|
|
|
$this->addNamespacedName($node);
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_Trait) {
|
2011-11-12 13:24:59 +01:00
|
|
|
$this->addNamespacedName($node);
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_Function) {
|
|
|
|
$this->addNamespacedName($node);
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Stmt_Const) {
|
|
|
|
foreach ($node->consts as $const) {
|
|
|
|
$this->addNamespacedName($const);
|
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
} elseif ($node instanceof PHPParser_Node_Expr_StaticCall
|
|
|
|
|| $node instanceof PHPParser_Node_Expr_StaticPropertyFetch
|
|
|
|
|| $node instanceof PHPParser_Node_Expr_ClassConstFetch
|
|
|
|
|| $node instanceof PHPParser_Node_Expr_New
|
|
|
|
|| $node instanceof PHPParser_Node_Expr_Instanceof
|
|
|
|
) {
|
2011-11-06 17:16:40 +01:00
|
|
|
if ($node->class instanceof PHPParser_Node_Name) {
|
|
|
|
$node->class = $this->resolveClassName($node->class);
|
|
|
|
}
|
2011-09-29 18:51:12 +02:00
|
|
|
} elseif ($node instanceof PHPParser_Node_Expr_FuncCall
|
|
|
|
|| $node instanceof PHPParser_Node_Expr_ConstFetch
|
2011-09-28 20:14:27 +02:00
|
|
|
) {
|
2011-11-06 17:16:40 +01:00
|
|
|
if ($node->name instanceof PHPParser_Node_Name) {
|
|
|
|
$node->name = $this->resolveOtherName($node->name);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof PHPParser_Node_Param
|
|
|
|
&& $node instanceof PHPParser_Node_Name
|
|
|
|
) {
|
2011-09-28 20:14:27 +02:00
|
|
|
$node->type = $this->resolveClassName($node->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:16:40 +01:00
|
|
|
protected function resolveClassName(PHPParser_Node_Name $name) {
|
2011-09-28 20:14:27 +02:00
|
|
|
// don't resolve special class names
|
|
|
|
if (in_array((string) $name, array('self', 'parent', 'static'))) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fully qualified names are already resolved
|
|
|
|
if ($name->isFullyQualified()) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve aliases (for non-relative names)
|
|
|
|
if (!$name->isRelative() && isset($this->aliases[$name->getFirst()])) {
|
|
|
|
$name->setFirst($this->aliases[$name->getFirst()]);
|
|
|
|
// if no alias exists prepend current namespace
|
|
|
|
} elseif (null !== $this->namespace) {
|
|
|
|
$name->prepend($this->namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PHPParser_Node_Name_FullyQualified($name->parts);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:16:40 +01:00
|
|
|
protected function resolveOtherName(PHPParser_Node_Name $name) {
|
2011-09-28 20:29:19 +02:00
|
|
|
// fully qualified names are already resolved and we can't do anything about unqualified
|
|
|
|
// ones at compiler-time
|
|
|
|
if ($name->isFullyQualified() || $name->isUnqualified()) {
|
2011-09-28 20:14:27 +02:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve aliases for qualified names
|
|
|
|
if ($name->isQualified() && isset($this->aliases[$name->getFirst()])) {
|
|
|
|
$name->setFirst($this->aliases[$name->getFirst()]);
|
2011-09-28 20:29:19 +02:00
|
|
|
// prepend namespace for relative names
|
|
|
|
} elseif (null !== $this->namespace) {
|
|
|
|
$name->prepend($this->namespace);
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 20:29:19 +02:00
|
|
|
return new PHPParser_Node_Name_FullyQualified($name->parts);
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
|
|
|
protected function addNamespacedName(PHPParser_Node $node) {
|
|
|
|
if (null !== $this->namespace) {
|
|
|
|
$node->namespacedName = clone $this->namespace;
|
|
|
|
$node->namespacedName->append($node->name);
|
|
|
|
} else {
|
|
|
|
$node->namespacedName = new PHPParser_Node_Name($node->name);
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|