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.
This package uses function name in a hash calculation. For closures, function name will always be "namespace{closure}", therefore calculated hash will be always the same, if arguments also match. This raises a problem, since once() will be run only once regardless of the method it is nested in and return the same result across methods.
What I did here is, that the hashing method checks, if once() was called from within a closure and instead of using generic function name, it uses the line number of once() call.
Here is an example, how I got an idea for even using once() in a closure:
If I was to use once() outside the closure, it would take $element argument into consideration, so every time the method would be called with a different argument, once() would call its closure. Now with once() being in a closure, the arguments (there are none in the example) are always the same and the calculated hash will match every time).
I could of course extract this expensive function call in its own method, but sometimes you would only need that part one time, so declaring a separate method seems overkill and using closure just seems more "right".
I also included two tests which demonstrate the functionality of this PR.
Bonus: If you wrap once() in a closure, you can also have multiple such occurances per method giving you different results. Currently you can only have one once() per method because every next one will return you the result of the first one.