framework
framework copied to clipboard
refactor: Some minor performance & readability enhancements
During my latest Inside Laravel livestream I noticed some minor inefficiencies in the Dispatcher code. These most likely are just older code that never got updated.
This includes 3 changes:
- Use
isset($uses[InteractsWithQueue::class], $uses[Queueable::class])instead of a two expensivein_array()calls, this takes two O(n) operations to just O(1). - It replaces an expensive
call_user_func()call with a dynamic method call, using($this->queueResolver)to dererefence the property before calling the closure within. - Simplifies the
pushCommandToQueue()implementation which previously checked if queue and/or delay were set and then callingpushOn()orlaterOn()when with named params we can easily just callpush()andlater()— the use ofqueue: $command->queue ?? nullrather than justqueue: $command->queueis for readability. This one is the only one I'm concerned about, it's possible that an implementation of theQueueinterface/child of theQueueabstract class has different behavior than just wrappingpush()andlater()— but as those must support thequeueparam I don't see any issue with it.
The refactored version is harder to read than the previous version! (For me)
In contrast, I like it and can follow it better. I think it helps keep a clean code base so +1!
Marking as draft since tests are failing.
@crynobone all tests are passing now, only one minor change to a single test (mocking laterOn which is no longer called instead of later)