-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
This sniff disallows the usage of the operators and
, or
, and xor
in favor of their more universally accepted counterparts &&
, ||
, and ^
(respectively). However, there is an error in the case of XOR.
The xor
operator is a logical operator that implicitly casts its arguments to booleans and returns a boolean.
The ^
operator is a bitwise operator that implicitly casts its non-integer arguments to the integers 0 and 1, and returns an integer.
See the PHP documentation for a more thorough explanation. The relevant excerpt is:
If both operands for [...] ^ [...] are strings, then the operation will be performed
on the ASCII values of the characters that make up the strings and the result
will be a string. In all other cases, both operands will be converted to integers
and the result will be an integer.
Given PHP's loose typing, following the suggestion of this sniff and replacing xor
with ^
is not likely to cause significant errors in many applications. However, I did notice that it caused a failure in one of my phpunit unit tests. As a simplified example:
$a = '';
$b = [];
// This first implementation passes because `xor` returns boolean `false`.
$this->assertFalse(is_string($a) xor is_string($b));
// The updated implementation (applying the fix this sniff suggests) fails
// because `^` returns integer `0`.
$this->assertFalse(is_string($a) ^ is_string($b));
My suggested fix is to either:
- Remove the check for
xor
entirely, or - Make the array replacement operators customizable in the config file.