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

[10.x] Add methods dumpWhen and ddWhen to Str and Collection #48281

Closed
wants to merge 10 commits into from

Conversation

monurakkaya
Copy link
Contributor

@monurakkaya monurakkaya commented Sep 3, 2023

I believe it would be useful to have such a shortcut for dumping things especially when working in large nested arrays/objects.

usage:

// Collection / EloquentCollection

collect([1,2,3]) // or User::get()
    ->dumpWhen(fn ($items) => $items->count() === 3) // or ddWhen()
    ->toArray();


// Str
Str::of($firstName)
    ->append($lastName)
    ->dumpWhen(fn ($str) => $str->endsWith('A')) // or ddWhen()
    ->toString();

@milwad-dev
Copy link
Contributor

Why need add a condition for dd or dump?
It's an additional method, you can do like this:

$collect = collect([1,2,3])->toArray();

if (...) {
   dd();
} 

@monurakkaya
Copy link
Contributor Author

monurakkaya commented Sep 3, 2023

Let's say we have n depth array of objects. While we are traversing the whole data, it would be useful to stop and check data if condition is met or not. Debbuging will be much easier this way, because we are focusing the only needed part of big data.

It can be in any level don't just think about root collection.

$hotels = collect([
    collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
    collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3])
]);

$ratings = $hotels->map(
    fn ($hotel) => $hotel->only('id', 'rating')
        ->ddWhen(is_null($hotel->get('rating')))
);

This way, I can print only the hotel that have problem.

@milwad-dev
Copy link
Contributor

So, Do you think this method is helpful for most of the projects!!
I think is good for the macro in the Collection

@rodrigopedra
Copy link
Contributor

The Enumerable@when() method accepts a third argument that runs a default callback.

Wouldn't this work the same?

$hotels = collect([
    collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
    collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);

$ratings = $hotels->map(fn ($hotel) => $hotel->when(
    is_null($hotel->get('rating')),
    $hotel->dd(),
    fn () => $hotel->only('id', 'rating'),
));

Although, as it is meant to be used while debugging, I would personally switch to a regular closure and use an if statement inside it.

Most IDE's have shortcuts for converting between regular closures and arrow functions.

Lastly, I know Laravel is ok with adding features on minor releases, but I am not sure if that extends to adding methods to interfaces/contracts.

Maybe, if maintainers think this is a good addition to the framework, you might need to add these methods to the base classes first, and then send a PR to add it to the Enumerable interface to 11.x.

But I am not totally sure about this.

@rodrigopedra
Copy link
Contributor

rodrigopedra commented Sep 4, 2023

Or even:

$hotels = collect([
    collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
    collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);

$ratings = $hotels
    ->map(fn ($hotel) => $hotel->only('id', 'rating'))
    ->transform(fn ($hotel) => $hotel->when(is_null($hotel->get('ratings')), fn () => $hotel->dd()));

Or terser:

$hotels = collect([
    collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
    collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);

$ratings = $hotels
    ->map(fn ($hotel) => $hotel->only('id', 'rating'))
    ->transform(fn ($hotel) => $hotel->get('ratings') ?? $hotel->dd());

@monurakkaya
Copy link
Contributor Author

monurakkaya commented Sep 4, 2023

As you can see from the name of the method, this is just a shortcut of ->when(condition, fn () => dd())

@rodrigopedra
Copy link
Contributor

Sure, I get it, I just miss the value of adding those.

Why not add convenience methods like mapWhen(), partitionWhen(), transformWhen(), ... or one to each of the many of the methods offered by the Collection class?

Maybe there is an opportunity for a high order method, something like collect()->when(condition)->method().

I guess this could be a better addition, in case you are interested to explore it.

Well, at the end, let's wait for the maintainer's opinion on this.

Have a nice day =)

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@rodrigopedra
Copy link
Contributor

@monurakkaya actually the when method already has support for a Higher Order Proxy, I didn't know that.

It allows one to do this:

$hotels = collect([
    collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
    collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);

$ratings = $hotels->map(
    fn ($hotel) => $hotel->only('id', 'rating')
        ->when(\is_null($hotel->get('rating')))->dd()
);

I thought you'd like to know, as it seems very handy for debugging and somewhat similar to your proposal.

@monurakkaya monurakkaya deleted the feat/10-x-dd-if branch September 12, 2023 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants