Skip to content

Commit f9e732a

Browse files
ArshidArshid
authored andcommitted
SleepToSerializeRector_Class
1 parent 0983a20 commit f9e732a

6 files changed

Lines changed: 22 additions & 16 deletions

File tree

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/Fixture/skip_sleep_empty.php.inc renamed to rules-tests/Php85/Rector/Class_/SleepToSerializeRector/Fixture/skip_sleep_empty.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
4+
namespace Rector\Tests\Php85\Rector\Class_\NullDebugInfoReturnRector\Fixture;
55

66
class SkipSleepEmpty {
77
private $id;

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/Fixture/skip_sleep_null.php.inc renamed to rules-tests/Php85/Rector/Class_/SleepToSerializeRector/Fixture/skip_sleep_null.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
4+
namespace Rector\Tests\Php85\Rector\Class_\NullDebugInfoReturnRector\Fixture;
55

66
class SkipSleepNull {
77
private $id;

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/Fixture/sleep.php.inc renamed to rules-tests/Php85/Rector/Class_/SleepToSerializeRector/Fixture/sleep.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
4+
namespace Rector\Tests\Php85\Rector\Class_\NullDebugInfoReturnRector\Fixture;
55

66
class User {
77
private $id;
@@ -16,7 +16,7 @@ class User {
1616
<?php
1717
declare(strict_types=1);
1818

19-
namespace Rector\Tests\Php85\Rector\MethodCall\NullDebugInfoReturnRector\Fixture;
19+
namespace Rector\Tests\Php85\Rector\Class_\NullDebugInfoReturnRector\Fixture;
2020

2121
class User {
2222
private $id;

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/SleepToSerializeRectorTest.php renamed to rules-tests/Php85/Rector/Class_/SleepToSerializeRector/SleepToSerializeRectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Tests\Php85\Rector\MethodCall\SleepToSerializeRector;
5+
namespace Rector\Tests\Php85\Rector\Class_\SleepToSerializeRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;

rules-tests/Php85/Rector/MethodCall/SleepToSerializeRector/config/configured_rule.php renamed to rules-tests/Php85/Rector/Class_/SleepToSerializeRector/config/configured_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Php85\Rector\ClassMethod\SleepToSerializeRector;
6+
use Rector\Php85\Rector\Class_\SleepToSerializeRector;
77

88
return static function (RectorConfig $rectorConfig): void {
99
$rectorConfig->rule(SleepToSerializeRector::class);

rules/Php85/Rector/ClassMethod/SleepToSerializeRector.php renamed to rules/Php85/Rector/Class_/SleepToSerializeRector.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Php85\Rector\ClassMethod;
5+
namespace Rector\Php85\Rector\Class_;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Expr\ArrayItem;
1010
use PhpParser\Node\Expr\PropertyFetch;
1111
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\Stmt\Class_;
1213
use PhpParser\Node\Stmt\ClassMethod;
1314
use Rector\PhpParser\Node\BetterNodeFinder;
1415
use Rector\Rector\AbstractRector;
@@ -22,7 +23,7 @@
2223
* @see https://3v4l.org/51uu0
2324
* @see https://3v4l.org/ktJnk
2425
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods
25-
* @see \Rector\Tests\Php85\Rector\MethodCall\SleepToSerializeRector\SleepToSerializeRectorTest
26+
* @see \Rector\Tests\Php85\Rector\Class_\SleepToSerializeRector\SleepToSerializeRectorTest
2627
*/
2728
final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface
2829
{
@@ -77,24 +78,29 @@ public function __serialize(): array {
7778
*/
7879
public function getNodeTypes(): array
7980
{
80-
return [ClassMethod::class];
81+
return [Class_::class];
8182
}
8283

8384
/**
84-
* @param ClassMethod $node
85+
* @param Class_ $node
8586
*/
8687
public function refactor(Node $node): ?Node
8788
{
88-
if (! $this->isName($node->name, '__sleep')) {
89+
if ($node->getMethod('__serialize') instanceof ClassMethod) {
8990
return null;
9091
}
9192

92-
if ($node->returnType instanceof Identifier && $this->isName($node->returnType, 'array')) {
93+
$classMethod = $node->getMethod('__sleep');
94+
if (! $classMethod instanceof ClassMethod) {
9395
return null;
9496
}
9597

96-
$returns = $this->betterNodeFinder->findReturnsScoped($node);
97-
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($node, $returns)) {
98+
if ($classMethod->returnType instanceof Identifier && $this->isName($classMethod->returnType, 'array')) {
99+
return null;
100+
}
101+
102+
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);
103+
if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
98104
return null;
99105
}
100106

@@ -123,8 +129,8 @@ public function refactor(Node $node): ?Node
123129
}
124130

125131
if($hasChanged){
126-
$node->name = new Identifier('__serialize');
127-
$node->returnType = new Identifier('array');
132+
$classMethod->name = new Identifier('__serialize');
133+
$classMethod->returnType = new Identifier('array');
128134
return $node;
129135
}
130136

0 commit comments

Comments
 (0)