-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 1 commit
a268587
302dc93
b4564da
783ff15
008d166
0c69706
3fa5dc7
d6d16d2
9ea5484
61d92bb
44e5abc
636afa3
2aa0ac9
70fd6f9
679f36f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?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 | ||
| ) {} | ||
| } | ||
|
|
||
| ?> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\DowngradeFinalPropertyPromotionRector\Rector\Class_\DowngradePropertyPromotionRector\Fixture; | ||
|
|
||
| class Fixture | ||
| { | ||
| public function __construct(readonly string $id) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| 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); | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| declare(strict_types=1); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| namespace Rector\DowngradePhp85\Rector\Class_; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| use PhpParser\Comment\Doc; | ||||||||||||||||||||||||||
| use PhpParser\Modifiers; | ||||||||||||||||||||||||||
| use PhpParser\Node; | ||||||||||||||||||||||||||
| use PhpParser\Node\Param; | ||||||||||||||||||||||||||
| use PhpParser\Node\Stmt\Class_; | ||||||||||||||||||||||||||
| use PhpParser\Node\Stmt\ClassMethod; | ||||||||||||||||||||||||||
| use PhpParser\Node\Stmt\Trait_; | ||||||||||||||||||||||||||
| use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||||||||||||||||||||||||||
| use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; | ||||||||||||||||||||||||||
| use Rector\PhpParser\Node\BetterNodeFinder; | ||||||||||||||||||||||||||
| use Rector\PhpParser\Printer\BetterStandardPrinter; | ||||||||||||||||||||||||||
| use Rector\Rector\AbstractRector; | ||||||||||||||||||||||||||
| use Rector\ValueObject\MethodName; | ||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| 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 [Class_::class, Trait_::class]; | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * @param Class_|Trait_ $node | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public function refactor(Node $node): null | ||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| $constructorClassMethod = $node->getMethod(MethodName::CONSTRUCT); | ||||||||||||||||||||||||||
| if (! $constructorClassMethod instanceof ClassMethod) { | ||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
|
||||||||||||||||||||||||||
| foreach ($constructorClassMethod->params as $promotedParam) { | ||||||||||||||||||||||||||
| if (($promotedParam->flags & Modifiers::FINAL) !== 0) { | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use VisibilityManipulator->hasVisibility() as well
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if(!$this->visibilityManipulator->hasVisibility($param, Visibility::PUBLIC)){
continue;
}Correct ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Visibility::FINAL |
||||||||||||||||||||||||||
| $promotedParam->flags &= ~Modifiers::FINAL; | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use VisibilityManipulator->makeNonFinal() usage, see
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should add Param as well on rector-src then |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| $existingDoc = $promotedParam->getDocComment(); | ||||||||||||||||||||||||||
| if (! $existingDoc) { | ||||||||||||||||||||||||||
| $promotedParam->setDocComment(new Doc('/** @final */')); | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use docblockUpdater instead, ensure existing doc when exists not removed, see example rector-downgrade-php/rules/DowngradePhp81/Rector/Property/DowngradeReadonlyPropertyRector.php Lines 131 to 142 in 8ab1dce
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
|
||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||
|
samsonasik marked this conversation as resolved.
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
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.