Skip to content

Commit

Permalink
[Php81][Restoration] Handle crash on ReadOnlyPropertyRector+MakeTyped…
Browse files Browse the repository at this point in the history
…PropertyNullableIfCheckedRector (#3046)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 11, 2022
1 parent 8ede862 commit 51c2bbe
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -24,6 +25,11 @@
*/
final class MakeTypedPropertyNullableIfCheckedRector extends AbstractRector
{
public function __construct(
private readonly VisibilityManipulator $visibilityManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Make typed property nullable if checked', [
Expand Down Expand Up @@ -101,6 +107,10 @@ public function refactor(Node $node): ?Node
$node->type = new NullableType($currentPropertyType);
$onlyProperty->default = $this->nodeFactory->createNull();

if ($node->isReadonly()) {
$this->visibilityManipulator->removeReadonly($node);
}

return $node;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Issues/IssueReadonlyPropertyNullable/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

final class SomeClass
{
private AnotherClass $anotherClass;

public function run()
{
if ($this->anotherClass === null) {
return null;
}
}
}

?>
-----
<?php

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

final class SomeClass
{
private ?AnotherClass $anotherClass = null;

public function run()
{
if ($this->anotherClass === null) {
return null;
}
}
}

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

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\IssueReadonlyPropertyNullable;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @see https://github.com/rectorphp/rector/issues/7590
*/
final class IssueReadonlyPropertyNullableTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->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,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector;

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

0 comments on commit 51c2bbe

Please sign in to comment.