diff --git a/config/set/php85.php b/config/set/php85.php index 81d2598ad6b..3bd05b90193 100644 --- a/config/set/php85.php +++ b/config/set/php85.php @@ -9,6 +9,7 @@ use Rector\Config\RectorConfig; use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector; use Rector\Php85\Rector\Class_\SleepToSerializeRector; +use Rector\Php85\Rector\Class_\WakeupToUnserializeRector; use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector; use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector; @@ -41,6 +42,7 @@ ChrArgModuloRector::class, SleepToSerializeRector::class, OrdSingleByteRector::class, + WakeupToUnserializeRector::class ] ); diff --git a/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/Fixture/wakeup.php.inc b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/Fixture/wakeup.php.inc new file mode 100644 index 00000000000..c8abafca716 --- /dev/null +++ b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/Fixture/wakeup.php.inc @@ -0,0 +1,33 @@ +id = 1; + } +} +?> +----- + $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } +} +?> diff --git a/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/WakeupToUnserializeRectorTest.php b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/WakeupToUnserializeRectorTest.php new file mode 100644 index 00000000000..e242647f5d3 --- /dev/null +++ b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/WakeupToUnserializeRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/config/configured_rule.php b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/config/configured_rule.php new file mode 100644 index 00000000000..659c7717a7c --- /dev/null +++ b/rules-tests/Php85/Rector/Class_/WakeupToUnserializeRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(WakeupToUnserializeRector::class); +}; diff --git a/rules/Php85/Rector/Class_/WakeupToUnserializeRector.php b/rules/Php85/Rector/Class_/WakeupToUnserializeRector.php new file mode 100644 index 00000000000..87b7dd8256b --- /dev/null +++ b/rules/Php85/Rector/Class_/WakeupToUnserializeRector.php @@ -0,0 +1,131 @@ + $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + 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'); + $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; + } +} diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 0fb9c7daf04..9670d115bf0 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -816,7 +816,13 @@ final class PhpVersionFeature * @var int */ public const DEPRECATED_METHOD_SLEEP = PhpVersion::PHP_85; - + + /** + * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods + * @var int + */ + public const DEPRECATED_METHOD_WAKEUP = PhpVersion::PHP_85; + /** * @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_string_which_are_not_one_byte_long_to_ord * @var int