slevomat / coding-standard

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

phpcbf fails with a fatal error originated in UseStatementHelper #254

Closed morozov closed 6 years ago

morozov commented 6 years ago

Steps to reproduce:

  1. Check out the branch where the issue was reproduced:git clone git@github.com:morozov/dbal.git -b slevomat-issue.
  2. Install dependencies: composer install
  3. Run vendor/bin/phpcbf tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php.

The output will start with:

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: scope_closer in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 130 in vendor/squizlabs/php_codesniffer/src/Runner.php on line 562

↪ composer show # truncated

slevomat/coding-standard    4.3.2  Slevomat Coding Standard for PHP_Code...
squizlabs/php_codesniffer   3.2.2  PHP_CodeSniffer tokenizes PHP, JavaSc...
kukulich commented 6 years ago

@morozov Could you please find the file that caused this error?

morozov commented 6 years ago

@kukulich you mean tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php?

kukulich commented 6 years ago

@morozov I'm sorry I missed it. Thanks.

kukulich commented 6 years ago

Hmm, it looks like a bug in PHPCS itself. The fixer creates invalid tokens and it's not possible to create a workaround :(

It's probably because of 417 CS errors in the file :)

You can use this workaround:

  1. Comment all Slevomat sniffs in vendor/doctrine/coding-standard/lib/Doctrine/ruleset.xml
  2. Run vendor/bin/phpcbf tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php
  3. Revert changes in vendor/doctrine/coding-standard/lib/Doctrine/ruleset.xml
morozov commented 6 years ago

Thanks @kukulich. Let's keep it open. If either of us is able to reduce the existing 417 CS errors to a smaller set, we could report the issue upstream to PHP CodeSniffer.

coudenysj commented 6 years ago

Probably fixed in https://github.com/slevomat/coding-standard/issues/312?

morozov commented 6 years ago

@coudenysj no, still reproducible. Same steps as above, plus:

{
  "require-dev": {
    "slevomat/coding-standard": "dev-master as 4.5.2"
  }
}

Then:

↪  composer update 
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Updating slevomat/coding-standard (4.5.2 => dev-master c04fb83):  Checking out c04fb83598
Writing lock file
Generating autoload files
PHP CodeSniffer Config installed_paths set to ../../slevomat/coding-standard/,../../doctrine/coding-standard/lib/

↪  vendor/bin/phpcbf tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: scope_closer in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 131 in vendor/squizlabs/php_codesniffer/src/Runner.php on line 562

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined index: scope_closer in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 131 in vendor/squizlabs/php_codesniffer/src/Runner.php on line 562

Call Stack:
    0.0001     398608   1. {main}() vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0023     816288   2. PHP_CodeSniffer\Runner->runPHPCBF() vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    0.1153    5689936   3. PHP_CodeSniffer\Runner->run() vendor/squizlabs/php_codesniffer/src/Runner.php:193
    0.1522    6253136   4. PHP_CodeSniffer\Runner->processFile() vendor/squizlabs/php_codesniffer/src/Runner.php:462
    0.6801   10692992   5. PHP_CodeSniffer\Reporter->cacheFileReport() vendor/squizlabs/php_codesniffer/src/Runner.php:611
    0.6802   10711576   6. PHP_CodeSniffer\Reports\Cbf->generateFileReport() vendor/squizlabs/php_codesniffer/src/Reporter.php:262
    0.6802   10711576   7. PHP_CodeSniffer\Fixer->fixFile() vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php:48
    1.3739    9691448   8. PHP_CodeSniffer\Files\LocalFile->process() vendor/squizlabs/php_codesniffer/src/Fixer.php:174
    1.3739    9691448   9. PHP_CodeSniffer\Files\LocalFile->process() vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php:91
    1.3995    8246624  10. SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff->process() vendor/squizlabs/php_codesniffer/src/Files/File.php:489
    1.3995    8246944  11. SlevomatCodingStandard\Helpers\UseStatementHelper::getUseStatements() vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Exceptions/ReferenceThrowableOnlySniff.php:35
    1.3995    8247056  12. SlevomatCodingStandard\Helpers\UseStatementHelper::getUseStatementPointers() vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php:84
    1.3996    8248600  13. PHP_CodeSniffer\Runner->handleErrors() vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php:131

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined variable: childOutput in vendor/squizlabs/php_codesniffer/src/Runner.php on line 705 in vendor/squizlabs/php_codesniffer/src/Runner.php on line 562

PHP_CodeSniffer\Exceptions\RuntimeException: Undefined variable: childOutput in vendor/squizlabs/php_codesniffer/src/Runner.php on line 705 in vendor/squizlabs/php_codesniffer/src/Runner.php on line 562

Call Stack:
    0.0001     398608   1. {main}() vendor/squizlabs/php_codesniffer/bin/phpcbf:0
    0.0023     816288   2. PHP_CodeSniffer\Runner->runPHPCBF() vendor/squizlabs/php_codesniffer/bin/phpcbf:18
    0.1153    5689936   3. PHP_CodeSniffer\Runner->run() vendor/squizlabs/php_codesniffer/src/Runner.php:193
    0.1171    5825744   4. PHP_CodeSniffer\Runner->processChildProcs() vendor/squizlabs/php_codesniffer/src/Runner.php:502
    1.4441    6193680   5. PHP_CodeSniffer\Runner->handleErrors() vendor/squizlabs/php_codesniffer/src/Runner.php:705
morozov commented 6 years ago

@kukulich why is it a "Won't Fix"? I still can reproduce it with slevomat/coding-standard 4.7.0.

Hmm, it looks like a bug in PHPCS itself. The fixer creates invalid tokens and it's not possible to create a workaround :(

Sorry, I missed that. Would it make sense to file a ticket for PHPCS?

kukulich commented 6 years ago

Yes, it would make sense to report it there.

morozov commented 6 years ago

I've filed https://github.com/squizlabs/PHP_CodeSniffer/issues/2142. It doesn't seems to be caused by the large number of errors since it's reproducible with just one violation:

<?php

namespace Doctrine\DBAL;

class PhpCodeSnifferFailure
{
    public function foo()
    {
        return [
        <<<HERE
HERE
            ,
        ];
    }
}

Also, the OOTB sniffs do not always expect the scope closer attribute to be set for T_CLASS tokens (still have no idea why, looks like a bug on their end). See the ClassDeclarationSniff for example:

public function process(File $phpcsFile, $stackPtr)
{
    $tokens = $phpcsFile->getTokens();
    if (isset($tokens[$stackPtr]['scope_closer']) === false) {
        return;
    }
deno085 commented 5 years ago

I ran into this problem today, I was running PSR2 and Phalcon standards. I Removed the Phalcon so it was just PSR2 and it ran fine, then ran it again with both and it ran fine that way...

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.