Skip to content

Replace Elvis operator (?:) by null coalesce operator (??) when applicable

License

Notifications You must be signed in to change notification settings

orklah/psalm-elvis-begone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

psalm-elvis-begone

A Psalm plugin to replace Elvis operator (?:) by null coalesce operator (??) when applicable

Installation:

$ composer require --dev orklah/psalm-elvis-begone
$ vendor/bin/psalm-plugin enable orklah/psalm-elvis-begone

Usage:

Run Psalter command:

$ vendor/bin/psalm --alter --plugin=vendor/orklah/psalm-elvis-begone/src/Plugin.php

Explanation:

The short ternary operator (or Elvis operator ?: ) is used to evaluate return its condition if it's true or the second operand if it's not.

When the type is known and the only falsy value is null, we can actually replace it with the null coalesce operator (??) for strictness and clarity.

It will prevent future values to be evaluated to false when it was not the intention

About

Replace Elvis operator (?:) by null coalesce operator (??) when applicable

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages