Fix notice returning reference in DotAccess::get #9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey there!
Ran into an issue with the latest 0.4.x releases within Laravel. It presented as my watches seemingly registering and polling correctly, however they never actually called my callback with any events.
Tracking the problem down, I found in
Watch::internal_iteratorany exceptions thrown are being caught and not handled which led to no logging/output or other indication of a problem. Removing the try/catch, I was getting an exception thrown:Only variable references should be returned by reference.This is likely to only apply to Laravel as it, by default, converts all PHP error logs into exceptions. Generally this would have gone to a log somewhere never to be seen again. However, it did uncover a potential problem.
The notice was being thrown by
DotAccess::getwhere it's returning the value:While the function's declared to return a reference to a variable, because of the ternary this can only ever return a reference to the result of an expression. That is, this is effectively equivalent to doing:
function &add($n1, $n2) { return $n1 + $n2; }. The reference returned isn't actually to the $currentValue / value returned from propGet. This can be minimally demonstrated with:If you swap out the arrays in the example for objects, they exhibit the same issue. In both cases, the attached diff will correctly return the variable reference instead of a reference to the result of the expression--making the method match what appears to be intended behaviour and also allow watches to work again in Laravel and other frameworks that treat PHP's errors more severely.
Cheers, and thank you! Been using this lib for a bit to hack away at things on my home cluster. Appreciate the work you've put in.