8000 [EventDispatcher] don't count empty listeners by xabbuh · Pull Request #11475 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged
merged 1 commit into from
Jul 27, 2014
Merged

Conversation

xabbuh
Copy link
Member
@xabbuh xabbuh commented Jul 25, 2014
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.

@fabpot
Copy link
Member
fabpot commented Jul 25, 2014

👍

@@ -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)));
Copy link
Contributor

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()?

Copy link
Member Author

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.

Copy link
Member Author

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.

@ghost
Copy link
ghost commented Jul 25, 2014

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 () {
};
Copy link
Contributor

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.

Copy link
Member Author

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.
@jakzal
Copy link
Contributor
jakzal commented Jul 25, 2014

👍

@fabpot
Copy link
Member
fabpot commented Jul 27, 2014

Thank you @xabbuh.

@fabpot fabpot merged commit fdbb04a into symfony:2.3 Jul 27, 2014
fabpot added a commit that referenced this pull request Jul 27, 2014
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
@xabbuh xabbuh deleted the issue-11444 branch July 27, 2014 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0