Skip to content
5 changes: 5 additions & 0 deletions rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ private function hasNonTypedProperty(array $properties): bool

private function shouldSkip(Class_ $class, Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return true;
}

if ($this->shouldSkipClass($class)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass;

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

final class ExtendsNotReadOnlyClassTest 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';
}
}
39 changes: 39 additions & 0 deletions tests/Issues/ExtendsNotReadOnlyClass/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass\Fixture;

use Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass\Source\ParentIsNonReadOnlyClass;

class Fixture extends ParentIsNonReadOnlyClass
{
public function __construct(
private string $foo
) {}

public function run(): string
{
return $this->foo;
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass\Fixture;

use Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass\Source\ParentIsNonReadOnlyClass;

final class Fixture extends ParentIsNonReadOnlyClass
{
public function __construct(
private readonly string $foo
) {}

public function run(): string
{
return $this->foo;
}
}

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

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\ExtendsNotReadOnlyClass\Source;

class ParentIsNonReadOnlyClass
{
}
16 changes: 16 additions & 0 deletions tests/Issues/ExtendsNotReadOnlyClass/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
ReadOnlyPropertyRector::class,
FinalizeClassesWithoutChildrenRector::class,
ReadOnlyClassRector::class,
]);
};