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 o
2E54
ne 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I would prefer to see
return 0 !== count($this->getListeners($eventName));
If for some reason, it's decided to return an object instead, it will always return true, as the object is casted to a boolean, and empty objects are also true.https://3v4l.org/O8WGj
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case you would have same problem with count
https://3v4l.org/goLns
Point of this PR is that hasListeners counts number of listeners needlessly. It only needs to know if it's not empty, it doesn't need to count all of them first to determine that.
We can change this to
return array() !== $this->getListeners...
if you prefer that. Just don't do a count if you don't need it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
count works on everything that implements
\Countable
, so if it returns an object while the method name implies a collection of some sorts, I expect it to also be countable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you plan to replace current implementation with collection (which I doubt, therefore premature optimization), at that point it should be replaced with
->isEmpty
then, notcount
. You don't need to know the count. Contract specifies you don't support this use case anyway. If you did, interface forgetListeners
would specifyarray|\Countable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this can't happen really: that'd be a BC break.
In any case, there is a test case on this method, so this won't happen silently.