-
-
Notifications
You must be signed in to change notification settings - Fork 431
fix: overly strict ArraySpreadInsteadOfArrayMergeRector #7247
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
Conversation
3ab7e36 to
628d85d
Compare
samsonasik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since it optional rule and not part of set, I am fine with this 👍
|
Thank you @calebdw |
This is only true for php8+ |
|
@staabm oops, yes, working with non array at https://3v4l.org/OWtO5#v7.4.33 I will create PR for it. |
|
@staabm it seems there is already test for php 7.4, see Lines 5 to 11 in 15eb8c2
so, the issue is probably it should skip |
|
I am not sure whether there is a bug. Just wanted to point out that one of the assumptions does not work for php7 |
|
@staabm it is a bug, I created PR: to avoid regression. |
Hello!
The
ArraySpreadInsteadOfArrayMergeRectorwas unnecessarily skipping valid transformations when it couldn't determine that parameters were explicitly typed asArrayTypeorConstantArrayType. This prevented the rector from transforming many validarray_merge()calls to the more modern spread operator syntax:This PR removes overly strict type requirement that prevented these valid transformations---
array_merge()itself enforces array types at runtime, it has a strict signature and throws aTypeErrorif non-arrays are passed.This is safe for the following reasons:
array_merge($var1, $var2), then$var1and$var2are guaranteed to be arrays at runtime. Converting to[...$var1, ...$var2]maintains the exact same runtime behavior and type requirements.array_merge($nonArray)and[...$nonArray]throw errors when given non-arrays, so the transformation doesn't change error handling (https://3v4l.org/2YVCi)array_merge(), there's really no reason to not use itThanks!