Skip to content

Commit

Permalink
[PostRector] Add support for no-namespaced code on UnusedImportRemovi…
Browse files Browse the repository at this point in the history
…ngPostRector (#3476)

* [PostRector] Add support for no-namespaced code on UnusedImportRemovingPostRector

* implemented 🎉

* rename fixture
  • Loading branch information
samsonasik committed Mar 12, 2023
1 parent c077c9d commit b66c8d3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\NodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Configuration\RectorConfigProvider;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -33,7 +34,7 @@ public function enterNode(Node $node): ?Node
return null;
}

if (! $node instanceof Namespace_) {
if (! $node instanceof Namespace_ && ! $node instanceof FileWithoutNamespace) {
return null;
}

Expand Down Expand Up @@ -113,7 +114,7 @@ class SomeClass
/**
* @return string[]
*/
private function findNonUseImportNames(Namespace_ $namespace): array
private function findNonUseImportNames(Namespace_|FileWithoutNamespace $namespace): array
{
$names = [];

Expand Down Expand Up @@ -148,7 +149,7 @@ private function findNonUseImportNames(Namespace_ $namespace): array
/**
* @return string[]
*/
private function findNamesInDocBlocks(Namespace_ $namespace): array
private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace): array
{
$names = [];

Expand All @@ -169,7 +170,7 @@ private function findNamesInDocBlocks(Namespace_ $namespace): array
/**
* @return string[]
*/
private function resolveUsedPhpAndDocNames(Namespace_ $namespace): array
private function resolveUsedPhpAndDocNames(Namespace_|FileWithoutNamespace $namespace): array
{
$phpNames = $this->findNonUseImportNames($namespace);
$docBlockNames = $this->findNamesInDocBlocks($namespace);
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/NoNamespaced/Fixture/no_namespaced_class.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;

class NoNamespacedClass
{
public function run(): String_
{
return new String_('test');
}
}

?>
-----
<?php

use PhpParser\Node\Scalar\String_;

class NoNamespacedClass
{
public function run(): String_
{
return new String_('test');
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/NoNamespaced/NoNamespacedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\NoNamespaced;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NoNamespacedTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
9 changes: 9 additions & 0 deletions tests/Issues/NoNamespaced/Source/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\NoNamespaced\Source;

class SomeClass
{
}
9 changes: 9 additions & 0 deletions tests/Issues/NoNamespaced/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->removeUnusedImports();
};

0 comments on commit b66c8d3

Please sign in to comment.