Skip to content

Commit

Permalink
Merge pull request #6335 from boesing/bugfix/recursive-class-aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Aug 31, 2021
2 parents b713140 + c9aebe3 commit eb973ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,7 @@ public function hasFullyQualifiedClassName(
?string $calling_fq_class_name = null,
?string $calling_method_id = null
): bool {
$fq_class_name_lc = strtolower($fq_class_name);

if (isset($this->classlike_aliases[$fq_class_name_lc])) {
$fq_class_name_lc = strtolower($this->classlike_aliases[$fq_class_name_lc]);
}
$fq_class_name_lc = strtolower($this->getUnAliasedName($fq_class_name));

if ($code_location) {
if ($calling_method_id) {
Expand Down Expand Up @@ -392,11 +388,7 @@ public function hasFullyQualifiedInterfaceName(
?string $calling_fq_class_name = null,
?string $calling_method_id = null
): bool {
$fq_class_name_lc = strtolower($fq_class_name);

if (isset($this->classlike_aliases[$fq_class_name_lc])) {
$fq_class_name_lc = strtolower($this->classlike_aliases[$fq_class_name_lc]);
}
$fq_class_name_lc = strtolower($this->getUnAliasedName($fq_class_name));

if (!isset($this->existing_interfaces_lc[$fq_class_name_lc])
|| !$this->existing_interfaces_lc[$fq_class_name_lc]
Expand Down Expand Up @@ -463,11 +455,7 @@ public function hasFullyQualifiedEnumName(
?string $calling_fq_class_name = null,
?string $calling_method_id = null
): bool {
$fq_class_name_lc = strtolower($fq_class_name);

if (isset($this->classlike_aliases[$fq_class_name_lc])) {
$fq_class_name_lc = strtolower($this->classlike_aliases[$fq_class_name_lc]);
}
$fq_class_name_lc = strtolower($this->getUnAliasedName($fq_class_name));

if (!isset($this->existing_enums_lc[$fq_class_name_lc])
|| !$this->existing_enums_lc[$fq_class_name_lc]
Expand Down Expand Up @@ -530,11 +518,7 @@ public function hasFullyQualifiedEnumName(

public function hasFullyQualifiedTraitName(string $fq_class_name, ?CodeLocation $code_location = null): bool
{
$fq_class_name_lc = strtolower($fq_class_name);

if (isset($this->classlike_aliases[$fq_class_name_lc])) {
$fq_class_name_lc = strtolower($this->classlike_aliases[$fq_class_name_lc]);
}
$fq_class_name_lc = strtolower($this->getUnAliasedName($fq_class_name));

if (!isset($this->existing_traits_lc[$fq_class_name_lc]) ||
!$this->existing_traits_lc[$fq_class_name_lc]
Expand Down Expand Up @@ -622,15 +606,14 @@ public function classExists(
*/
public function classExtends(string $fq_class_name, string $possible_parent, bool $from_api = false): bool
{
$fq_class_name_lc = strtolower($fq_class_name);
$unaliased_fq_class_name = $this->getUnAliasedName($fq_class_name);
$unaliased_fq_class_name_lc = strtolower($unaliased_fq_class_name);

if ($fq_class_name_lc === 'generator') {
if ($unaliased_fq_class_name_lc === 'generator') {
return false;
}

$fq_class_name = $this->classlike_aliases[$fq_class_name_lc] ?? $fq_class_name;

$class_storage = $this->classlike_storage_provider->get($fq_class_name_lc);
$class_storage = $this->classlike_storage_provider->get($unaliased_fq_class_name);

if ($from_api && !$class_storage->populated) {
throw new UnpopulatedClasslikeException($fq_class_name);
Expand Down Expand Up @@ -666,9 +649,7 @@ public function classImplements(string $fq_class_name, string $interface): bool
return false;
}

if (isset($this->classlike_aliases[$fq_class_name])) {
$fq_class_name = $this->classlike_aliases[$fq_class_name];
}
$fq_class_name = $this->getUnAliasedName($fq_class_name);

if (!$this->classlike_storage_provider->has($fq_class_name)) {
return false;
Expand Down Expand Up @@ -852,7 +833,12 @@ public function getUnAliasedName(string $alias_name): string
return $alias_name;
}

return $this->classlike_aliases[$alias_name_lc] ?? $alias_name;
$result = $this->classlike_aliases[$alias_name_lc] ?? $alias_name;
if ($result === $alias_name) {
return $result;
}

return $this->getUnAliasedName($result);
}

public function consolidateAnalyzedData(Methods $methods, ?Progress $progress, bool $find_unused_code): void
Expand Down
9 changes: 9 additions & 0 deletions tests/Internal/Codebase/ClassLikesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ public function testWillDetectClassImplementingAliasedInterface(): void

self::assertTrue($this->classlikes->classImplements('Baz', 'Foo'));
}

public function testWillResolveAliasedAliases(): void
{
$this->classlikes->addClassAlias('Foo', 'bar');
$this->classlikes->addClassAlias('Bar', 'baz');
$this->classlikes->addClassAlias('Baz', 'qoo');

self::assertSame('Foo', $this->classlikes->getUnAliasedName('Qoo'));
}
}

0 comments on commit eb973ab

Please sign in to comment.