collect() helpers accepts arguments other than iterables leading to unexpected behaviour in case of an oversight #57160
Unanswered
michapietsch
asked this question in
Ideas
Replies: 1 comment
-
This is not an issue of the helper per se but lies in framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 1062 to 1073 in 7514188 To solve this, you could create a PR targeting In that case, I doubt, runtime checks are really a solution here. You might have more luck with static analysis tools like Rector |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12.30.1
PHP Version
8.3.24
Database Driver & Version
No response
Description
I ran into a bug when I tried to spread two arrays into a new collection like this:
The result is equal to
collect(1)
.I simply forgot the surrounding
[]
.I see a chance to improve developer experience here: Communicate that the helper is being used wrong.
After all the helper is declared to accept:
@param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value
.But it accepts e. g.
1
which is neither of these, and it silently omit the additional arguments from the array spread.I suggest the helper could throw an invalid argument exception for non-iterable values, and/or throw when more than one argument is passed.
PHPstan requires levels 9 to catch such things.
Steps To Reproduce
In Tinker run:
The output will be:
Beta Was this translation helpful? Give feedback.
All reactions