Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodingStyle] Add RemoveUselessAliasInUseStatementRector #5394

Merged
merged 7 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 353 Rules Overview
# 354 Rules Overview

<br>

Expand All @@ -8,7 +8,7 @@

- [CodeQuality](#codequality) (73)

- [CodingStyle](#codingstyle) (27)
- [CodingStyle](#codingstyle) (28)

- [DeadCode](#deadcode) (43)

Expand Down Expand Up @@ -1867,6 +1867,19 @@ Remove final from constants in classes defined as final

<br>

### RemoveUselessAliasInUseStatementRector

Remove useless alias in use statement as same name with last use statement name

- class: [`Rector\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector`](../rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php)

```diff
-use App\Bar as Bar;
+use App\Bar;
```

<br>

### SeparateMultiUseImportsRector

Split multi use imports and trait statements to standalone lines
Expand Down Expand Up @@ -4765,13 +4778,13 @@ Change simple property init and assign to constructor promotion
```diff
class SomeClass
{
- public float $someVariable;
- public float $price;
-
public function __construct(
- float $someVariable = 0.0
+ public float $someVariable = 0.0
- float $price = 0.0
+ public float $price = 0.0
) {
- $this->someVariable = $someVariable;
- $this->price = $price;
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions config/set/coding-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\Property\SplitGroupedPropertiesRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector;
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector;
Expand Down Expand Up @@ -71,5 +72,6 @@
SplitGroupedClassConstantsRector::class,
ExplicitPublicClassMethodRector::class,
ArraySpreadInsteadOfArrayMergeRector::class,
RemoveUselessAliasInUseStatementRector::class,
]);
};
1 change: 1 addition & 0 deletions phpstan-for-fixtures.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ parameters:
- 'rules-tests/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector/Fixture/skip_result_object.php.inc'
- 'rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/non_namespaced_class_with_annotation.php.inc'
- 'rules-tests/Removing/Rector/ClassMethod/ArgumentRemoverRector/Fixture/fixture2.php.inc'
- 'rules-tests/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector/Fixture/no_namespace_useless_alias.php.inc'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Doctrine\ORM\Mapping as Mapping;

?>
-----
<?php

use Doctrine\ORM\Mapping;

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

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use stdClass as stdClass;

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use stdClass;

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

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use Doctrine\ORM\Mapping as Mapping;

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use Doctrine\ORM\Mapping;

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

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use stdClass as Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use Doctrine\ORM\Mapping as ORM;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\Fixture;

use stdClass;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveUselessAliasInUseStatementRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveUselessAliasInUseStatementRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\Stmt;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\Stmt\RemoveUselessAliasInUseStatementRector\RemoveUselessAliasInUseStatementRectorTest
*/
final class RemoveUselessAliasInUseStatementRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove useless alias in use statement as same name with last use statement name',
[
new CodeSample(
<<<'CODE_SAMPLE'
use App\Bar as Bar;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use App\Bar;
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FileWithoutNamespace::class, Namespace_::class];
}

/**
* @param FileWithoutNamespace|Namespace_ $node
*/
public function refactor(Node $node): null|FileWithoutNamespace|Namespace_
{
$hasChanged = false;
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Use_) {
continue;
}

if (count($stmt->uses) !== 1) {
continue;
}

if (! isset($stmt->uses[0])) {
continue;
}

$aliasName = $stmt->uses[0]->alias instanceof Identifier
? $stmt->uses[0]->alias->toString()
: null;

if ($aliasName === null) {
continue;
}

$useName = $stmt->uses[0]->name->toString();
$lastName = Strings::after($useName, '\\', -1) ?? $useName;
if ($lastName === $aliasName) {
$stmt->uses[0]->alias = null;
$hasChanged = true;
}
}

if ($hasChanged) {
return $node;
}

return null;
}
}