Skip to content

Commit

Permalink
[AutoImport] Add mirror comment support on first Use_ on no namespace…
Browse files Browse the repository at this point in the history
… code (#3465)
  • Loading branch information
samsonasik committed Mar 8, 2023
1 parent ed5caee commit 291aab4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 12 deletions.
41 changes: 29 additions & 12 deletions rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ public function __construct(
) {
}

/**
* @param Stmt[] $stmts
* @param Use_[] $newUses
*/
private function mirrorUseComments(array $stmts, array $newUses, int $indexStmt = 0): void
{
if ($stmts === []) {
return;
}

if ($stmts[$indexStmt] instanceof Use_) {
$comments = (array) $stmts[$indexStmt]->getAttribute(AttributeKey::COMMENTS);

if ($comments !== []) {
$newUses[0]->setAttribute(
AttributeKey::COMMENTS,
$stmts[$indexStmt]->getAttribute(AttributeKey::COMMENTS)
);

$stmts[$indexStmt]->setAttribute(AttributeKey::COMMENTS, null);
}
}
}

/**
* @param Stmt[] $stmts
* @param array<FullyQualifiedObjectType|AliasedObjectType> $useImportTypes
Expand Down Expand Up @@ -57,12 +81,16 @@ public function addImportsToStmts(array $stmts, array $useImportTypes, array $fu
$nodesToAdd = array_merge([new Nop()], $newUses);
}

$this->mirrorUseComments($stmts, $newUses, $key + 1);

array_splice($stmts, $key + 1, 0, $nodesToAdd);

return $stmts;
}
}

$this->mirrorUseComments($stmts, $newUses);

// make use stmts first
return array_merge($newUses, $stmts);
}
Expand Down Expand Up @@ -96,18 +124,7 @@ public function addImportsToNamespace(
return;
}

if ($namespace->stmts[0] instanceof Use_) {
$comments = (array) $namespace->stmts[0]->getAttribute(AttributeKey::COMMENTS);

if ($comments !== []) {
$newUses[0]->setAttribute(
AttributeKey::COMMENTS,
$namespace->stmts[0]->getAttribute(AttributeKey::COMMENTS)
);

$namespace->stmts[0]->setAttribute(AttributeKey::COMMENTS, null);
}
}
$this->mirrorUseComments($namespace->stmts, $newUses);

$namespace->stmts = array_merge($newUses, $namespace->stmts);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Issues/AutoImportBeforeDocblock/Fixture/no_namespace.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* License
*/

use Foobar\A;

/**
* Docblock
*/
final class NoNamespace
{
public function run()
{
new A();
new \Foobar\B();
}
}

?>
-----
<?php

/**
* License
*/
use Foobar\B;
use Foobar\A;

/**
* Docblock
*/
final class NoNamespace
{
public function run()
{
new A();
new B();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* License
*/

use Foobar\A;

/**
* Docblock
*/
final class NoNamespaceWithStrictTypes
{
public function run()
{
new A();
new \Foobar\B();
}
}

?>
-----
<?php

declare(strict_types=1);
/**
* License
*/
use Foobar\B;

use Foobar\A;

/**
* Docblock
*/
final class NoNamespaceWithStrictTypes
{
public function run()
{
new A();
new B();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* Docblock
*/
final class NoNamespaceWithStrictTypesNoExistingUse
{
public function run()
{
new A();
new \Foobar\B();
}
}

?>
-----
<?php

declare(strict_types=1);

use Foobar\B;

/**
* Docblock
*/
final class NoNamespaceWithStrictTypesNoExistingUse
{
public function run()
{
new A();
new B();
}
}

?>

0 comments on commit 291aab4

Please sign in to comment.