Skip to content

Commit 3add97c

Browse files
ArshidArshid
authored andcommitted
Add rector to migrate __sleep() to __serialize()
1 parent 9ec79ce commit 3add97c

3 files changed

Lines changed: 30 additions & 37 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
5+
6+
class User {
7+
private $id;
8+
private $name;
9+
10+
public function __sleep(): array {
11+
return [];
12+
}
13+
}
14+
?>
15+
?>

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/Fixture/sleep_empty.php.inc

Lines changed: 0 additions & 29 deletions
This file was deleted.

rules/Php85/Rector/ClassMethod/SleepToSerializeRector.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node
8787
return null;
8888
}
8989

90-
if (! $this->isName($node->name, '__sleep')) {
90+
if ($node->returnType instanceof Identifier && $this->isName($node->returnType, 'array')) {
9191
return null;
9292
}
9393

@@ -97,11 +97,13 @@ public function refactor(Node $node): ?Node
9797
}
9898

9999
foreach ($returns as $return) {
100-
if (! $return->expr instanceof Array_) {
100+
if (! $return->expr instanceof Array_ ) {
101101
return null;
102102
}
103-
if (! empty($return->expr->items)) {
104103

104+
if (count($return->expr->items) > 0) {
105+
$newItems = [];
106+
$hasChanged = false;
105107
foreach ($return->expr->items as $item) {
106108
if ($item !== null && $item->value instanceof Node\Scalar\String_) {
107109
$propName = $item->value->value;
@@ -111,14 +113,19 @@ public function refactor(Node $node): ?Node
111113
);
112114
}
113115
}
114-
$return->expr->items = $newItems;
116+
if (count($newItems) > 0) {
117+
$hasChanged = true;
118+
$return->expr->items = $newItems;
119+
}
115120
}
116-
117121
}
118122

119-
$node->name = new Identifier('__serialize');
120-
$node->returnType = new Identifier('array');
123+
if($hasChanged){
124+
$node->name = new Identifier('__serialize');
125+
$node->returnType = new Identifier('array');
126+
return $node;
127+
}
121128

122-
return $node;
129+
return null;
123130
}
124131
}

0 commit comments

Comments
 (0)