[CodingStyle] Handle Nullable Type and under intanceof for RemoveUnusedAliasRector (#784)

This commit is contained in:
Abdul Malik Ikhsan 2021-08-28 19:50:19 +07:00 committed by GitHub
parent 541e40b48f
commit dd49d3874b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 0 deletions

View File

@ -550,3 +550,5 @@ parameters:
message: '#Parameter \#2 \$length of function str_split expects int<1, max\>, int given#'
paths:
- rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php
- '#^Cognitive complexity for "Rector\\CodingStyle\\Naming\\NameRenamer\:\:renameNameNode\(\)" is 12, keep it under 9$#'

View File

@ -0,0 +1,31 @@
<?php
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture;
use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\AbstractKernel as BaseKernel;
class SomeClass
{
public function run(?BaseKernel $baseKernel)
{
$anotherBaseKernel = $baseKernel ?? new BaseKernel();
}
}
?>
-----
<?php
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture;
use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\AbstractKernel;
class SomeClass
{
public function run(?AbstractKernel $baseKernel)
{
$anotherBaseKernel = $baseKernel ?? new AbstractKernel();
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture;
use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\AbstractKernel as BaseKernel;
class UnderInstanceOf
{
public function run(?BaseKernel $baseKernel)
{
if ($baseKernel instanceof BaseKernel) {
}
}
}
?>
-----
<?php
namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture;
use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\AbstractKernel;
class UnderInstanceOf
{
public function run(?AbstractKernel $baseKernel)
{
if ($baseKernel instanceof AbstractKernel) {
}
}
}
?>

View File

@ -5,10 +5,12 @@ declare(strict_types=1);
namespace Rector\CodingStyle\Naming;
use PhpParser\Node;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
@ -65,9 +67,41 @@ final class NameRenamer
if ($parentNode instanceof UnionType) {
$this->renameUnionType($lastName, $parentNode, $usedName);
}
if ($parentNode instanceof NullableType) {
$this->renameNullableType($lastName, $parentNode, $usedName);
}
if ($parentNode instanceof Instanceof_) {
$this->renameInInstanceOf($lastName, $parentNode, $usedName);
}
}
}
private function renameInInstanceOf(
string $lastName,
Instanceof_ $instanceof,
Name | Identifier $usedNameNode
): void {
if (! $this->nodeNameResolver->areNamesEqual($instanceof->class, $usedNameNode)) {
return;
}
$instanceof->class = new Name($lastName);
}
private function renameNullableType(
string $lastName,
NullableType $nullableType,
Name | Identifier $usedNameNode
): void {
if (! $this->nodeNameResolver->areNamesEqual($nullableType->type, $usedNameNode)) {
return;
}
$nullableType->type = new Name($lastName);
}
private function renameTraitUse(string $lastName, TraitUse $traitUse, Name | Identifier $usedNameNode): void
{
foreach ($traitUse->traits as $key => $traitName) {