-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add ArrayToArrGetRector #382
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
feat: add ArrayToArrGetRector #382
Conversation
|
What do you say? @peterfox |
|
Great! Do it |
|
Not sure I really care for this rule for non-nested array access? Why nearly double the number of characters needed? -$array['key'];
+data_get($array, 'key');Now, converting a null coalesce could be useful: -$array['key'] ?? null;
+data_get($array, 'key');
-$array['key'] ?? 'foo';
+data_get($array, 'key', 'foo'); |
|
I'm open to making it for |
|
@MrPunyapal sorry for my slowness. Been busy of late. The rule looks really good. I agree, it might be better to use There's some useful code here as well I'd like to put into their own classes but that can be done later once this is merged. With that in mind, would you be okay to make the change to |
|
@peterfox updated things based on your suggestions. |
…ay access to data_get() helper
4bf9ecb to
afb6d38
Compare
peterfox
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.
Thanks @MrPunyapal this looks good to me
|
I don't understand. Why introduce slowness and pull away from core PHP just for the sake of using Laravel methods? Did anyone benchmark this on a large scale? |
|
This pull request introduces a new Rector rule that automatically converts direct array access to use Laravel's
Arr::get()method, improving code consistency and maintainability when working with nested arrays. The implementation includes comprehensive support for both simple array access and null coalesce patterns, with smart handling of edge cases.New Rector Rule: Array Access to
Arr::get()ConversionAdded
ArrayToArrGetRectorrule to convert array access patterns:$array['key']→\Illuminate\Support\Arr::get($array, 'key')$array['nested']['key']→\Illuminate\Support\Arr::get($array, 'nested.key')$array['key'] ?? 'default'→\Illuminate\Support\Arr::get($array, 'key', 'default')$array['key'] ?? throw new Exception()(unchanged)The rule handles both scalar string and integer keys, building proper dot notation for nested access
Skips dynamic keys, complex expressions, and null coalesce with throw statements to maintain safety and original behavior
Example