-
-
Notifications
You must be signed in to change notification settings - Fork 431
[Php8.5] Add WakeupToUnserializeRector #7242
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
7 commits
Select commit
Hold shift + click to select a range
cb55c60
WakeupToUnserializeRector
0b0163e
WakeupToUnserializeRector
65ad358
WakeupToUnserializeRector
c57a516
WakeupToUnserializeRector - auto assign
f773812
WakeupToUnserializeRector - auto assign
5f7702a
Merge branch 'main' into __wakeup
arshidkv12 a57b9ec
WakeupToUnserializeRector - auto assign
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
33 changes: 33 additions & 0 deletions
33
rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/Fixture/wakeup.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,33 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\WakeupToUnserializeRector\Fixture; | ||
|
|
||
| class User { | ||
| private $id; | ||
| private $name; | ||
|
|
||
| public function __wakeup(){ | ||
| $this->id = 1; | ||
| } | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php85\Rector\Class_\WakeupToUnserializeRector\Fixture; | ||
|
|
||
| class User { | ||
| private $id; | ||
| private $name; | ||
|
|
||
| public function __unserialize(array $data): void{ | ||
| foreach ($data as $property => $value) { | ||
| if (property_exists($this, $property)) { | ||
| $this->{$property} = $value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/WakeupToUnserializeRectorTest.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_\WakeupToUnserializeRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class WakeupToUnserializeRectorTest 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'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/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\Php85\Rector\Class_\WakeupToUnserializeRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(WakeupToUnserializeRector::class); | ||
| }; |
131 changes: 131 additions & 0 deletions
131
rules/Php85/Rector/Class_/WakeupToUnserializeRector.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,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Php85\Rector\Class_; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Assign; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Param; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use PhpParser\Node\Stmt\Foreach_; | ||
| use PhpParser\Node\Stmt\If_; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\ValueObject\PhpVersionFeature; | ||
| use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see https://3v4l.org/NS419 | ||
| * @see https://3v4l.org/nMOpl | ||
| * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods | ||
| * @see \Rector\Tests\Php85\Rector\Class_\WakeupToUnserializeRector\WakeupToUnserializeRectorTest | ||
| */ | ||
| final class WakeupToUnserializeRector extends AbstractRector implements MinPhpVersionInterface | ||
| { | ||
| public function provideMinPhpVersion(): int | ||
| { | ||
| return PhpVersionFeature::DEPRECATED_METHOD_WAKEUP; | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Change __wakeup() to __unserialize()', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class User { | ||
| public function __wakeup() { | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class User { | ||
| public function __unserialize(array $data): void{ | ||
| foreach ($data as $property => $value) { | ||
| if (property_exists($this, $property)) { | ||
| $this->{$property} = $value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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->getMethod('__unserialize') instanceof ClassMethod) { | ||
| return null; | ||
| } | ||
|
|
||
| $classMethod = $node->getMethod('__wakeup'); | ||
| if (! $classMethod instanceof ClassMethod) { | ||
| return null; | ||
| } | ||
|
|
||
| $classMethod->name = new Identifier('__unserialize'); | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $classMethod->returnType = new Identifier('void'); | ||
| $param = new Param( | ||
| var: new Variable('data'), | ||
| type: new Identifier('array') | ||
| ); | ||
|
|
||
| $classMethod->params[] = $param; | ||
|
|
||
| $classMethod->stmts = [$this->assignProperties()]; | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| protected function assignProperties(): Foreach_{ | ||
| $assign = new Assign( | ||
| new PropertyFetch(new Variable('this'), new Variable('property')), | ||
| new Variable('value') | ||
| ); | ||
|
|
||
| $if = new If_( | ||
| new FuncCall(new Name('property_exists'), [ | ||
| new Node\Arg(new Variable('this')), | ||
| new Node\Arg(new Variable('property')), | ||
| ]), | ||
| [ | ||
| 'stmts' => [new Expression($assign)], | ||
| ] | ||
| ); | ||
|
|
||
| $foreach = new Foreach_( | ||
| new Variable('data'), | ||
| new Variable('value'), | ||
| [ | ||
| 'keyVar' => new Variable('property'), | ||
| 'stmts' => [$if], | ||
| ] | ||
| ); | ||
|
|
||
| return $foreach; | ||
| } | ||
| } | ||
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.