rspec/rules/S3981/php/rule.adoc

29 lines
770 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
The count of elements from an array or Countable object is always greater than or equal to zero. So testing that the count is greater than or equal to zero doesn't make sense, since the result is always ``++true++``. Similarly testing that it is less than zero will always return ``++false++``. Perhaps the intent was to check the non-emptiness of the object or array instead.
2020-06-30 12:48:39 +02:00
== Noncompliant Code Example
----
if (count($arr) >= 0) { ... }
if (count($arr) < 0) { ... }
$result = count($arr) >= 0;
if (0 > count($arr)) { ... }
----
== Compliant Solution
----
if (count($arr) != 0) { ... }
if (count($arr) > 0) { ... }
----
ifdef::rspecator-view[]
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::rspecator-view[]