From d4559f5ba37d7c8e0896dddabe016f3adf0bf83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Wed, 18 Aug 2021 16:18:44 +0200 Subject: [PATCH 1/3] qa: add failing test regarding recursive alias resolving MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- tests/Internal/Codebase/ClassLikesTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Internal/Codebase/ClassLikesTest.php b/tests/Internal/Codebase/ClassLikesTest.php index 785a52938..59c72695c 100644 --- a/tests/Internal/Codebase/ClassLikesTest.php +++ b/tests/Internal/Codebase/ClassLikesTest.php @@ -38,4 +38,13 @@ final class ClassLikesTest extends TestCase 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')); + } } From 4e0484f283d37ed590a3bcfce0cef5314c10c7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Wed, 18 Aug 2021 16:18:58 +0200 Subject: [PATCH 2/3] bugfix: resolve recursive aliases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/Psalm/Internal/Codebase/ClassLikes.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index affc067e4..31c325270 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -852,7 +852,12 @@ class ClassLikes 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 From c9aebe3082f8886b36bcb5c64da7b3f4d2f55431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Wed, 18 Aug 2021 16:37:08 +0200 Subject: [PATCH 3/3] bugfix: resolve recursive aliases in multiple methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `ClassLikes#hasFullyQualifiedClassName` - `ClassLikes#hasFullyQualifiedInterfaceName` - `ClassLikes#hasFullyQualifiedEnumName` - `ClassLikes#hasFullyQualifiedTraitName` - `ClassLikes#classExtends` - `ClassLikes#classImplements` Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/Psalm/Internal/Codebase/ClassLikes.php | 37 ++++++---------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index 31c325270..1402be44d 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -321,11 +321,7 @@ class ClassLikes ?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) { @@ -392,11 +388,7 @@ class ClassLikes ?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] @@ -463,11 +455,7 @@ class ClassLikes ?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] @@ -530,11 +518,7 @@ class ClassLikes 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] @@ -622,15 +606,14 @@ class ClassLikes */ 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); @@ -666,9 +649,7 @@ class ClassLikes 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;