Skip to content

Commit 78aaf7e

Browse files
[Php82] Add ReadOnlyClassRector (#2296)
* [Php82] Add ReadOnlyClassRector * skip has non-readonly property * note * note * note * [ci-review] Rector Rectify * skip has AllowDynamicProperties attribute * skip already readonly * [ci-review] Rector Rectify * skip property promotion not readonly * no params means no property promotion, skip if no property defined * note * note * skip final class, possibly extendable * add fixture * add @see * visibility union ndoe rules/Privatization/NodeManipulator/VisibilityManipulator.php * visibility union ndoe rules/Privatization/NodeManipulator/VisibilityManipulator.php * remove already readonly fixture * comment * skip anonymous class fixture * skip non-final class fixture * skip allow dynamic fixture * class check * skip has writable property fixture * skip no properties * skip property promotion writable * debug * [ci-review] Rector Rectify * fix * eol * fix * [ci-review] Rector Rectify * final touch: add up-to-php82 level setlist Co-authored-by: GitHub Action <[email protected]>
1 parent 93cf392 commit 78aaf7e

18 files changed

+363
-2
lines changed

config/set/level/up-to-php82.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Core\ValueObject\PhpVersion;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
use Rector\Set\ValueObject\SetList;
9+
10+
return static function (RectorConfig $rectorConfig): void {
11+
$rectorConfig->sets([SetList::PHP_82, LevelSetList::UP_TO_PHP_81]);
12+
13+
// parameter must be defined after import, to override imported param version
14+
$rectorConfig->phpVersion(PhpVersion::PHP_82);
15+
};

config/set/php82.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ReadOnlyClassRector::class);
10+
};

packages/Set/ValueObject/LevelSetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
final class LevelSetList implements SetListInterface
1010
{
11+
/**
12+
* @var string
13+
*/
14+
public const UP_TO_PHP_82 = __DIR__ . '/../../../config/set/level/up-to-php82.php';
15+
1116
/**
1217
* @var string
1318
*/

packages/Set/ValueObject/SetList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ final class SetList implements SetListInterface
128128
*/
129129
public const PHP_81 = __DIR__ . '/../../../config/set/php81.php';
130130

131+
/**
132+
* @var string
133+
*/
134+
public const PHP_82 = __DIR__ . '/../../../config/set/php82.php';
135+
131136
/**
132137
* @var string
133138
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
final class OnlyReadonlyProperty
6+
{
7+
private readonly string $property;
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
15+
16+
final readonly class OnlyReadonlyProperty
17+
{
18+
private string $property;
19+
}
20+
21+
?>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
final class OnlyReadonlyProperty2
6+
{
7+
public function __construct(private readonly string $property)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
17+
18+
final readonly class OnlyReadonlyProperty2
19+
{
20+
public function __construct(private string $property)
21+
{
22+
}
23+
}
24+
25+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
#[\AllowDynamicProperties]
6+
final class SkipAllowDynamic
7+
{
8+
private readonly string $property;
9+
}
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\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
class SkipAnonymousClass
6+
{
7+
public function run()
8+
{
9+
new class {
10+
private readonly string $foo;
11+
};
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
final class SkipHasWritableProperty
6+
{
7+
private string $property;
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;
4+
5+
final class SkipNoProperties
6+
{
7+
}

0 commit comments

Comments
 (0)