Skip to content

SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator sniff alters the script logic #897

@morozov

Description

@morozov

Consider the following code example:

<?php

function test(array $options)
{
    return (int) isset($options['value']) ? $options['value'] : 1;
}

var_dump(test([]));
// int(1)

// ------------------------------------------------------------------------------------------------
// FOUND 1 ERROR AFFECTING 1 LINE
// ------------------------------------------------------------------------------------------------
//  5 | ERROR | [x] Use null coalesce operator instead of ternary operator.
//    |       |     (SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed)
// ------------------------------------------------------------------------------------------------

After fixing using PHPCBF, this code gets converted into:

<?php

function test(array $options)
{
    return (int) $options['value'] ?? 1;
}

var_dump(test([]));

// Notice: Undefined index: value in test.php on line 5
// 
// Call Stack:
//     0.0002     469520   1. {main}() test.php:0
//     0.0003     469520   2. test() test.php:8
// 
// test.php:8:
// int(0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions