Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@
ChrArgModuloRector::class,
SleepToSerializeRector::class,
OrdSingleByteRector::class,
WakeupToUnserializeRector::class
]
);

Expand Down
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;
}
}
}
}
?>
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';
}
}
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 rules/Php85/Rector/Class_/WakeupToUnserializeRector.php
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');
$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;
}
}
8 changes: 7 additions & 1 deletion src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading