PHPCSExtra icon indicating copy to clipboard operation
PHPCSExtra copied to clipboard

Feature suggestion: sniff to check for opportunities to use null coalesce

Open jrfnl opened this issue 8 months ago • 0 comments

Is your feature request related to a problem?

Help people find opportunities to modernize their codebase.

Describe the solution you'd like

Some typical code patterns the sniff could look for:

$b = isset($a) ? $a : '';
// Fixed version (PHP 7.0+):
$b = $a ?? '';

$a = isset($a) ? $a : '';
// Fixed version (PHP 7.0+):
$a = $a ?? '';
// Fixed version (PHP 7.4+):
$a ??= '';

$a = '';
if (isset($b)) {
    $a = $b;
}
if (isset($b) === true) {
    $a = $b;
}

// Fixed version (PHP 7.0+):
$a = $b ?? '';

Additional context (optional)

This will need a LOT of tests ;-)

  • [ ] I intend to create a pull request to implement this feature.

jrfnl avatar Apr 21 '25 03:04 jrfnl