slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 176 forks source link

ControlStructures.EarlyExit fixer breaks code #463

Closed morozov closed 6 years ago

morozov commented 6 years ago

I'm using the standard as part of doctrine/coding-standard. Please consider the example:

<?php declare(strict_types=1);

/**
 * @return iterable|int[]
 */
function test(bool $flag) : iterable
{
    if ($flag) {
        yield 1;
    } else {
        yield 2;
    }
}

foreach (test(true) as $value) {
    var_dump($value);
}

This code gets flagged with the following error:

------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------
 10 | ERROR | [x] Remove useless else to reduce code nesting.
    |       |     (SlevomatCodingStandard.ControlStructures.EarlyExit.UselessElse)
------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------

If automatically fixed, the file turns into:

<?php declare(strict_types=1);

/**
 * @return iterable|int[]
 */
function test(bool $flag) : iterable
{
    if ($flag) {
        yield 1;
    }

    yield 2;
}

foreach (test(true) as $value) {
    var_dump($value);
}

The logic is now changed and instead of yielding one value, the function yields both.

kukulich commented 6 years ago

Thank you for your report.

Fixed in https://github.com/slevomat/coding-standard/commit/1926e819fdfcebc54f13963e7d1a9cbd5ffbae38

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.