2020-05-12 01:09:00 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\Use_;
|
2020-05-12 01:09:00 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-08-04 15:26:33 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Namespace_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\TraitUse;
|
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-05-12 01:09:00 +00:00
|
|
|
/**
|
2021-05-05 23:44:08 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector\SeparateMultiUseImportsRectorTest
|
2020-05-12 01:09:00 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SeparateMultiUseImportsRector extends AbstractRector
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Split multi use imports and trait statements to standalone lines', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-05-12 01:09:00 +00:00
|
|
|
use A, B;
|
|
|
|
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
use SomeTrait, AnotherTrait;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-05-12 01:09:00 +00:00
|
|
|
use A;
|
|
|
|
use B;
|
|
|
|
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
use SomeTrait;
|
|
|
|
use AnotherTrait;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-05-12 01:09:00 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2023-08-04 15:26:33 +00:00
|
|
|
return [FileWithoutNamespace::class, Namespace_::class, Class_::class];
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-08-04 15:26:33 +00:00
|
|
|
* @param FileWithoutNamespace|Namespace_|Class_ $node
|
2024-01-02 02:40:38 +00:00
|
|
|
* @return \Rector\PhpParser\Node\CustomNode\FileWithoutNamespace|\PhpParser\Node\Stmt\Namespace_|\PhpParser\Node\Stmt\Class_|null
|
2020-05-12 01:09:00 +00:00
|
|
|
*/
|
2023-08-04 15:26:33 +00:00
|
|
|
public function refactor(Node $node)
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2023-08-04 15:26:33 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if ($stmt instanceof Use_) {
|
|
|
|
$refactorUseImport = $this->refactorUseImport($stmt);
|
|
|
|
if ($refactorUseImport !== null) {
|
|
|
|
unset($node->stmts[$key]);
|
|
|
|
\array_splice($node->stmts, $key, 0, $refactorUseImport);
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($stmt instanceof TraitUse) {
|
|
|
|
$refactorTraitUse = $this->refactorTraitUse($stmt);
|
|
|
|
if ($refactorTraitUse !== null) {
|
|
|
|
unset($node->stmts[$key]);
|
|
|
|
\array_splice($node->stmts, $key, 0, $refactorTraitUse);
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2023-08-04 15:26:33 +00:00
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $node;
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
/**
|
|
|
|
* @return Use_[]|null $use
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorUseImport(Use_ $use) : ?array
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($use->uses) < 2) {
|
2021-05-06 08:23:37 +00:00
|
|
|
return null;
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
$uses = [];
|
2020-05-12 01:09:00 +00:00
|
|
|
foreach ($use->uses as $singleUse) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$uses[] = new Use_([$singleUse]);
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
return $uses;
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
/**
|
|
|
|
* @return TraitUse[]|null
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorTraitUse(TraitUse $traitUse) : ?array
|
2020-05-12 01:09:00 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($traitUse->traits) < 2) {
|
2021-05-06 08:23:37 +00:00
|
|
|
return null;
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
$traitUses = [];
|
2020-05-12 01:09:00 +00:00
|
|
|
foreach ($traitUse->traits as $singleTraitUse) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$traitUses[] = new TraitUse([$singleTraitUse]);
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
return $traitUses;
|
2020-05-12 01:09:00 +00:00
|
|
|
}
|
|
|
|
}
|