Skip to content

Detect always defaulting null coalescing operations #4336

@derrabus

Description

@derrabus

Feature request

Example code

https://phpstan.org/r/5959b487-8706-43d3-902a-138dd8c00b48

function sayHello(): void
{
	echo 'Hello, ' . ($name ?? 'anonymous') . '!';
}

Description

The code snippet above is valid PHP and phpstan does not complain about it.

However, we can see that the variable $name here will definitely be undefined. Because of that, the expression will always evaluate to the default value. This is a potential source of errors, e.g. when there is a type in the variable name.

It would be awesome if phpstan could detect and report cases like that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions