Skip to content

Commit

Permalink
misc: simplify alias handling
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Sep 18, 2024
1 parent 594d5cd commit ae0f5bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 244 deletions.
21 changes: 8 additions & 13 deletions src/Type/Parser/Factory/Specifications/AliasSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,27 @@ public function manipulateToken(TraversingToken $token): TraversingToken

private function resolveAlias(string $symbol): string
{
$alias = $symbol;
$aliases = PhpParser::parseUseStatements($this->reflection);

$namespaceParts = explode('\\', $symbol);
$firstPart = array_shift($namespaceParts);

if ($firstPart) {
$alias = strtolower($firstPart);
$lastPart = strtolower(end($namespaceParts));

if (isset($aliases[$lastPart])) {
return $aliases[$lastPart];
}

$aliases = PhpParser::parseUseStatements($this->reflection);
$alias = strtolower(array_shift($namespaceParts));

if (! isset($aliases[$alias])) {
return $symbol;
}

$fqcn = $aliases[$alias]['fqcn'];

if ($namespaceParts === []) {
return $fqcn;
}

if (! $aliases[$alias]['isExplicitAlias']) {
return $symbol;
return $aliases[$alias];
}

return $fqcn . '\\' . implode('\\', $namespaceParts);
return $aliases[$alias] . '\\' . implode('\\', $namespaceParts);
}

private function resolveNamespaced(string $symbol): string
Expand Down
6 changes: 3 additions & 3 deletions src/Utility/Reflection/PhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
*/
final class PhpParser
{
/** @var array<string, array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}>> */
/** @var array<string, array<string, string>> */
private static array $statements = [];

/**
* @param ReflectionClass<object>|ReflectionFunction|ReflectionMethod $reflection
* @return array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}>
* @return array<string, string>
*/
public static function parseUseStatements(\ReflectionClass|\ReflectionFunction|\ReflectionMethod $reflection): array
{
Expand All @@ -34,7 +34,7 @@ public static function parseUseStatements(\ReflectionClass|\ReflectionFunction|\

/**
* @param ReflectionClass<object>|ReflectionFunction|ReflectionMethod $reflection
* @return array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}>
* @return array<string, string>
*/
private static function fetchUseStatements(\ReflectionClass|\ReflectionFunction|\ReflectionMethod $reflection): array
{
Expand Down
15 changes: 4 additions & 11 deletions src/Utility/Reflection/TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $content)
}

/**
* @return array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}>
* @return array<string, string>
*/
public function parseUseStatements(string $namespaceName): array
{
Expand Down Expand Up @@ -58,7 +58,7 @@ public function parseUseStatements(string $namespaceName): array
}

/**
* @return array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}>
* @return array<string, string>
*/
private function parseUseStatement(): array
{
Expand Down Expand Up @@ -86,18 +86,12 @@ private function parseUseStatement(): array
$explicitAlias = true;
$alias = '';
} elseif ($name === ',') {
$statements[strtolower($alias)] = [
'fqcn' => $groupRoot . $class,
'isExplicitAlias' => $explicitAlias,
];
$statements[strtolower($alias)] = $groupRoot . $class;
$class = $alias = '';
$explicitAlias = false;
} elseif ($name === ';') {
if ($alias !== '') {
$statements[strtolower($alias)] = [
'fqcn' => $groupRoot . $class,
'isExplicitAlias' => $explicitAlias,
];
$statements[strtolower($alias)] = $groupRoot . $class;
}
break;
} elseif ($name === '{') {
Expand All @@ -106,7 +100,6 @@ private function parseUseStatement(): array
}
}

/** @var array<non-empty-string, array{fqcn: non-empty-string, isExplicitAlias: bool}> */
return $statements;
}

Expand Down
Loading

0 comments on commit ae0f5bd

Please sign in to comment.