-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add RemoveUselessReadOnlyTagRector (#5790)
* [DeadCode] Add RemoveUselessReadOnlyDocRector * roll * final touch: rename to RemoveUselessReadOnlyTagRector * final touch: doc
- Loading branch information
1 parent
717e3e0
commit ef333de
Showing
10 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...s/DeadCode/Rector/Property/RemoveUselessReadOnlyTagRector/Fixture/remove_on_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
final readonly class RemoveOnClass | ||
{ | ||
public function __construct( | ||
private string $name | ||
) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
final readonly class RemoveOnClass | ||
{ | ||
public function __construct( | ||
private string $name | ||
) | ||
{ | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
.../Rector/Property/RemoveUselessReadOnlyTagRector/Fixture/remove_on_param_construct.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
final class RemoveOnParamConstruct | ||
{ | ||
public function __construct( | ||
/** | ||
* @readonly | ||
*/ | ||
private readonly string $name | ||
) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
final class RemoveOnParamConstruct | ||
{ | ||
public function __construct( | ||
private readonly string $name | ||
) | ||
{ | ||
} | ||
} | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
...eadCode/Rector/Property/RemoveUselessReadOnlyTagRector/Fixture/remove_on_property.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
final class RemoveOnProperty | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private readonly string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
final class RemoveOnProperty | ||
{ | ||
private readonly string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...dCode/Rector/Property/RemoveUselessReadOnlyTagRector/Fixture/skip_no_readonly_doc.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
class SkipNoReadonlyDoc | ||
{ | ||
private readonly string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Code/Rector/Property/RemoveUselessReadOnlyTagRector/Fixture/skip_with_description.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\Fixture; | ||
|
||
class SkipHasDescription | ||
{ | ||
/** | ||
* @readonly some desc | ||
*/ | ||
private readonly string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ode/Rector/Property/RemoveUselessReadOnlyTagRector/RemoveUselessReadOnlyTagRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveUselessReadOnlyTagRectorTest 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'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-tests/DeadCode/Rector/Property/RemoveUselessReadOnlyTagRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([RemoveUselessReadOnlyTagRector::class]); |
108 changes: 108 additions & 0 deletions
108
rules/DeadCode/Rector/Property/RemoveUselessReadOnlyTagRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\Property; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
use Rector\Privatization\NodeManipulator\VisibilityManipulator; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\RemoveUselessReadOnlyTagRectorTest | ||
*/ | ||
final class RemoveUselessReadOnlyTagRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly VisibilityManipulator $visibilityManipulator, | ||
private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
private readonly DocBlockUpdater $docBlockUpdater | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove useless @readonly annotation on native readonly type', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private readonly string $name; | ||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
private readonly string $name; | ||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class, Property::class, Param::class]; | ||
} | ||
|
||
/** | ||
* @param Class_|Property|Param $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// for param, only on property promotion | ||
if ($node instanceof Param && $node->flags === 0) { | ||
return null; | ||
} | ||
|
||
if (! $this->visibilityManipulator->isReadonly($node)) { | ||
return null; | ||
} | ||
|
||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
$readonlyDoc = $phpDocInfo->getByName('readonly'); | ||
if (! $readonlyDoc instanceof PhpDocTagNode) { | ||
return null; | ||
} | ||
|
||
if (! $readonlyDoc->value instanceof GenericTagValueNode) { | ||
return null; | ||
} | ||
|
||
if ($readonlyDoc->value->value !== '') { | ||
return null; | ||
} | ||
|
||
$phpDocInfo->removeByName('readonly'); | ||
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
|
||
return $node; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters