phan/phan

Suggestion: plugin to warn for empty() on existing variables

Offen

#3.167 geöffnet am 31.08.2019

 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)PHP (373 Forks)batch import
enhancementhelp wanted

Repository-Metriken

Stars
 (5.455 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 7T) (8 gemergte PRs in 30 T)

Beschreibung

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.

Contributor Guide