Coding-standard: EarlyExitSniff fails with yield + else

Created on 12 May 2020  路  4Comments  路  Source: slevomat/coding-standard

while (($data = fgetcsv($handle, 0, ',')) !== false) {
    // skip headline
    if (is_numeric($data[0])) {
        yield $data;
    } else {
        $styledOutput->writeln(print_r($data, true) . 'id is non numeric. Headline? Skipped row!');
    }
}

produces the folllowing error:

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index:  in /xxxx/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php on line 168 in /xxxx/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index:  in /xxxx/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php on line 168 in /xxxx/vendor/squizlabs/php_codesniffer/src/Runner.php on line 606

Call Stack:
    0.0004     395840   1. {main}() /xxxx/vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0071    1583144   2. PHP_CodeSniffer\Runner->runPHPCBF() /xxxx/vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    0.0620    7371880   3. PHP_CodeSniffer\Runner->run() /xxxx/vendor/squizlabs/php_codesniffer/src/Runner.php:200
    0.0656    7981576   4. PHP_CodeSniffer\Runner->processFile() /xxxx/vendor/squizlabs/php_codesniffer/src/Runner.php:434
    0.1483   10042576   5. PHP_CodeSniffer\Reporter->cacheFileReport() /xxxx/vendor/squizlabs/php_codesniffer/src/Runner.php:656
    0.1484   10061256   6. PHP_CodeSniffer\Reports\Cbf->generateFileReport() /xxxx/vendor/squizlabs/php_codesniffer/src/Reporter.php:285
    0.1484   10044744   7. PHP_CodeSniffer\Fixer->fixFile() /xxxx/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php:49
    0.1506   10067360   8. PHP_CodeSniffer\Files\LocalFile->process() /xxxx/vendor/squizlabs/php_codesniffer/src/Fixer.php:174
    0.1506   10067360   9. PHP_CodeSniffer\Files\LocalFile->process() /xxxx/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:91
    0.2097   10947432  10. SlevomatCodingStandard\Sniffs\ControlStructures\EarlyExitSniff->process() /xxxx/vendor/squizlabs/php_codesniffer/src/Files/File.php:496
    0.2097   10947432  11. SlevomatCodingStandard\Sniffs\ControlStructures\EarlyExitSniff->processElse() /xxxx/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php:76
    0.2098   10949592  12. PHP_CodeSniffer\Runner->handleErrors() /xxxx/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php:168

Here is the config:

<rule ref="SlevomatCodingStandard.ControlStructures.EarlyExit">
    <properties>
      <property name="ignoreStandaloneIfInScope" value="true"/>
      <property name="ignoreOneLineTrailingIf" value="true"/>
      <property name="ignoreTrailingIfWithOneInstruction" value="true"/>
    </properties>
</rule>

It does work, when I remove the } else { block beforehand.

Bug

All 4 comments

Hey, I actually got a follow up on this. Turns out, that "yield" is continuing execution afterwards and thus can't be used like a normal "continue" or "break". Which means that the previous code was actually correct, as it used else (sorry!).

what the fixer should have done is:

while (($data = fgetcsv($handle, 0, ',')) !== false) {
    // skip headline
    if (is_numeric($data[0])) {
        yield $data;
        continue;
    }

    $styledOutput->writeln(print_r($data, true) . 'id is non numeric. Headline? Skipped row!');
}

(or just left it alone)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xthiago picture xthiago  路  4Comments

kukulich picture kukulich  路  7Comments

Stadly picture Stadly  路  6Comments

grogy picture grogy  路  5Comments

alexz707 picture alexz707  路  7Comments