nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
552 stars 11 forks source link

Why does the documentation not say a closure can be passed? #72

Open mauriciomateus opened 6 years ago

mauriciomateus commented 6 years ago

Hi:

Passing a closure is extremely helpful. I would not have found out a closure can be passed unless for the video.

Even though I am building complex stuff nowadays I did not know that "$mixed value" in the docblocks can be understood as a callback. Can it?

Kind of a silly question but I'd appreciate an answer.

Thanks.

adamwathan commented 6 years ago

Hey @mauriciomateus! Yeah there's definitely some tricks in the Laravel source that aren't documented like that. mixed in a docblock doesn't always mean it can accept a callback, but a surprisingly large amount of methods in Laravel can accept a callback instead of a value if it makes sense.

The way I usually find out is just having an insight, "hey I really wish this could accept a callback, I wonder if it can?", then checking the source for that function and seeing if there's any clues.

One of those clues is if Laravel calls the value() function on the argument that gets passed in, which will automatically invoke the value if it's a callback, or just return the value as is otherwise:

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/helpers.php#L1139-L1142

Hope that helps!

javi-dev commented 6 years ago

One of the more useful things I've learned from this course is to use the source code as documentation. Source diving used to "scare" me, ("this is not my code, I'll spend hours googling this obscure thing instead"). Watching Adam doing it so often and so naturally inspired me to do it myself, even breaking and dd()ing things in the vendor folder without a care in the world, I can always rm -rf it.

I don't want to derail the thread, but I couldn't help myself to take this opportunity to thank @adamwathan for teaching me this. It's a valuable bonus skill, unrelated to TDD (but I probably wouldn't be so adventurous I didn't have the tests backing me up).