-
Notifications
You must be signed in to change notification settings - Fork 24
DowngradeFinalPropertyPromotionRector #317
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a268587
DowngradeFinalPropertyPromotionRector
302dc93
DowngradeFinalPropertyPromotionRector
b4564da
DowngradeFinalPropertyPromotionRector
783ff15
Support Param nodes in makeNonFinal()
008d166
Support Param nodes in makeNonFinal()
0c69706
DowngradeFinalPropertyPromotionRector
3fa5dc7
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik d6d16d2
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 9ea5484
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 61d92bb
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 44e5abc
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 636afa3
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 2aa0ac9
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 70fd6f9
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik 679f36f
Update rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromo…
samsonasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
...lass_/DowngradeFinalPropertyPromotionRector/DowngradeFinalPropertyPromotionRectorTest.php
This file contains hidden or 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\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class DowngradeFinalPropertyPromotionRectorTest 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'; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...owngradePhp85/Rector/Class_/DowngradeFinalPropertyPromotionRector/Fixture/fixture.php.inc
This file contains hidden or 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 | ||
|
|
||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class Fixture | ||
| { | ||
| public function __construct( | ||
| final public string $id | ||
| ) {} | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class Fixture | ||
| { | ||
| public function __construct( | ||
| /** | ||
| * @final | ||
| */ | ||
| public string $id | ||
| ) {} | ||
| } | ||
|
|
||
| ?> |
25 changes: 25 additions & 0 deletions
25
...lass_/DowngradeFinalPropertyPromotionRector/Fixture/implicit_final_property_promotion.php
This file contains hidden or 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,25 @@ | ||
| <?php | ||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class ImplicitFinalPropertyPromotion | ||
| { | ||
| public function __construct(final string $foo) | ||
| { | ||
| } | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class ImplicitFinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| /** | ||
| * @final | ||
| */ | ||
| public string $foo | ||
| ) | ||
| { | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
...Rector/Class_/DowngradeFinalPropertyPromotionRector/Fixture/skip_fixture_readonly.php.inc
This file contains hidden or 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,12 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class SkipFixtureReadonly | ||
| { | ||
| public function __construct(readonly string $id) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
10 changes: 10 additions & 0 deletions
10
...gradePhp85/Rector/Class_/DowngradeFinalPropertyPromotionRector/config/configured_rule.php
This file contains hidden or 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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(DowngradeFinalPropertyPromotionRector::class); | ||
| }; |
121 changes: 121 additions & 0 deletions
121
rules/DowngradePhp85/Rector/Class_/DowngradeFinalPropertyPromotionRector.php
This file contains hidden or 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,121 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\DowngradePhp85\Rector\Class_; | ||
|
|
||
| use PhpParser\Builder\Property; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Param; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| 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 Rector\ValueObject\MethodName; | ||
| use Rector\ValueObject\Visibility; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @changelog https://wiki.php.net/rfc/final_promotion | ||
| * | ||
| * @see \Rector\Tests\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector\DowngradeFinalPropertyPromotionRectorTest | ||
| */ | ||
| final class DowngradeFinalPropertyPromotionRector extends AbstractRector | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private const TAGNAME = 'final'; | ||
|
|
||
| public function __construct( | ||
| private readonly VisibilityManipulator $visibilityManipulator, | ||
| private readonly DocBlockUpdater $docBlockUpdater, | ||
| private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Change constructor final property promotion to @final annotation assign', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function __construct( | ||
| final public string $id | ||
| ){} | ||
| } | ||
| CODE_SAMPLE | ||
|
|
||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $id | ||
| ) {} | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethod $node | ||
| */ | ||
| public function refactor(Node $node): ?ClassMethod | ||
| { | ||
| if (! $this->isName($node, MethodName::CONSTRUCT)) { | ||
| return null; | ||
| } | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $hasChanged = false; | ||
| foreach ($node->params as $param) { | ||
| if (! $param->isPromoted()) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $this->visibilityManipulator->hasVisibility($param, Visibility::FINAL)) { | ||
| continue; | ||
| } | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $hasChanged = true; | ||
| $this->visibilityManipulator->makeNonFinal($param); | ||
|
|
||
| $this->addPhpDocTag($param); | ||
|
|
||
| } | ||
|
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($hasChanged) { | ||
| return $node; | ||
| } | ||
|
|
||
| return null; | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private function addPhpDocTag(Property|Param $node): bool | ||
| { | ||
| $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
|
|
||
| if ($phpDocInfo->hasByName(self::TAGNAME)) { | ||
| return false; | ||
| } | ||
|
|
||
| $phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode(''))); | ||
| $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
| return true; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arshidkv12 oh, this is
.phpfile, and skipped from the scan, should be.php.inc, I was wondering why it was working ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#324