Skip to content

[MinimalScope] Add ChangeLocalPropertyToVariableRector#2558

Merged
TomasVotruba merged 1 commit intomasterfrom
minimal-scope
Jan 4, 2020
Merged

[MinimalScope] Add ChangeLocalPropertyToVariableRector#2558
TomasVotruba merged 1 commit intomasterfrom
minimal-scope

Conversation

@TomasVotruba
Copy link
Copy Markdown
Member

Closes #1900

@TomasVotruba TomasVotruba merged commit aecc17e into master Jan 4, 2020
@TomasVotruba TomasVotruba deleted the minimal-scope branch January 4, 2020 00:38
class SomeClass
{
private $count;
public function run()
Copy link
Copy Markdown
Contributor

@staabm staabm Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldnt the rector fail when the method e.g. looks like

public function run()
     {
         If ($this->count == 5) {
           $this->count = 10;
           return $this->count;
         }
         $this->count = 5;
         return $this->count;
     }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should it fail?

Copy link
Copy Markdown
Contributor

@staabm staabm Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the logic will change because of the rector.

In case it gets transformed into

public function run()
     {
         If ($count == 5) {
           $count = 10;
           return $count;
         }
         $count = 5;
         return $count;
     }

The method will return different values when invoked 2 times in a row

Copy link
Copy Markdown
Member Author

@TomasVotruba TomasVotruba Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method will return different values when invoked 2 times in a row

I see. That's what I was missing.

How would you change the rule to make it work in these cases?

Copy link
Copy Markdown
Contributor

@staabm staabm Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could work when you made sure that there is only a single property-write (or zero)

Copy link
Copy Markdown
Member Author

@TomasVotruba TomasVotruba Jan 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds clear, I'll add fixture for that. Thanks for spotting this 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might needs full new service to analyze scope in the future. Let's see how it handles reality now

TomasVotruba added a commit that referenced this pull request Jun 23, 2022
rectorphp/rector-src@2907687 Fix @return on StaticDoctrineAnnotationParser::resolveAnnotationValue() (#2558)
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.

[DeadCode] Local-only property to variable

2 participants