-
-
Notifications
You must be signed in to change notification settings - Fork 431
FinalPropertyPromotionRector #7231
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
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cecef08
FinalPropertyPromotionRector
062bbcd
FinalPropertyPromotionRector
cd40ec7
FinalPropertyPromotionRector
214ba02
FinalPropertyPromotionRector
f521838
FinalPropertyPromotionRector
942b6a8
FinalPropertyPromotionRector
0fd25a3
FinalPropertyPromotionRector
e03200e
FinalPropertyPromotionRector
d562db6
FinalPropertyPromotionRector
71931fc
FinalPropertyPromotionRector
0dff280
FinalPropertyPromotionRector
bdc8376
FinalPropertyPromotionRector
be186eb
FinalPropertyPromotionRector
aa1cd9e
FinalPropertyPromotionRector
78fb697
FinalPropertyPromotionRector Check The Promotion
b053cf7
FinalPropertyPromotionRector Check The Promotion
964ea93
FinalPropertyPromotionRector.php
arshidkv12 26d8d58
Update FinalPropertyPromotionRector.php
arshidkv12 1834fbf
Update FinalPropertyPromotionRector.php
arshidkv12 a68383b
Update FinalPropertyPromotionRector.php
arshidkv12 1d7f092
Update rules/Php85/Rector/Class_/FinalPropertyPromotionRector.php
arshidkv12 8d837c8
Update rules/Php85/Rector/Class_/FinalPropertyPromotionRector.php
arshidkv12 7407a58
FinalPropertyPromotionRector
57ba7f3
FinalPropertyPromotionRector
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
...sts/Php85/Rector/Class_/FinalPropertyPromotionRector/FinalPropertyPromotionRectorTest.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\Php85\Rector\Class_\FinalPropertyPromotionRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class FinalPropertyPromotionRectorTest 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
.../Php85/Rector/Class_/FinalPropertyPromotionRector/Fixture/child_from_parent_class.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\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| use Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source\ParentClass; | ||
|
|
||
| class ChildExtendsParent extends ParentClass | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $idd | ||
| ){} | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| use Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source\ParentClass; | ||
|
|
||
| class ChildExtendsParent extends ParentClass | ||
| { | ||
| public function __construct( | ||
| final public string $idd | ||
| ){} | ||
| } | ||
| ?> |
30 changes: 30 additions & 0 deletions
30
rules-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/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,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| final public string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> |
15 changes: 15 additions & 0 deletions
15
rules-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/Fixture/non_typed_args.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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> |
30 changes: 30 additions & 0 deletions
30
rules-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/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,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| public readonly string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| final public readonly string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> |
14 changes: 14 additions & 0 deletions
14
...Php85/Rector/Class_/FinalPropertyPromotionRector/Fixture/skip_already_final_param.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,14 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| final public string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> |
12 changes: 12 additions & 0 deletions
12
...sts/Php85/Rector/Class_/FinalPropertyPromotionRector/Fixture/skip_anonymous_class.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 | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| $obj = new class { | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $id | ||
| ){} | ||
| }; | ||
| ?> |
15 changes: 15 additions & 0 deletions
15
...s-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/Fixture/skip_final_class.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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Fixture; | ||
|
|
||
| final class FinalPropertyPromotion | ||
| { | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $id | ||
| ){} | ||
| } | ||
|
|
||
| ?> |
7 changes: 7 additions & 0 deletions
7
rules-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/Source/ParentClass.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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\Source; | ||
|
|
||
| class ParentClass | ||
| { | ||
| } |
11 changes: 11 additions & 0 deletions
11
rules-tests/Php85/Rector/Class_/FinalPropertyPromotionRector/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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Php85\Rector\Class_\FinalPropertyPromotionRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(FinalPropertyPromotionRector::class); | ||
|
|
||
| }; |
120 changes: 120 additions & 0 deletions
120
rules/Php85/Rector/Class_/FinalPropertyPromotionRector.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,120 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Php85\Rector\Class_; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Param; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| 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\PhpVersionFeature; | ||
| use Rector\ValueObject\Visibility; | ||
| use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see https://wiki.php.net/rfc/final_promotion | ||
| * @see \Rector\Tests\Php85\Rector\Class_\FinalPropertyPromotionRector\FinalPropertyPromotionRectorTest | ||
| */ | ||
| final class FinalPropertyPromotionRector extends AbstractRector implements MinPhpVersionInterface | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private const TAGNAME = 'final'; | ||
|
|
||
| public function __construct( | ||
| private VisibilityManipulator $visibilityManipulator, | ||
| private readonly DocBlockUpdater $docBlockUpdater, | ||
| private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Add native final promoted properties in non-final class to avoid child to override the promoted properties based on `@final` tag', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| public function __construct( | ||
| /** @final */ | ||
| public string $id | ||
| ) {} | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| public function __construct( | ||
| final public string $id | ||
| ) {} | ||
| CODE_SAMPLE | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Class_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Class_ $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($node->isFinal()) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->isAnonymous()) { | ||
| return null; | ||
| } | ||
|
|
||
| $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); | ||
|
|
||
| if (! $constructClassMethod instanceof ClassMethod) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
| $params = $constructClassMethod->getParams(); | ||
|
|
||
| foreach ($params as $param) { | ||
| if (! $param->isPromoted()) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->visibilityManipulator->hasVisibility($param, Visibility::FINAL)) { | ||
| continue; | ||
| } | ||
|
|
||
| $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param); | ||
|
|
||
| if (! $phpDocInfo->hasByName(self::TAGNAME)) { | ||
| continue; | ||
| } | ||
| $hasChanged = true; | ||
| $this->visibilityManipulator->makeFinal($param); | ||
| $phpDocInfo->removeByName(self::TAGNAME); | ||
| $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($param); | ||
| } | ||
|
|
||
| if($hasChanged){ | ||
| return $node; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public function provideMinPhpVersion(): int | ||
| { | ||
| return PhpVersionFeature::FINAL_PROPERTY_PROMOTION; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.