Skip to content

Commit 7dee71d

Browse files
authored
[Php86] Add ConstructorReadonlyAssignToDefaultRector for readonly property defaults (#8502)
1 parent fee72fa commit 7dee71d

12 files changed

Lines changed: 370 additions & 1 deletion

File tree

‎config/set/php-version-based.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
use Rector\Php85\Rector\Property\AddOverrideAttributeToOverriddenPropertiesRector;
140140
use Rector\Php85\Rector\ShellExec\ShellExecFunctionCallOverBackticksRector;
141141
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
142+
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
142143
use Rector\Php86\Rector\FuncCall\MinMaxToClampRector;
143144
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
144145
use Rector\Removing\Rector\FuncCall\RemoveFuncCallRector;
@@ -325,6 +326,7 @@
325326

326327
// PHP 8.6
327328
MinMaxToClampRector::class,
329+
ConstructorReadonlyAssignToDefaultRector::class,
328330
]);
329331

330332
// configured rules, each bound to the PHP version its configuration targets

‎config/set/php86.php‎

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

55
use Rector\Config\RectorConfig;
6+
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
67
use Rector\Php86\Rector\FuncCall\MinMaxToClampRector;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->rules([MinMaxToClampRector::class]);
10+
$rectorConfig->rules([MinMaxToClampRector::class, ConstructorReadonlyAssignToDefaultRector::class]);
1011
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ConstructorReadonlyAssignToDefaultRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class ClassConstDefault
6+
{
7+
private const DEFAULT_NAME = 'value';
8+
9+
public readonly string $name;
10+
11+
public function __construct()
12+
{
13+
$this->name = self::DEFAULT_NAME;
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
22+
23+
final class ClassConstDefault
24+
{
25+
private const DEFAULT_NAME = 'value';
26+
27+
public readonly string $name = self::DEFAULT_NAME;
28+
29+
public function __construct()
30+
{
31+
}
32+
}
33+
34+
?>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class Fixture
6+
{
7+
public readonly int $number;
8+
9+
public function __construct()
10+
{
11+
$this->number = 100;
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
20+
21+
final class Fixture
22+
{
23+
public readonly int $number = 100;
24+
25+
public function __construct()
26+
{
27+
}
28+
}
29+
30+
?>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class SkipConditionalAssign
6+
{
7+
public readonly int $number;
8+
9+
public function __construct(bool $flag)
10+
{
11+
if ($flag) {
12+
$this->number = 100;
13+
} else {
14+
$this->number = 200;
15+
}
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class SkipNewObject
6+
{
7+
public readonly \stdClass $value;
8+
9+
public function __construct()
10+
{
11+
$this->value = new \stdClass();
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class SkipNotReadonly
6+
{
7+
public int $number;
8+
9+
public function __construct()
10+
{
11+
$this->number = 100;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector\Fixture;
4+
5+
final class SkipVariableAssign
6+
{
7+
public readonly int $number;
8+
9+
public function __construct(int $number)
10+
{
11+
$this->number = $number;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php86\Rector\Class_\ConstructorReadonlyAssignToDefaultRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(ConstructorReadonlyAssignToDefaultRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_86);
13+
};

0 commit comments

Comments
 (0)