fix: overly strict ArraySpreadInsteadOfArrayMergeRector#7247
Merged
samsonasik merged 1 commit intorectorphp:mainfrom Sep 12, 2025
Merged
fix: overly strict ArraySpreadInsteadOfArrayMergeRector#7247samsonasik merged 1 commit intorectorphp:mainfrom
samsonasik merged 1 commit intorectorphp:mainfrom
Conversation
3ab7e36 to
628d85d
Compare
samsonasik
approved these changes
Sep 12, 2025
Member
samsonasik
left a comment
There was a problem hiding this comment.
since it optional rule and not part of set, I am fine with this 👍
Member
|
Thank you @calebdw |
Contributor
This is only true for php8+ |
Member
|
@staabm oops, yes, working with non array at https://3v4l.org/OWtO5#v7.4.33 I will create PR for it. |
Member
|
@staabm it seems there is already test for php 7.4, see so, the issue is probably it should skip |
Contributor
|
I am not sure whether there is a bug. Just wanted to point out that one of the assumptions does not work for php7 |
Member
|
@staabm it is a bug, I created PR: to avoid regression. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!