-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] don't count empty listeners #11475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
👍 |
@@ -82,7 +82,7 @@ public function getListeners($eventName = null) | |||
*/ | |||
public function hasListeners($eventName = null) | |||
{ | |||
return (bool) count($this->getListeners($eventName)); | |||
return (bool) count(array_filter($this->getListeners($eventName))); |
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.
Should we still return empty listeners in getListeners()
?
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.
No, I guess that doesn't make any sense, does it? So, moving the filter to getListeners()
would be better I think.
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.
I moved the array_filter()
call.
Please note that by moving the array_filter() from my proposed solution in the return value of getListeners() to hasListeners() still makes getListeners() return empty arrays... |
public function testGetListenersWhenAddedCallbackListenerIsRemoved() | ||
{ | ||
$listener = function () { | ||
}; |
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.
Could you define this (and previous) anonymous function in a single line. At the moment looks a bit odd.
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.
Of course, done.
When event listeners for certain events are removed from the event dispatcher, empty arrays are not being removed. Therefore, counting on empty arrays leads to wrong results of the hasListeners() method.
👍 |
Thank you @xabbuh. |
This PR was merged into the 2.3 branch. Discussion ---------- [EventDispatcher] don't count empty listeners | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11444 | License | MIT | Doc PR | When event listeners for certain events are removed from the event dispatcher, empty arrays are not being removed. Therefore, counting on empty arrays leads to wrong results of the hasListeners() method. Thanks to @mlindenb for discovering this an proposing a solution. Commits ------- fdbb04a [EventDispatcher] don't count empty listeners
When event listeners for certain events are removed from the event
dispatcher, empty arrays are not being removed. Therefore, counting
on empty arrays leads to wrong results of the hasListeners() method.
Thanks to @mlindenb for discovering this an proposing a solution.