[PSR-4] Fix declare for NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector (#4332)

This commit is contained in:
Tomas Votruba 2020-09-29 23:16:47 +02:00 committed by GitHub
parent a71d3d2d2b
commit 7a5cf0ed5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\PSR4\Rector\FileSystem;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Autodiscovery\ValueObject\NodesWithFileDestination;
use Rector\Core\RectorDefinition\ComposerJsonAwareCodeSample;
@ -86,12 +87,25 @@ CODE_SAMPLE
return;
}
$nodesWithStrictTypesThenNamespace = [];
foreach ($nodes as $key => $node) {
if ($node instanceof Declare_) {
$nodesWithStrictTypesThenNamespace[] = $node;
unset($nodes[$key]);
}
}
$namespace = new Namespace_(new Name($expectedNamespace));
$namespace->stmts = $nodes;
$nodesWithFileDestination = new NodesWithFileDestination([
$namespace,
], $smartFileInfo->getRealPath(), $smartFileInfo);
$nodesWithStrictTypesThenNamespace[] = $namespace;
$nodesWithFileDestination = new NodesWithFileDestination(
$nodesWithStrictTypesThenNamespace,
$smartFileInfo->getRealPath(),
$smartFileInfo
);
$this->printNodesWithFileDestination($nodesWithFileDestination);
}
}

View File

@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
class NamespaceLessClass
{
}

View File

@ -1,5 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\FileSystem\NormalizeNamespaceByPSR4ComposerAutoloadFileSystemRector\Fixture;
class NamespaceLessClass