Skip to content

Undefined index in FunctionDeclarationSniff #3917

@GuySartorelli

Description

@GuySartorelli

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions