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

[Php71] Add MultiDimensionalArrayToArrayDestructRector #6031

Conversation

nikolicaleksa
Copy link
Contributor

No description provided.

@TomasVotruba
Copy link
Member

Hi, thanks for the proposal.
I personally find there very hard to read and modify later. Why do you think this rule should be part of generic set?

@nikolicaleksa
Copy link
Contributor Author

Hi, thanks for the proposal. I personally find there very hard to read and modify later. Why do you think this rule should be part of generic set?

Hi, are you talking about the MR code being hard to read or the modified code that rector produces with this rule?

I think this rule should be part of a generic set as it's quite similar to the existing rule ListToArrayDestructRector which is already part of the set, the only difference is that this one works with associative arrays.

I personally find this PHP feature neat to use when you have multidimensional associative arrays as you only have couple of options

  • Reference value by foreach-var and accessor all the time ($item['id'], $item['name']....)
  • Introduce a local variable so you don't have to repeat writing the array accessor ($id = $item['id'];, $name = $item['name'];)
  • Use array destructing to introduce local variables and save couple of lines

@ruudk
Copy link
Contributor

ruudk commented Jun 26, 2024

Interesting rule. I never knew it was possible to destruct multi dimensional arrays like this. Only did it for values. TIL!

I think this rule should be part of a generic set as it's quite similar to the existing rule ListToArrayDestructRector which is already part of the set, the only difference is that this one works with associative arrays.

This is something completely different. The ListToArrayDestruct is converts an older syntax list($id1, $name1) = $array to a newer syntax [$id1, $name1] = $array. It's shorter and more common. Also, it changes a single line.

The rule you are proposing is a preference that can potentially create a lot of changes (everything in the loop) and is not something every developers wants.

@nikolicaleksa
Copy link
Contributor Author

Interesting rule. I never knew it was possible to destruct multi dimensional arrays like this. Only did it for values. TIL!

I think this rule should be part of a generic set as it's quite similar to the existing rule ListToArrayDestructRector which is already part of the set, the only difference is that this one works with associative arrays.

This is something completely different. The ListToArrayDestruct is converts an older syntax list($id1, $name1) = $array to a newer syntax [$id1, $name1] = $array. It's shorter and more common. Also, it changes a single line.

The rule you are proposing is a preference that can potentially create a lot of changes (everything in the loop) and is not something every developers wants.

In that case, developer can disable the rule in the configuration or we could have this as part of some other set. In my opinion, even though it changes more than a single line, this is something that will contribute to making the code cleaner and shorter by remove the need for constant foreach variable reference.

@samsonasik
Copy link
Member

I think this should not be included in config/sets, so user can manually enable it theirself

@nikolicaleksa
Copy link
Contributor Author

I think this should not be included in config/sets, so user can manually enable it theirself

Also a possibility. Just let me know what you decide so that I can adjust.

@samsonasik
Copy link
Member

I think not registered in any set is the way it become easy merge, as it may be personal preference syntax

@nikolicaleksa
Copy link
Contributor Author

I think not registered in any set is the way it become easy merge, as it may be personal preference syntax

Done

@nikolicaleksa
Copy link
Contributor Author

@samsonasik Seems like PHPStan is complaining now that it must be registered in a set. How should I fix this?

@samsonasik
Copy link
Member

It seems you can ignore at phpstan.neon

rector-src/phpstan.neon

Lines 238 to 239 in d68e8fa

# optional as changes behavior, should be used explicitly outside PHP upgrade
- '#Register "Rector\\Php73\\Rector\\FuncCall\\JsonThrowOnErrorRector" service to "php73\.php" config set#'

@@ -32,7 +32,7 @@

- [Php70](#php70) (19)

- [Php71](#php71) (7)
- [Php71](#php71) (8)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these changes need to be reverted.

Copy link
Member

Choose a reason for hiding this comment

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

the documentation is generated weekly, you can revert the doc change, and it will be regenerated later by CI

Copy link
Contributor

@ruudk ruudk Jun 26, 2024

Choose a reason for hiding this comment

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

Would be great if manual changes to these files are rejected :D

@TomasVotruba
Copy link
Member

@nikolicaleksa I think better than PHP 7.1, where rules are features most devs want, is better to target "coding style" namespace, where it depends on taste. PHPStan error will be fixed :)

@nikolicaleksa
Copy link
Contributor Author

nikolicaleksa commented Jun 26, 2024

@nikolicaleksa I think better than PHP 7.1, where rules are features most devs want, is better to target "coding style" namespace, where it depends on taste. PHPStan error will be fixed :)

Should I move it to "coding style" set and leave it enabled for that set or just move it there but not register?

@TomasVotruba
Copy link
Member

@nikolicaleksa Only move to the namespace. No registrations as that would automatically run for everyone using the set.

@samsonasik
Copy link
Member

PHPStan notice seems only on php set, on other namespace, that seems working ok without registering in config set.

@nikolicaleksa
Copy link
Contributor Author

All done 🤝

@TomasVotruba
Copy link
Member

Awesome! Thank you for fast work 🎉

@TomasVotruba TomasVotruba merged commit e3b3e18 into rectorphp:main Jun 26, 2024
35 checks passed
@ruudk
Copy link
Contributor

ruudk commented Jun 26, 2024

Just discussed this with a team member, and they gave this feedback:

I knew already, but never really used it as the foreach line becomes too long pretty quickly. You need to split into multiple lines, and then it becomes less readable often.
So doing it more like this:
foreach ($users as $user) {
  ['firstName' => $firstName, 'lastName' => $lastName] = $user;
  
  // .. more fancy stuff
}

Did you consider an alternative syntax as soon as the array shape line becomes too long?

@samsonasik
Copy link
Member

It seems cause downgrade error:

---------------------------------------------------------
Parse error: rector-prefixed-downgraded/rules/CodingStyle/Rector/Foreach/MultiDimensionalArrayToArrayDestructRector.php:4
    2| 
    3| declare (strict_types=1);
  > 4| namespace Rector\CodingStyle\Rector\Foreach;

Foreach should be Foreach_

@samsonasik
Copy link
Member

downgrade error should be resolved at #6045

@samsonasik
Copy link
Member

@ruudk that possibly an improvement area to make rule configurable with "limit" variable config...

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