Skip to content

Wrong indentation false positive with arrow function after array in function call #968

Closed

Description

@InvisibleSmiley

Describe the bug

Indentation is reported as off when it's not (false positive).
"Correcting" it produces an indentation-related error from another sniff (a valid one).

AFAICT the issue is triggered by the arrow function contained in the function call.

Code sample

<?php

namespace My;

class Example
{
    public static function test(): string
    {
        return json_encode(
            array_filter(
                [
                    'foo' => 42,
                    'bar' => null,
                ],
                static fn(mixed $value) => $value !== null
            ),
            JSON_THROW_ON_ERROR
        );
    }
}

Code example variant

<?php

namespace My;

class Example
{
    public static function test(): string
    {
        $value = array_filter(
            [
                'foo' => 42,
                'bar' => null,
            ],
            static fn(mixed $value) => $value !== null
        );
        return json_encode($value, JSON_THROW_ON_ERROR);
    }
}

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above
  2. Run phpcs test.php --standard=PSR12
  3. See error message displayed
FILE: /path/to/test.php
-----------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------
 17 | ERROR | [x] Line indented incorrectly; expected at least 16 spaces, found 12
-----------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------

Expected behavior

No errors/warnings

Versions (please complete the following information)

Operating System any (tried with MacOS 15.3.2)
PHP version any recent one (tried with 8.3.19)
PHP_CodeSniffer version 3.12.2, master
Standard PSR12
Install type Composer

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • 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.

Activity

  1. jrfnl commented on Apr 22, 2025

    @jrfnl
    Member

    @InvisibleSmiley Thanks for reporting this. The issue is reproducible and needs further investigation to see what's happening with this in the Generic.WhiteSpace.ScopeIndent sniff to find a fix.
    I can also confirm that the arrow function handling is most likely the problem, as changing the arrow function to a closure fixes the issue.

  2. added a commit that references this issue on Nov 4, 2025
    ff60d8a
  3. added this to the 4.0.1 milestone on Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions