From 124becad76fe9709653469e48966b50a0eab512e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 5 Feb 2024 16:21:22 +0100 Subject: [PATCH 1/3] Support root-namespace parent classes --- .../ParentClassNameCollectingNodeVisitor.php | 16 ++++++++++++---- .../Fixture/SomeClassWithInternalParent.php | 9 +++++++++ .../Fixture/SomeClassWithRootNamespaceParent.php | 9 +++++++++ .../ParentClassResolverTest.php | 5 ++++- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/ParentClassResolver/Fixture/SomeClassWithInternalParent.php create mode 100644 tests/ParentClassResolver/Fixture/SomeClassWithRootNamespaceParent.php diff --git a/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php b/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php index 1948f83..ed56605 100644 --- a/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php +++ b/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php @@ -40,10 +40,18 @@ public function getParentClassNames(): array sort($uniqueParentClassNames); // remove native classes - $namespacedClassNames = array_filter( - $uniqueParentClassNames, - fn (string $parentClassName): bool => str_contains($parentClassName, '\\') - ); + $namespacedClassNames = []; + foreach($uniqueParentClassNames as $className) { + try { + $reflectionClass = new \ReflectionClass($className); + if ($reflectionClass->isInternal()) { + continue; + } + $namespacedClassNames[] = $className; + } catch (\ReflectionException $e) { + $namespacedClassNames[] = $className; + } + } // remove obviously vendor names $namespacedClassNames = array_filter($namespacedClassNames, function (string $className): bool { diff --git a/tests/ParentClassResolver/Fixture/SomeClassWithInternalParent.php b/tests/ParentClassResolver/Fixture/SomeClassWithInternalParent.php new file mode 100644 index 0000000..647b9b6 --- /dev/null +++ b/tests/ParentClassResolver/Fixture/SomeClassWithInternalParent.php @@ -0,0 +1,9 @@ +parentClassResolver->resolve($phpFileInfos, function () { }); - $this->assertSame([SomeParentClass::class], $parentClassNames); + $this->assertSame([ + \SomeUnknownRootNamespaceClass::class, + SomeParentClass::class + ], $parentClassNames); } } From d123c7e85d702902487ebc91b3a60a8e86637419 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 5 Feb 2024 16:27:20 +0100 Subject: [PATCH 2/3] Update ParentClassNameCollectingNodeVisitor.php --- src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php b/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php index ed56605..dd06926 100644 --- a/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php +++ b/src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php @@ -43,6 +43,7 @@ public function getParentClassNames(): array $namespacedClassNames = []; foreach($uniqueParentClassNames as $className) { try { + // @phpstan-ignore-next-line $reflectionClass = new \ReflectionClass($className); if ($reflectionClass->isInternal()) { continue; From 84b7d5af414c600da1b152012faffdd69c08f2c8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 5 Feb 2024 16:28:14 +0100 Subject: [PATCH 3/3] Update ParentClassResolverTest.php --- tests/ParentClassResolver/ParentClassResolverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ParentClassResolver/ParentClassResolverTest.php b/tests/ParentClassResolver/ParentClassResolverTest.php index 2757d44..e65918c 100644 --- a/tests/ParentClassResolver/ParentClassResolverTest.php +++ b/tests/ParentClassResolver/ParentClassResolverTest.php @@ -30,7 +30,7 @@ public function test(): void }); $this->assertSame([ - \SomeUnknownRootNamespaceClass::class, + \SomeUnknownRootNamespaceClass::class, // @phpstan-ignore-line SomeParentClass::class ], $parentClassNames); }