Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php81][Restoration] Handle crash on ReadOnlyPropertyRector+MakeTypedPropertyNullableIfCheckedRector #3046

Merged
merged 4 commits into from
Nov 11, 2022

Conversation

samsonasik
Copy link
Member

Given the following code:

final class SomeClass
{
    private AnotherClass $anotherClass;

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

It changed to:

-private AnotherClass $anotherClass;
+private readonly ?AnotherClass $anotherClass = null;

which nullable property can't be readonly, and immediatelly cause fatal error:

Fatal error: Readonly property SomeClass::$anotherClass cannot have default value

see https://3v4l.org/3RBpN

Applied rules:

Rector\Php81\Rector\Property\ReadOnlyPropertyRector
Rector\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector

This PR try to fix it.

Fixes rectorphp/rector#7590

@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @Wohlie

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@TomasVotruba
Copy link
Member

Thank you 👍

@TomasVotruba TomasVotruba merged commit 51c2bbe into main Nov 11, 2022
@TomasVotruba TomasVotruba deleted the handle-crash-readonly-make-typed branch November 11, 2022 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect behavior of ReadOnlyPropertyRector, MakeTypedPropertyNullableIfCheckedRector
3 participants