Properly handle fully qualified and relative names

This commit is contained in:
nikic 2011-08-04 13:23:35 +02:00
parent 2703f42933
commit 9c53838b1f
5 changed files with 58 additions and 11 deletions

View File

@ -570,8 +570,8 @@ class_name:
name:
namespace_name { $$ = $1; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $3->resolveType(Name::RELATIVE); $$ = $3; }
| T_NS_SEPARATOR namespace_name { $2->resolveType(Name::ABSOLUTE); $$ = $2; }
| T_NAMESPACE T_NS_SEPARATOR namespace_name { $3->setResolveType(Name::RELATIVE); $$ = $3; }
| T_NS_SEPARATOR namespace_name { $2->setResolveType(Name::FULLY_QUALIFIED); $$ = $2; }
;
class_name_reference:

View File

@ -5,15 +5,25 @@
*/
class PHPParser_Node_Name extends PHPParser_NodeAbstract
{
const ABSOLUTE = 1;
const RELATIVE = 2;
const NORMAL = 0;
const FULLY_QUALIFIED = 1;
const RELATIVE = 2;
protected $resolveType;
protected $resolveType = self::NORMAL;
public function resolveType($type) {
public function setResolveType($type) {
$this->resolveType = $type;
}
/**
* Gets the first part of the name, i.e. everything before the first namespace separator.
*
* @return string First part of the name
*/
public function getFirst() {
return $this->parts[0];
}
/**
* Gets the last part of the name, i.e. everything after the last namespace separator.
*
@ -23,6 +33,42 @@ class PHPParser_Node_Name extends PHPParser_NodeAbstract
return $this->parts[count($this->parts) - 1];
}
/**
* Checks whether the name is unqualified. (E.g. Name)
*
* @return bool Whether the name is unqualified
*/
public function isUnqualified() {
return self::NORMAL == $this->resolveType && 1 == count($this->parts);
}
/**
* Checks whether the name is qualified. (E.g. Name\Name)
*
* @return bool Whether the name is qualified
*/
public function isQualified() {
return self::NORMAL == $this->resolveType && 1 < count($this->parts);
}
/**
* Checks whether the name is fully qualified. (E.g. \Name)
*
* @return bool Whether the name is fully qualified
*/
public function isFullyQualified() {
return self::FULLY_QUALIFIED == $this->resolveType;
}
/**
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
*
* @return bool Whether the name is fully qualified
*/
public function isRelative() {
return self::RELATIVE == $this->resolveType;
}
/**
* Returns a string representation of the name by imploding the namespace parts with a separator.
*

View File

@ -1998,11 +1998,11 @@ class PHPParser_Parser
}
private function yyn246($line, $docComment) {
$this->yyastk[$this->yysp-(3-3)]->resolveType(PHPParser_Node_Name::RELATIVE); $this->yyval = $this->yyastk[$this->yysp-(3-3)];
$this->yyastk[$this->yysp-(3-3)]->setResolveType(PHPParser_Node_Name::RELATIVE); $this->yyval = $this->yyastk[$this->yysp-(3-3)];
}
private function yyn247($line, $docComment) {
$this->yyastk[$this->yysp-(2-2)]->resolveType(PHPParser_Node_Name::ABSOLUTE); $this->yyval = $this->yyastk[$this->yysp-(2-2)];
$this->yyastk[$this->yysp-(2-2)]->setResolveType(PHPParser_Node_Name::FULLY_QUALIFIED); $this->yyval = $this->yyastk[$this->yysp-(2-2)];
}
private function yyn248($line, $docComment) {

View File

@ -2397,11 +2397,11 @@ class PHPParser_ParserDebug
}
private function yyn246($line, $docComment) {
$this->yyastk[$this->yysp-(3-3)]->resolveType(PHPParser_Node_Name::RELATIVE); $this->yyval = $this->yyastk[$this->yysp-(3-3)];
$this->yyastk[$this->yysp-(3-3)]->setResolveType(PHPParser_Node_Name::RELATIVE); $this->yyval = $this->yyastk[$this->yysp-(3-3)];
}
private function yyn247($line, $docComment) {
$this->yyastk[$this->yysp-(2-2)]->resolveType(PHPParser_Node_Name::ABSOLUTE); $this->yyval = $this->yyastk[$this->yysp-(2-2)];
$this->yyastk[$this->yysp-(2-2)]->setResolveType(PHPParser_Node_Name::FULLY_QUALIFIED); $this->yyval = $this->yyastk[$this->yysp-(2-2)];
}
private function yyn248($line, $docComment) {

View File

@ -5,7 +5,8 @@ class PHPParser_PrettyPrinter_Zend extends PHPParser_PrettyPrinterAbstract
// Names and Variables
public function pName(PHPParser_Node_Name $node) {
return implode('\\', $node->parts);
return ($node->isFullyQualified() ? '\\' : ($node->isRelative() ? 'namespace\\' : ''))
. implode('\\', $node->parts);
}
public function pVariable(PHPParser_Node_Variable $node) {