You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #54447 Remove unnecessary empty usages (ostrolucky)
This PR was merged into the 7.1 branch.
Discussion
----------
Remove unnecessary empty usages
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? |no
| Deprecations? | no
| Issues |
| License | MIT
I've written a rector for this rectorphp/rector-src#5783 and decided to apply it on Symfony src here.
As for motivation for removing empty where possible, in my and several other prominent devs opinions, using `empty()` is a bad practice for several reasons:
- it's misleading. It doesn't work with collection or stringable objects. Its meaning is more like `isset($foo) && (bool) $foo` rather than `count($foo) === 0` or `strlen($foo) === 0`, contrary to name of this function.
- it decreases safety of the code, because it's doing implicit `isset` call, so once you remove variable assignment, `empty()` call that references it continues silently working
- it increases complexity because of having to negate the call in most cases (there is no `nonempty()` function)
- it's unnecessary in most cases and hence adds extra opcodes and increases length of code for no good reason
There were also several articles written on this topic, I know about these 2:
- https://www.contextualcode.com/Blog/php-micro-optimization.-variable-boolean-cast-vs-!empty
- https://www.beberlei.de/2021/02/19/when_to_use_empty_in_php_i_say_never.html (this one is from `@beberlei`)
However, personally I'm not in a super strict camp. I like that I don't have to explicitly specify `[] === $foo` or `'' === $foo` or `null === $foo` (and so on and on) and I think Symfony shares the similar opinion.
Commits
-------
ccc813c65f Remove unnecessary empty usages
0 commit comments