rspec/rules/S3981/php/rule.adoc

41 lines
946 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
2022-02-04 17:28:24 +01:00
[source,php]
2020-06-30 12:48:39 +02:00
----
if (count($arr) >= 0) { ... }
if (count($arr) < 0) { ... }
$result = count($arr) >= 0;
if (0 > count($arr)) { ... }
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,php]
2020-06-30 12:48:39 +02:00
----
if (count($arr) != 0) { ... }
if (count($arr) > 0) { ... }
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]