Skip to content

Commit

Permalink
[CodingStyle] Handle Nullable Type and under intanceof for RemoveUnus…
Browse files Browse the repository at this point in the history
…edAliasRector (#784)
  • Loading branch information
samsonasik committed Aug 28, 2021
1 parent 541e40b commit dd49d38
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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$#'
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}

?>
34 changes: 34 additions & 0 deletions rules/CodingStyle/Naming/NameRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
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;
Expand Down Expand Up @@ -65,7 +67,39 @@ public function renameNameNode(array $usedNameNodes, string $lastName): void
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
Expand Down

0 comments on commit dd49d38

Please sign in to comment.