mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-17 16:42:25 +00:00
add UseWithAliasRector init
This commit is contained in:
parent
dd8aa9c546
commit
87d8799a9e
31
src/Rector/Contrib/PhpParser/UseWithAliasRector.php
Normal file
31
src/Rector/Contrib/PhpParser/UseWithAliasRector.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\PhpParser;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Covers https://github.com/nikic/PHP-Parser/commit/3da189769cfa19dabd890b85e1a4bfe63cfcc7fb
|
||||
*/
|
||||
final class UseWithAliasRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof UseUse) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dump($node);
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $methodCallNode
|
||||
*/
|
||||
public function refactor(Node $methodCallNode): ?Node
|
||||
{
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ rectors:
|
|||
Rector\Rector\Contrib\PhpParser\CatchAndClosureUseNameRector: ~
|
||||
Rector\Rector\Contrib\PhpParser\SetLineRector: ~
|
||||
Rector\Rector\Contrib\PhpParser\RemoveNodeRector: ~
|
||||
Rector\Rector\Contrib\PhpParser\UseWithAliasRector: ~
|
||||
|
||||
Rector\Rector\Dynamic\PropertyNameReplacerRector:
|
||||
'PhpParser\Node\Stmt\Class_':
|
||||
|
|
|
@ -11,5 +11,8 @@ final class CommandNamingTest extends TestCase
|
|||
{
|
||||
$name = CommandNaming::classToName('SomeNameCommand');
|
||||
$this->assertSame('some-name', $name);
|
||||
|
||||
$name = CommandNaming::classToName('AlsoNamespace\SomeNameCommand');
|
||||
$this->assertSame('some-name', $name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
class MyAbstractVisitor extends \PhpParser\NodeVisitorAbstract
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class SomeVisitor extends MyAbstractVisitor
|
||||
{
|
||||
public function leaveNode(\PhpParser\Node $node)
|
||||
{
|
||||
return \PhpParser\NodeTraverser::REMOVE_NODE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
class MyAbstractVisitor extends \PhpParser\NodeVisitorAbstract
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class SomeVisitor extends MyAbstractVisitor
|
||||
{
|
||||
public function leaveNode(\PhpParser\Node $node)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
25
tests/Rector/Contrib/PhpParser/UseWithAliasRector/Test.php
Normal file
25
tests/Rector/Contrib/PhpParser/UseWithAliasRector/Test.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\PhpParser\UseWithAliasRector;
|
||||
|
||||
use Rector\Rector\Contrib\PhpParser\UseWithAliasRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [UseWithAliasRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
namespace Rector\BetterReflection\TypesFinder\PhpDocumentor;
|
||||
|
||||
use PhpParser\Node\Stmt\GroupUse;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
|
||||
class NamespaceNodeToReflectionTypeContext
|
||||
{
|
||||
/**
|
||||
* @return string[] indexed by alias
|
||||
*/
|
||||
private function aliasesToFullyQualifiedNames(Namespace_ $namespace) : array
|
||||
{
|
||||
// flatten(flatten(map(stuff)))
|
||||
return \array_merge([], ...\array_merge([], ...\array_map(function ($use) : array {
|
||||
/** @var $use Use_|GroupUse */
|
||||
|
||||
return \array_map(function (UseUse $useUse) use ($use) : array {
|
||||
if ($use instanceof GroupUse) {
|
||||
return [$useUse->alias => $use->prefix->toString() . '\\' . $useUse->name->toString()];
|
||||
}
|
||||
|
||||
return [$useUse->alias => $useUse->name->toString()];
|
||||
}, $use->uses);
|
||||
}, $this->classAlikeUses($namespace))));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
namespace Rector\BetterReflection\TypesFinder\PhpDocumentor;
|
||||
|
||||
use PhpParser\Node\Stmt\GroupUse;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
|
||||
class NamespaceNodeToReflectionTypeContext
|
||||
{
|
||||
/**
|
||||
* @return string[] indexed by alias
|
||||
*/
|
||||
private function aliasesToFullyQualifiedNames(Namespace_ $namespace) : array
|
||||
{
|
||||
// flatten(flatten(map(stuff)))
|
||||
return \array_merge([], ...\array_merge([], ...\array_map(function ($use) : array {
|
||||
/** @var $use Use_|GroupUse */
|
||||
|
||||
return \array_map(function (UseUse $useUse) use ($use) : array {
|
||||
if ($use instanceof GroupUse) {
|
||||
return [$useUse->alias => $use->prefix->toString() . '\\' . $useUse->name->toString()];
|
||||
}
|
||||
|
||||
return [$useUse->alias => $useUse->name->toString()];
|
||||
}, $use->uses);
|
||||
}, $this->classAlikeUses($namespace))));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user