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/php-version-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
use Rector\Php85\Rector\Property\AddOverrideAttributeToOverriddenPropertiesRector;
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
use Rector\Php86\Rector\FuncCall\MinMaxToClampRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallRector;
Expand Down Expand Up @@ -325,6 +326,7 @@

// PHP 8.6
MinMaxToClampRector::class,
ConstructorReadonlyAssignToDefaultRector::class,
]);

// configured rules, each bound to the PHP version its configuration targets
Expand Down
3 changes: 2 additions & 1 deletion config/set/php86.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
use Rector\Php86\Rector\FuncCall\MinMaxToClampRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([MinMaxToClampRector::class]);
$rectorConfig->rules([MinMaxToClampRector::class, ConstructorReadonlyAssignToDefaultRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ConstructorReadonlyAssignToDefaultRectorTest 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,34 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class ClassConstDefault
{
private const DEFAULT_NAME = 'value';

public readonly string $name;

public function __construct()
{
$this->name = self::DEFAULT_NAME;
}
}

?>
-----
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class ClassConstDefault
{
private const DEFAULT_NAME = 'value';

public readonly string $name = self::DEFAULT_NAME;

public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class Fixture
{
public readonly int $number;

public function __construct()
{
$this->number = 100;
}
}

?>
-----
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class Fixture
{
public readonly int $number = 100;

public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class SkipConditionalAssign
{
public readonly int $number;

public function __construct(bool $flag)
{
if ($flag) {
$this->number = 100;
} else {
$this->number = 200;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class SkipNewObject
{
public readonly \stdClass $value;

public function __construct()
{
$this->value = new \stdClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class SkipNotReadonly
{
public int $number;

public function __construct()
{
$this->number = 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;

final class SkipVariableAssign
{
public readonly int $number;

public function __construct(int $number)
{
$this->number = $number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
use Rector\ValueObject\PhpVersion;

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

$rectorConfig->phpVersion(PhpVersion::PHP_86);
};
200 changes: 200 additions & 0 deletions rules/Php86/Rector/Class_/ConstructorReadonlyAssignToDefaultRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

declare(strict_types=1);

namespace Rector\Php86\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/readonly_property_defaults
* @see \Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\ConstructorReadonlyAssignToDefaultRectorTest
*/
final class ConstructorReadonlyAssignToDefaultRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Move a constant constructor assignment of a readonly property to a property default value',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public readonly int $number;

public function __construct()
{
$this->number = 100;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public readonly int $number = 100;

public function __construct()
{
}
}
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return null;
}

if ($constructClassMethod->stmts === null) {
return null;
}

$keysToRemove = [];

foreach ($node->getProperties() as $property) {
if (! $this->isDefaultableReadonlyProperty($property)) {
continue;
}

$propertyName = $this->getName($property->props[0]);

$assignKey = $this->matchConstantAssignKey($constructClassMethod->stmts, $propertyName);
if ($assignKey === null) {
continue;
}

$expression = $constructClassMethod->stmts[$assignKey];
if (! $expression instanceof Expression || ! $expression->expr instanceof Assign) {
continue;
}

$property->props[0]->default = $expression->expr->expr;
$keysToRemove[] = $assignKey;
}

if ($keysToRemove === []) {
return null;
}

foreach ($keysToRemove as $keyToRemove) {
unset($constructClassMethod->stmts[$keyToRemove]);
}

$constructClassMethod->stmts = array_values($constructClassMethod->stmts);

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::READONLY_PROPERTY_DEFAULT_VALUE;
}

private function isDefaultableReadonlyProperty(Property $property): bool
{
if (! $property->isReadonly()) {
return false;
}

if ($property->isStatic()) {
return false;
}

if ($property->hooks !== []) {
return false;
}

if (count($property->props) !== 1) {
return false;
}

return ! $property->props[0]->default instanceof Expr;
}

/**
* @param Node\Stmt[] $stmts
*/
private function matchConstantAssignKey(array $stmts, string $propertyName): ?int
{
foreach ($stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;
if (! $assign->var instanceof PropertyFetch) {
continue;
}

if (! $this->isName($assign->var->var, 'this')) {
continue;
}

if (! $this->isName($assign->var->name, $propertyName)) {
continue;
}

if (! $this->isConstantValue($assign->expr)) {
return null;
}

return $key;
}

return null;
}

private function isConstantValue(Expr $expr): bool
{
if ($expr instanceof Scalar) {
return true;
}

if ($expr instanceof ConstFetch || $expr instanceof ClassConstFetch) {
return true;
}

if ($expr instanceof UnaryMinus || $expr instanceof UnaryPlus) {
return $this->isConstantValue($expr->expr);
}

return false;
}
}
Loading
Loading