倉庫指標
- 星標
- (5,455 顆星)
- PR 合併指標
- (平均合併 7天) (30 天內合併 8 個 PR)
描述
Per PHP docs, empty($var) is mostly equivalent to !isset($var) || !$var. However, I personally keep finding projects where empty is just used to check e.g. whether an array is empty. That's probably because of the deceiving name, but I rarely see empty used on variables that may not exist.
Assuming that phan can determine whether a given variable may be unset in a given context, I propose to add a plugin which would warn when empty is used on a variable that will 100% exists. In those cases, it should suggest to just use !$var.
The rationale behind this idea is that empty makes the code needlessly more verbose, it has slightly worse performance, since it involves a function call (OK, that's really small, but I'd say it's just a plus), and most importantly, it could make the code less future-proof, since it will suppress warnings if the variable becomes unset.