PHP 7.0 introduced the null coalescing operator to allow for abbreviating typical isset($x) ? $x : $y ternaries.
Related PHPCompatibility sniff(s):
NewLanguageConstructs
PHP manual references:
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
$action = $_POST['action'] ?? 'default';
// The above is identical to this if/else statement
$action = $_POST['action'] ?? 'default';
$username = $_GET['user'] ?? 'nobody';
Notes for implementation of the sniff:
Whether or not to shorten if/else statements which comply with this pattern should probably be decided based on a public property which can be set from within a custom ruleset.
Short description
PHP 7.0 introduced the null coalescing operator to allow for abbreviating typical
isset($x) ? $x : $y
ternaries.Related PHPCompatibility sniff(s):
NewLanguageConstructs
PHP manual references:
http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
Example code:
Detect the following code pattern(s):
And fix these to:
Notes for implementation of the sniff:
Prior art:
ternary_to_null_coalescing