Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined index in FunctionDeclarationSniff #3917

Open
3 tasks done
GuySartorelli opened this issue Nov 2, 2023 · 3 comments
Open
3 tasks done

Undefined index in FunctionDeclarationSniff #3917

GuySartorelli opened this issue Nov 2, 2023 · 3 comments

Comments

@GuySartorelli
Copy link

GuySartorelli commented Nov 2, 2023

Describe the bug

There's sort of two bugs in one here. One is that the error message is displayed differently when running phpcs than it is when running phpcbf. The other is that the error happens at all.

When I run phpcs over the below code, it gives a relatively nice error message, and when I run phpcbf over it gives a nasty looking one - but in both cases the error message doesn't tell me what I need to change about my PHP code to fix it.

Code sample

<?php
public function process(Schema $schema, string $query, array $context, array $vars, callable $next)

Custom ruleset

N/A

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --standard=psr12 test.php
  3. See error message displayed
    FILE: /path/to/project/test.php
    -------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 1 LINE
    -------------------------------------------------------------------------------------------------------------------
     1 | ERROR | [x] Header blocks must be separated by a single blank line
     1 | ERROR | [ ] An error occurred during processing; checking has been aborted. The error message was: Undefined
       |       |     array key -1 in
       |       |     /path/to/project/vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
       |       |     on line 130
    -------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------
    
    Time: 31ms; Memory: 6MB
    
  4. Run phpcbf --standard=psr12 test.php
  5. See error message displayed
    PHP Fatal error:  Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined array key -1 in /path/to/project/vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php on line 130 in /path/to/project/vendor/squizlabs/php_codesniffer/src/Runner.php:608
    Stack trace:
    #0 /path/to/project/vendor/squizlabs/php_codesniffer/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php(130): PHP_CodeSniffer\Runner->handleErrors()
    #1 /path/to/project/vendor/squizlabs/php_codesniffer/src/Files/File.php(498): PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionDeclarationSniff->process()
    #2 /path/to/project/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
    #3 /path/to/project/vendor/squizlabs/php_codesniffer/src/Fixer.php(174): PHP_CodeSniffer\Files\LocalFile->process()
    #4 /path/to/project/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php(52): PHP_CodeSniffer\Fixer->fixFile()
    #5 /path/to/project/vendor/squizlabs/php_codesniffer/src/Reporter.php(285): PHP_CodeSniffer\Reports\Cbf->generateFileReport()
    #6 /path/to/project/vendor/squizlabs/php_codesniffer/src/Runner.php(658): PHP_CodeSniffer\Reporter->cacheFileReport()
    #7 /path/to/project/vendor/squizlabs/php_codesniffer/src/Runner.php(438): PHP_CodeSniffer\Runner->processFile()
    #8 /path/to/project/vendor/squizlabs/php_codesniffer/src/Runner.php(204): PHP_CodeSniffer\Runner->run()
    #9 /path/to/project/vendor/squizlabs/php_codesniffer/bin/phpcbf(18): PHP_CodeSniffer\Runner->runPHPCBF()
    #10 /path/to/project/vendor/bin/phpcbf(120): include('...')
    #11 {main}
      thrown in /path/to/project/vendor/squizlabs/php_codesniffer/src/Runner.php on line 608
    

Expected behavior

Instead of throwing a Undefined array key -1 error, the output should indicate what is wrong with my PHP code so that I can fix it. In this case, it's missing either:

  • abstract and a semicolon
  • opening and closing curly braces

Alternatively, it should at least give me some hints to help me find the problem myself (e.g. tell me which line in the PHP file triggered the error - it currently incorrectly reports line 1) - with clean output in both phpcs and phpcbf

Versions (please complete the following information)

Operating System Ubuntu 22.04
PHP version 8.1.24
PHP_CodeSniffer version 3.7.2
Standard PSR12
Install type Composer (local)

Additional context

I found this while working on linting developer documentation using a wrapper I'm developing which passes in markdown php code blocks to be linted, which is why the example is a bit weird - but the instructions above all work with just regular old sniffer.

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@jrfnl
Copy link
Contributor

jrfnl commented Nov 3, 2023

@GuySartorelli Scanning code with parse errors is likely to always lead to problems. Most sniffs try their best to bow out silently when a parse error is encountered, but there are so many different type of parse errors/typos possible, it is hard to account for all situations and PHPCS shouldn't have to in the first place.

I'd recommend running a PHP linter over your code, like PHP Parallel Lint before passing it off to PHPCS. Alternatively you can include the Generic.PHP.Syntax sniff which will detect and report parse errors.

Please be aware that the results of most sniffs will not be reliable when your code has parse errors.

I'm going to suggest closing this issue as this is not an issue with PHPCS, but an issue with your code containing parse errors.

@GuySartorelli
Copy link
Author

GuySartorelli commented Nov 3, 2023

I'm going to suggest closing this issue as this is not an issue with PHPCS, but an issue with your code containing parse errors.

It seems to me that at the very least the way that PHPCS and PHPCBF display the error should be the same.

But it also seems reasonably to me that there would be a mechanism for sniffs to say "this is the line in the file I was up to" so that the error output can indicate which line in the file caused the error.

Even if not doing the above, it would be useful if there was a message that said something basic like "This may be the result of parsing errors in your PHP code - please check your PHP code, e.g. with a linter like PHP Parellel Lint"

@jrfnl
Copy link
Contributor

jrfnl commented Nov 3, 2023

It seems to me that at the very least the way that PHPCS and PHPCBF display the error should be the same.

PHPCS catches the error and displays the caught error on line 1 of the report for the file and will stop scanning the file.

As the report for PHPCBF doesn't display individual errors, the error can't be displayed in the report and is just thrown. There is already an issue open to improve that - see #2871 and #3642.

But it also seems reasonably to me that there would be a mechanism for sniffs to say "this is the line in the file I was up to" so that the error output can indicate which line in the file caused the error.

The error handler which catches the error does not have access to that information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants