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

Incorrect behavior of ReadOnlyPropertyRector, MakeTypedPropertyNullableIfCheckedRector #7590

Closed
Wohlie opened this issue Nov 10, 2022 · 2 comments · Fixed by rectorphp/rector-src#3046
Labels

Comments

@Wohlie
Copy link

Wohlie commented Nov 10, 2022

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/1f0af0d6-cd51-4f66-a14d-8e5b3e1797f6

<?php

 final class SomeClass
 {
     private AnotherClass $anotherClass;

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

Responsible rules

  • ReadOnlyPropertyRector

  • MakeTypedPropertyNullableIfCheckedRector

Expected Behavior

The property anotherClass can't be read only, because read only properties can't have a default value.

With the refactored code, you will get a Fatal error: Readonly property SomeClass::$anotherClass cannot have default value exception: https://3v4l.org/Ke5Jc

@Wohlie Wohlie added the bug label Nov 10, 2022
@samsonasik
Copy link
Member

The original code seems already fatal error, see https://3v4l.org/Ke5Jc

@Wohlie
Copy link
Author

Wohlie commented Nov 10, 2022

Yes, but you will get another error.

If you change the order of the rules, the output is correct, with no default value:
https://getrector.org/demo/843629aa-01d0-4738-8f0a-59672231e748

You can also use the following code snipped:
https://getrector.org/demo/670db2f0-3ed9-4c8a-95dd-f0ae2d59621a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants