8000 [EventDispatcher] Remove unneded count() by ostrolucky · Pull Request #22237 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[EventDispatcher] Remove unneded count() #22237

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
Apr 3, 2017

Conversation

ostrolucky
Copy link
Contributor
Q A
Branch? 2.7
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

@curry684
Copy link
Contributor

Anything that's not a bug fix should be based on master, not stable branches.

@@ -80,7 +80,7 @@ public function getListeners($eventName = null)
*/
public function hasListeners($eventName = null)
{
return (bool) count($this->getListeners($eventName));
return (bool) $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.

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

Copy link
Contributor Author
@ostrolucky ostrolucky Apr 1, 2017

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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, not count. You don't need to know the count. Contract specifies you don't support this use case anyway. If you did, interface for getListeners would specify array|\Countable.

Copy link
Member

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.

@nicolas-grekas
Copy link
Member

Thank you @ostrolucky.

@nicolas-grekas nicolas-grekas merged commit 9a1915f into symfony:2.7 Apr 3, 2017
nicolas-grekas added a commit that referenced this pull request Apr 3, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[EventDispatcher] Remove unneded count()

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

9a1915f [EventDispatcher] Remove unneded count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0