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

Overview

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

You might also like...
ShowDoc is a tool greatly applicable for an IT team to share documents online一个非常适合IT团队的在线API文档、技术文档工具

中文版教程:https://www.showdoc.com.cn/help What is ShowDoc ? Whenever we take over a module or project which has been developed by other people, we always

null

Google Cloud Domains for PHP Idiomatic PHP client for Google Cloud Domains. API documentation NOTE: This repository is part of Google Cloud PHP. Any s

The only way to implement the pipe operator in PHP.

Pipe Operator in PHP Introduction This package is based on the pipe operator RFC by Sara Golemon and Marcelo Camargo (2016), who explains the problem

This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify that a number is indeed a number of an operator X

phone-number-checker This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify t

Opsie Operator is a CLI to check your website for HTTP downtime.

Opsie Operator Monitor any website. Run in Docker or any Kubernetes cluster. Send webhooks. 🤝 Supporting If you are using one or more Renoki Co. open

An extended laravel eloquent WHERE method to work with sql LIKE operator.

Laravel Eloquent WhereLike An extended laravel eloquent WHERE method to work with sql LIKE operator. Inspiration The idea of this package comes from o

This is an experiment to export all RFCs from the PHP wiki into Git, including the change history for each RFC (along with the date and author of each change). This is not meant to replace the wiki.

PHP Requests for Comments (RFCs) About This repository is an experiment to export all RFCs from the PHP wiki into Git, including the change history fo

A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.
A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.

TALL multiselect cards A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards. Table of content

This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Laravel Verify New Email Laravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When

For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.
For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.

Super Admin All Sites Menu Also available at https://wordpress.org/plugins/super-admin-all-sites-menu/ Use | Prerequisite | Install | Filters | Demo |

This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Laravel Verify New Email Laravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When

A super simple, clean and pretty error handler that replace the default error handler of PHP. You need only include this file!

php-custom-error-handler A super simple, clean and pretty error handler that replace the default error handler of PHP. You need just include only this

Provides powerful menu editor to replace category based menus in Magento 2

Magento 2 Menu Provides powerful menu editor to replace category based menus in Magento 2. Setup Create new menu in the admin area Content Elements

php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

Easily replace Google Fonts with Bunny Fonts.

=== Replace Google Fonts with Bunny Fonts === Contributors: antonioleutsch Donate link: https://paypal.me/antonioleutsch Tags: google fonts, bunny fon

Replace, concat strings or change number fields permanently using Grid Options
Replace, concat strings or change number fields permanently using Grid Options

It's Pimcore Bundle to replace ,concat strings or change number fields permanently using Grid Options. It will save replaced strings directly in object.

Comments
  • invalid php generated

    invalid php generated

    I'm not going to claim the source PHP the tool was asked to deal with was good. Far from it, it was horrible. But the tool's output was worse in so far as it was invalid php.

    Input: ($arr[$opt] ? '1' : null) ?: '0' Output: ($arr[$opt] ? '1' : null ?? '0'

    The context around it: that line is an argument to a function being called.... something like this:

    <?php
    
    class test {
        function weird(string $variable) {
            $array = [
                'a' => 'apple',
                'b' => 'bananana',
            ];
         
            $this->target(
                'Alpha',
                'Bravo',
                ($array[$variable] ? 'Charlie' : null) ?: 'Delta'
            );
                
        }
    
        function target(string $one, string $two, string $three) {
            echo "$one $two $three" . PHP_EOL;
        }
    }
    
    $thing = new test();
    $thing->weird('a');
    $thing->weird('z');
    

    (once the resulting php syntax error brought my attention to this bit of code, it was thoroughly re-written and no longer resembles... whatever you want to call the above. I'm filing this just in the hopes you can see an obvious fix and save others from bit of panic that sets in when a commit that was 100% automated tooling fails on a pre-commit hook as simple as a syntax check.)

    opened by cabbey 1
Releases(v1.0.0)
Owner
null
The only way to implement the pipe operator in PHP.

Pipe Operator in PHP Introduction This package is based on the pipe operator RFC by Sara Golemon and Marcelo Camargo (2016), who explains the problem

BoostPHP 21 Nov 12, 2022
This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify that a number is indeed a number of an operator X

phone-number-checker This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify t

faso-dev 4 Feb 7, 2022
Michael Pratt 307 Dec 23, 2022
This is an experiment to export all RFCs from the PHP wiki into Git, including the change history for each RFC (along with the date and author of each change). This is not meant to replace the wiki.

PHP Requests for Comments (RFCs) About This repository is an experiment to export all RFCs from the PHP wiki into Git, including the change history fo

Ben Ramsey 34 Jun 20, 2022
For the super admin, replace WP Admin Bar My Sites menu with an All Sites menu.

Super Admin All Sites Menu Also available at https://wordpress.org/plugins/super-admin-all-sites-menu/ Use | Prerequisite | Install | Filters | Demo |

Per Søderlind 18 Dec 17, 2022
A super simple, clean and pretty error handler that replace the default error handler of PHP. You need only include this file!

php-custom-error-handler A super simple, clean and pretty error handler that replace the default error handler of PHP. You need just include only this

null 6 Nov 7, 2022
Provides powerful menu editor to replace category based menus in Magento 2

Magento 2 Menu Provides powerful menu editor to replace category based menus in Magento 2. Setup Create new menu in the admin area Content > Elements

SNOW.DOG 230 Nov 17, 2022
php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

php String Objects Chains like length,forEach,filter,replace,repalcAll much More.... Module

im__koli 1 Mar 29, 2022
Easily replace Google Fonts with Bunny Fonts.

=== Replace Google Fonts with Bunny Fonts === Contributors: antonioleutsch Donate link: https://paypal.me/antonioleutsch Tags: google fonts, bunny fon

Antonio Leutsch 4 Dec 15, 2022
Replace, concat strings or change number fields permanently using Grid Options

It's Pimcore Bundle to replace ,concat strings or change number fields permanently using Grid Options. It will save replaced strings directly in object.

LemonMind.com 5 Aug 31, 2022