Skip to content

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

Open
@InvisibleSmiley

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.

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