2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2017-04-27 21:04:51 +02:00
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
|
2018-01-10 17:24:26 +01:00
|
|
|
class NameContext
|
|
|
|
{
|
2017-04-27 21:04:51 +02:00
|
|
|
/** @var null|Name Current namespace */
|
|
|
|
protected $namespace;
|
|
|
|
|
2017-08-15 22:48:24 +02:00
|
|
|
/** @var Name[][] Map of format [aliasType => [aliasName => originalName]] */
|
2017-04-27 21:04:51 +02:00
|
|
|
protected $aliases = [];
|
|
|
|
|
2017-08-15 22:48:24 +02:00
|
|
|
/** @var Name[][] Same as $aliases but preserving original case */
|
2017-04-28 17:10:30 +02:00
|
|
|
protected $origAliases = [];
|
|
|
|
|
2017-04-27 21:04:51 +02:00
|
|
|
/** @var ErrorHandler Error handler */
|
|
|
|
protected $errorHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a name context.
|
|
|
|
*
|
|
|
|
* @param ErrorHandler $errorHandler Error handling used to report errors
|
|
|
|
*/
|
|
|
|
public function __construct(ErrorHandler $errorHandler) {
|
|
|
|
$this->errorHandler = $errorHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a new namespace.
|
|
|
|
*
|
|
|
|
* This also resets the alias table.
|
|
|
|
*
|
|
|
|
* @param Name|null $namespace Null is the global namespace
|
|
|
|
*/
|
|
|
|
public function startNamespace(Name $namespace = null) {
|
|
|
|
$this->namespace = $namespace;
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->origAliases = $this->aliases = [
|
2017-04-27 21:04:51 +02:00
|
|
|
Stmt\Use_::TYPE_NORMAL => [],
|
|
|
|
Stmt\Use_::TYPE_FUNCTION => [],
|
|
|
|
Stmt\Use_::TYPE_CONSTANT => [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an alias / import.
|
|
|
|
*
|
|
|
|
* @param Name $name Original name
|
|
|
|
* @param string $aliasName Aliased name
|
|
|
|
* @param int $type One of Stmt\Use_::TYPE_*
|
|
|
|
* @param array $errorAttrs Attributes to use to report an error
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []) {
|
2017-04-27 21:04:51 +02:00
|
|
|
// Constant names are case sensitive, everything else case insensitive
|
|
|
|
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
|
|
|
$aliasLookupName = $aliasName;
|
|
|
|
} else {
|
|
|
|
$aliasLookupName = strtolower($aliasName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->aliases[$type][$aliasLookupName])) {
|
2017-08-13 14:06:08 +02:00
|
|
|
$typeStringMap = [
|
2017-04-27 21:04:51 +02:00
|
|
|
Stmt\Use_::TYPE_NORMAL => '',
|
|
|
|
Stmt\Use_::TYPE_FUNCTION => 'function ',
|
|
|
|
Stmt\Use_::TYPE_CONSTANT => 'const ',
|
2017-08-13 14:06:08 +02:00
|
|
|
];
|
2017-04-27 21:04:51 +02:00
|
|
|
|
|
|
|
$this->errorHandler->handleError(new Error(
|
|
|
|
sprintf(
|
|
|
|
'Cannot use %s%s as %s because the name is already in use',
|
|
|
|
$typeStringMap[$type], $name, $aliasName
|
|
|
|
),
|
|
|
|
$errorAttrs
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->aliases[$type][$aliasLookupName] = $name;
|
2017-04-28 17:10:30 +02:00
|
|
|
$this->origAliases[$type][$aliasName] = $name;
|
2017-04-27 21:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current namespace.
|
|
|
|
*
|
|
|
|
* @return null|Name Namespace (or null if global namespace)
|
|
|
|
*/
|
|
|
|
public function getNamespace() {
|
|
|
|
return $this->namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 17:15:24 +02:00
|
|
|
* Get resolved name.
|
2017-04-27 21:04:51 +02:00
|
|
|
*
|
2017-04-28 17:15:24 +02:00
|
|
|
* @param Name $name Name to resolve
|
|
|
|
* @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
|
2017-04-27 21:04:51 +02:00
|
|
|
*
|
2017-04-28 17:15:24 +02:00
|
|
|
* @return null|Name Resolved name, or null if static resolution is not possible
|
2017-04-27 21:04:51 +02:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getResolvedName(Name $name, int $type) {
|
2017-04-27 21:04:51 +02:00
|
|
|
// don't resolve special class names
|
2017-08-15 22:56:53 +02:00
|
|
|
if ($type === Stmt\Use_::TYPE_NORMAL && $name->isSpecialClassName()) {
|
2017-04-27 21:04:51 +02:00
|
|
|
if (!$name->isUnqualified()) {
|
|
|
|
$this->errorHandler->handleError(new Error(
|
|
|
|
sprintf("'\\%s' is an invalid class name", $name->toString()),
|
|
|
|
$name->getAttributes()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fully qualified names are already resolved
|
|
|
|
if ($name->isFullyQualified()) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
// Try to resolve aliases
|
|
|
|
if (null !== $resolvedName = $this->resolveAlias($name, $type)) {
|
|
|
|
return $resolvedName;
|
2017-04-27 21:04:51 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 17:15:24 +02:00
|
|
|
if ($type !== Stmt\Use_::TYPE_NORMAL && $name->isUnqualified()) {
|
2017-04-27 21:04:51 +02:00
|
|
|
if (null === $this->namespace) {
|
|
|
|
// outside of a namespace unaliased unqualified is same as fully qualified
|
|
|
|
return new FullyQualified($name, $name->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot resolve statically
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no alias exists prepend current namespace
|
|
|
|
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
|
|
|
|
}
|
2017-04-28 17:10:30 +02:00
|
|
|
|
2017-04-28 17:15:24 +02:00
|
|
|
/**
|
|
|
|
* Get resolved class name.
|
|
|
|
*
|
|
|
|
* @param Name $name Class ame to resolve
|
|
|
|
*
|
|
|
|
* @return Name Resolved name
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getResolvedClassName(Name $name) : Name {
|
2017-04-28 17:15:24 +02:00
|
|
|
return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
/**
|
2017-06-13 19:51:22 +02:00
|
|
|
* Get possible ways of writing a fully qualified name (e.g., by making use of aliases).
|
2017-04-28 17:10:30 +02:00
|
|
|
*
|
2017-06-13 19:51:22 +02:00
|
|
|
* @param string $name Fully-qualified name (without leading namespace separator)
|
|
|
|
* @param int $type One of Stmt\Use_::TYPE_*
|
2017-04-28 17:10:30 +02:00
|
|
|
*
|
|
|
|
* @return Name[] Possible representations of the name
|
|
|
|
*/
|
2017-06-13 19:51:22 +02:00
|
|
|
public function getPossibleNames(string $name, int $type) : array {
|
2017-04-28 17:10:30 +02:00
|
|
|
$lcName = strtolower($name);
|
|
|
|
|
2017-06-13 19:51:22 +02:00
|
|
|
if ($type === Stmt\Use_::TYPE_NORMAL) {
|
|
|
|
// self, parent and static must always be unqualified
|
|
|
|
if ($lcName === "self" || $lcName === "parent" || $lcName === "static") {
|
|
|
|
return [new Name($name)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
// Collect possible ways to write this name, starting with the fully-qualified name
|
2017-06-13 19:51:22 +02:00
|
|
|
$possibleNames = [new FullyQualified($name)];
|
2017-04-28 17:10:30 +02:00
|
|
|
|
2017-06-13 19:51:22 +02:00
|
|
|
if (null !== $nsRelativeName = $this->getNamespaceRelativeName($name, $lcName, $type)) {
|
2017-04-28 17:10:30 +02:00
|
|
|
// Make sure there is no alias that makes the normally namespace-relative name
|
|
|
|
// into something else
|
|
|
|
if (null === $this->resolveAlias($nsRelativeName, $type)) {
|
|
|
|
$possibleNames[] = $nsRelativeName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for relevant namespace use statements
|
|
|
|
foreach ($this->origAliases[Stmt\Use_::TYPE_NORMAL] as $alias => $orig) {
|
2017-08-15 22:48:24 +02:00
|
|
|
$lcOrig = $orig->toLowerString();
|
2017-04-28 17:10:30 +02:00
|
|
|
if (0 === strpos($lcName, $lcOrig . '\\')) {
|
|
|
|
$possibleNames[] = new Name($alias . substr($name, strlen($lcOrig)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for relevant type-specific use statements
|
|
|
|
foreach ($this->origAliases[$type] as $alias => $orig) {
|
|
|
|
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
|
|
|
// Constants are are complicated-sensitive
|
2017-08-18 22:57:27 +02:00
|
|
|
$normalizedOrig = $this->normalizeConstName($orig->toString());
|
|
|
|
if ($normalizedOrig === $this->normalizeConstName($name)) {
|
2017-04-28 17:10:30 +02:00
|
|
|
$possibleNames[] = new Name($alias);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Everything else is case-insensitive
|
2017-08-15 22:48:24 +02:00
|
|
|
if ($orig->toLowerString() === $lcName) {
|
2017-04-28 17:10:30 +02:00
|
|
|
$possibleNames[] = new Name($alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $possibleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shortest representation of this fully-qualified name.
|
|
|
|
*
|
2017-06-13 19:51:22 +02:00
|
|
|
* @param string $name Fully-qualified name (without leading namespace separator)
|
|
|
|
* @param int $type One of Stmt\Use_::TYPE_*
|
2017-04-28 17:10:30 +02:00
|
|
|
*
|
|
|
|
* @return Name Shortest representation
|
|
|
|
*/
|
2017-06-13 19:51:22 +02:00
|
|
|
public function getShortName(string $name, int $type) : Name {
|
2017-04-28 17:10:30 +02:00
|
|
|
$possibleNames = $this->getPossibleNames($name, $type);
|
|
|
|
|
|
|
|
// Find shortest name
|
|
|
|
$shortestName = null;
|
2017-11-10 23:33:12 +01:00
|
|
|
$shortestLength = \INF;
|
2017-04-28 17:10:30 +02:00
|
|
|
foreach ($possibleNames as $possibleName) {
|
|
|
|
$length = strlen($possibleName->toCodeString());
|
|
|
|
if ($length < $shortestLength) {
|
|
|
|
$shortestName = $possibleName;
|
|
|
|
$shortestLength = $length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shortestName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveAlias(Name $name, $type) {
|
|
|
|
$firstPart = $name->getFirst();
|
|
|
|
|
|
|
|
if ($name->isQualified()) {
|
|
|
|
// resolve aliases for qualified names, always against class alias table
|
|
|
|
$checkName = strtolower($firstPart);
|
|
|
|
if (isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName])) {
|
|
|
|
$alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName];
|
|
|
|
return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
|
|
|
|
}
|
|
|
|
} elseif ($name->isUnqualified()) {
|
|
|
|
// constant aliases are case-sensitive, function aliases case-insensitive
|
|
|
|
$checkName = $type === Stmt\Use_::TYPE_CONSTANT ? $firstPart : strtolower($firstPart);
|
|
|
|
if (isset($this->aliases[$type][$checkName])) {
|
|
|
|
// resolve unqualified aliases
|
|
|
|
return new FullyQualified($this->aliases[$type][$checkName], $name->getAttributes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No applicable aliases
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:51:22 +02:00
|
|
|
private function getNamespaceRelativeName(string $name, string $lcName, int $type) {
|
2017-04-28 17:10:30 +02:00
|
|
|
if (null === $this->namespace) {
|
|
|
|
return new Name($name);
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:51:22 +02:00
|
|
|
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
|
|
|
// The constants true/false/null always resolve to the global symbols, even inside a
|
|
|
|
// namespace, so they may be used without qualification
|
|
|
|
if ($lcName === "true" || $lcName === "false" || $lcName === "null") {
|
|
|
|
return new Name($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:10:30 +02:00
|
|
|
$namespacePrefix = strtolower($this->namespace . '\\');
|
|
|
|
if (0 === strpos($lcName, $namespacePrefix)) {
|
2017-06-13 19:51:22 +02:00
|
|
|
return new Name(substr($name, strlen($namespacePrefix)));
|
2017-04-28 17:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-13 19:51:22 +02:00
|
|
|
private function normalizeConstName(string $name) {
|
2017-04-28 17:10:30 +02:00
|
|
|
$nsSep = strrpos($name, '\\');
|
|
|
|
if (false === $nsSep) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants have case-insensitive namespace and case-sensitive short-name
|
|
|
|
$ns = substr($name, 0, $nsSep);
|
|
|
|
$shortName = substr($name, $nsSep + 1);
|
|
|
|
return strtolower($ns) . '\\' . $shortName;
|
|
|
|
}
|
2017-04-27 21:04:51 +02:00
|
|
|
}
|