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

[ChangeSwitchToMatchRector] Skip when switch does not assigns a variable #6217

Closed
wants to merge 1 commit into from

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Apr 23, 2021

Failing Test for ChangeSwitchToMatchRector

Based on https://getrector.org/demo/f6b01d1b-dd26-4fcf-9260-43f850c0d2bc

It should not change this, as the docs clearly say:

A match expression returns a value.

https://www.php.net/match

This switch does not return a value.

# Failing Test for ChangeSwitchToMatchRector

Based on https://getrector.org/demo/f6b01d1b-dd26-4fcf-9260-43f850c0d2bc

It should not change this, as the docs clearly say:
```
A match expression returns a value.
```
https://www.php.net/match

This switch does not return a value.
@TomasVotruba
Copy link
Member

@tomasnorre Hi Tomas, if you're still up to help with Rector, this might be a good issue to start with 👍
If you don't understand anything, just ask. I'll try to guide you

@tomasnorre
Copy link
Contributor

I'll check it out.

@tomasnorre
Copy link
Contributor

The idea I have atm is to add a function to the SwitchAnalyzer, and include that into the shouldSkipSwitch()

public function hasReturnStmt(Switch_ $switch): bool
{
    // code goes here     
}

But I honestly cannot get my head around how that check would look like.

@ruudk
Copy link
Contributor Author

ruudk commented Apr 24, 2021

Maybe check if there is 1 variable assign in every switch case, and check if that is always the same variable. The cases should contain nothing else except whitespaces or comments. Then apply the rector.

In any other case, don't do anything?

@TomasVotruba
Copy link
Member

TomasVotruba commented Apr 24, 2021

@tomasnorre To find node inside node, you can use NodeFinder:

foreach ($switch->cases as $case) {
    // to find e.g. Return_ node inside Case node
	.... = $this->nodeFinder->findInstanceOf($case, Return::__class);
}

It might help to know how Switch_ is build: https://github.com/rectorphp/php-parser-nodes-docs#phpparsernodestmtswitch_

@samsonasik
Copy link
Member

@ruudk I cherry-picked your commit at PR #6257

TomasVotruba added a commit that referenced this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants