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 array key in Generic.WhiteSpace.ScopeIndent #437

Closed
3 tasks done
SunflowerFuchs opened this issue Apr 8, 2024 · 3 comments · Fixed by #502
Closed
3 tasks done

Undefined array key in Generic.WhiteSpace.ScopeIndent #437

SunflowerFuchs opened this issue Apr 8, 2024 · 3 comments · Fixed by #502

Comments

@SunflowerFuchs
Copy link

Describe the bug

When checking a specific file (see minimal code sample), phpcs throws an undefined array key error.

Code sample

<?php

match (true) {
    default => [
        'unrelated' => '',
        'example' => array_filter(
            array_map(
                function () {
                    return null;
                },
                []
            )
        )
    ]
};

(Yes, i know the code is very convoluted, it's very much an edge-case)
Interestingly enough, the unrelated array key is relevant, if removed then the error doesn't occur anymore.

Custom ruleset

No custom ruleset defined.

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php
  3. See error message displayed
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: Undefined array key 68 in /app/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php on line 1481
|       | The error originated in the Generic.WhiteSpace.ScopeIndent sniff on line 1481. (Internal.Exception)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Expected behavior

Show an understandable/fixable error, instead of a generic error coming from inside of phpcs.

Versions

Operating System Linux 6.6 (in docker)
PHP version 8.1 & 8.3
PHP_CodeSniffer version 3.8.1 & 3.9.1
Standard whatever the default is (PEAR?)
Install type composer

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.
@DannyvdSluijs
Copy link
Contributor

I can confirm this is reproducible on the current master branch (commit b0ecdf1).

The line causing the error can be seen at
https://github.com/DannyvdSluijs/PHP_CodeSniffer_Continuation/blob/master/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php#L1481

At frist glance there is no obvious culprit and the same issue can be reproduced using version 3.7.1, 3.6.2. Using git bisect it seems that 97f7898 is causing the issue which was aimed to solve squizlabs/PHP_CodeSniffer#3287

@jrfnl
Copy link
Member

jrfnl commented May 19, 2024

I've been digging into this one and it looks like it is a pretty complex bug in the File::findStartOfStatement() method related to the handling of match expressions, which is causing this behaviour.

Currently working on fixing that bug. Once that bug is fixed, I'll have to see if the reported issue can still be reproduced and if there is anything within the ScopeIndent sniff, which would still need fixing.

jrfnl added a commit that referenced this issue May 19, 2024
jrfnl added a commit that referenced this issue May 19, 2024
jrfnl added a commit that referenced this issue May 19, 2024
Both the mentioned issues are fixed by the improvements to the `File::findStartOfStatement()` method in this same PR.

Fixes #110
Fixes #437
Fixes squizlabs/PHP_CodeSniffer 3875
@jrfnl jrfnl self-assigned this May 19, 2024
@jrfnl jrfnl added this to the 3.10.x Next milestone May 19, 2024
@jrfnl
Copy link
Member

jrfnl commented May 19, 2024

Pff.. this was complex one. PR #502 should fix it though. Testing appreciated.

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