Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Support root-namespace parent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Feb 5, 2024
1 parent e60a9f5 commit ffd6931
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
$reflection = new \ReflectionClass($className);

Check failure on line 46 in src/NodeVisitor/ParentClassNameCollectingNodeVisitor.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $objectOrClass of class ReflectionClass constructor expects class-string<T of object>|T of object, string given.
if ($reflection->isInternal()) {
continue;
}
$namespacedClassNames[] = $className;
} catch (\ReflectionException $e) {
$namespacedClassNames[] = $className;
}
}

// remove obviously vendor names
$namespacedClassNames = array_filter($namespacedClassNames, function (string $className): bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\Finalize\Tests\ParentClassResolver\Fixture;

final class SomeClassWithRootNamespaceParent extends \SomeUnknownRootNamespaceClass
{
}
5 changes: 4 additions & 1 deletion tests/ParentClassResolver/ParentClassResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public function test(): void
$parentClassNames = $this->parentClassResolver->resolve($phpFileInfos, function () {
});

$this->assertSame([SomeParentClass::class], $parentClassNames);
$this->assertSame([
\SomeUnknownRootNamespaceClass::class,

Check failure on line 33 in tests/ParentClassResolver/ParentClassResolverTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class SomeUnknownRootNamespaceClass not found.
SomeParentClass::class
], $parentClassNames);
}
}

0 comments on commit ffd6931

Please sign in to comment.