Skip to content

Commit ac4f09b

Browse files
authored
feat: add RemoveReadonlyPropertyVisibilityOnReadonlyClassRector (#7115)
This Rector removes the `readonly` visibility modifier from properties in classes that are already marked as `readonly`. This is useful for cleaning up code where the `readonly` modifier is redundant due to the class's readonly status.
1 parent e05e393 commit ac4f09b

14 files changed

+406
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
#[SomeAttribute]
6+
final readonly class ClassWithAttribute
7+
{
8+
private readonly string $property;
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
16+
17+
#[SomeAttribute]
18+
final readonly class ClassWithAttribute
19+
{
20+
private string $property;
21+
}
22+
23+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
#[SomeAttribute]final readonly class ClassWithAttributeInline
6+
{
7+
private readonly string $property;
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
15+
16+
#[SomeAttribute]final readonly class ClassWithAttributeInline
17+
{
18+
private string $property;
19+
}
20+
21+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class ClassWithoutAttribute {
6+
private readonly string $property;
7+
}
8+
9+
?>
10+
-----
11+
<?php
12+
13+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
14+
15+
final readonly class ClassWithoutAttribute {
16+
private string $property;
17+
}
18+
19+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class CombinePromoAndPropertyReadonly
6+
{
7+
public readonly string $b;
8+
9+
public function __construct(
10+
public readonly string $a,
11+
?string $b = null
12+
) {
13+
$this->b = $b ?? 'foo';
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
22+
23+
final readonly class CombinePromoAndPropertyReadonly
24+
{
25+
public string $b;
26+
27+
public function __construct(
28+
public string $a,
29+
?string $b = null
30+
) {
31+
$this->b = $b ?? 'foo';
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class ImplicitPublicReadonlyProperty
6+
{
7+
readonly string $foo;
8+
9+
public function __construct()
10+
{
11+
$this->foo = 'bar';
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
20+
21+
final readonly class ImplicitPublicReadonlyProperty
22+
{
23+
public string $foo;
24+
25+
public function __construct()
26+
{
27+
$this->foo = 'bar';
28+
}
29+
}
30+
31+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class ImplicitPublicReadonlyPropertyPromotion
6+
{
7+
public function __construct(readonly string $name)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
17+
18+
final readonly class ImplicitPublicReadonlyPropertyPromotion
19+
{
20+
public function __construct(public string $name)
21+
{
22+
}
23+
}
24+
25+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class OnlyReadonlyProperty
6+
{
7+
private readonly string $property;
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
15+
16+
final readonly class OnlyReadonlyProperty
17+
{
18+
private string $property;
19+
}
20+
21+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class OnlyReadonlyProperty2
6+
{
7+
public function __construct(private readonly string $property)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
17+
18+
final readonly class OnlyReadonlyProperty2
19+
{
20+
public function __construct(private string $property)
21+
{
22+
}
23+
}
24+
25+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class WithAttributeOnProperty
6+
{
7+
#[MyAttr]
8+
public readonly string $id;
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
16+
17+
final readonly class WithAttributeOnProperty
18+
{
19+
#[MyAttr]
20+
public string $id;
21+
}
22+
23+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
4+
5+
final readonly class WithAttributeOnPropertyPromotion
6+
{
7+
private function __construct(
8+
#[MyAttr]
9+
private readonly string $id
10+
){}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\Fixture;
18+
19+
final readonly class WithAttributeOnPropertyPromotion
20+
{
21+
private function __construct(
22+
#[MyAttr]
23+
private string $id
24+
){}
25+
}
26+
27+
?>

0 commit comments

Comments
 (0)