-
-
Notifications
You must be signed in to change notification settings - Fork 414
Add rector to migrate __sleep() to __serialize() #7212
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 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cd475eb
Add rector to migrate __sleep() to __serialize()
631c7da
Add rector to migrate __sleep() to __serialize()
9ec79ce
Sleep method
3add97c
Add rector to migrate __sleep() to __serialize()
02052bb
Add rector to migrate __sleep() to __serialize()
e4a6dbc
Merge branch 'main' into __sleep__
arshidkv12 0983a20
SleepToSerializeRector
f9e732a
SleepToSerializeRector_Class
2344caa
SleepToSerializeRector_Class
1e10bdb
SleepToSerializeRector_Class
c35548e
SleepToSerializeRector_Class
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
29 changes: 29 additions & 0 deletions
29
rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/Fixture/sleep.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,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
||
class User { | ||
private $id; | ||
private $name; | ||
|
||
public function __sleep() { | ||
return ['id', 'name']; | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture; | ||
|
||
class User { | ||
private $id; | ||
private $name; | ||
|
||
public function __serialize(): array { | ||
return ['id' => $this->id, 'name' => $this->name]; | ||
} | ||
} | ||
?> |
28 changes: 28 additions & 0 deletions
28
rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/SleepToSerializeRectorTest.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\MethodCall\SleepToSerializeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SleepToSerializeRectorTest 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/MethodCall/SleepToSerializeRector/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\ClassMethod\SleepToSerializeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SleepToSerializeRector::class); | ||
}; |
108 changes: 108 additions & 0 deletions
108
rules/Php85/Rector/ClassMethod/SleepToSerializeRector.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,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php85\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
|
||
/** | ||
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods | ||
* @see \Rector\Tests\Php85\Rector\MethodCall\SleepToSerializeRector\SleepToSerializeRectorTest | ||
*/ | ||
final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATED_METHOD_SLEEP; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change __sleep() to __serialize() with correct return values', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class User { | ||
private $id; | ||
private $name; | ||
|
||
public function __sleep() { | ||
return ['id', 'name']; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class User { | ||
private $id; | ||
private $name; | ||
|
||
public function __serialize(): array { | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
]; | ||
} | ||
} | ||
CODE_SAMPLE | ||
) | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node->name, '__sleep')) { | ||
return null; | ||
} | ||
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$node->name = new Identifier('__serialize'); | ||
$node->returnType = new Identifier('array'); | ||
|
||
if(!is_array($node->stmts)){ | ||
return null; | ||
} | ||
|
||
foreach ($node->stmts as $stmt) { | ||
if ($stmt instanceof Node\Stmt\Return_ && $stmt->expr instanceof Array_) { | ||
$newItems = []; | ||
foreach ($stmt->expr->items as $item) { | ||
if ($item !== null && $item->value instanceof Node\Scalar\String_) { | ||
$propName = $item->value->value; | ||
$newItems[] = new ArrayItem( | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new PropertyFetch(new Node\Expr\Variable('this'), $propName), | ||
$item->value | ||
); | ||
} | ||
} | ||
$stmt->expr->items = $newItems; | ||
} | ||
} | ||
samsonasik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return $node; | ||
} | ||
} |
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.