PHP_CodeSniffer
PHP_CodeSniffer copied to clipboard
Wrong indentation false positive with arrow function after array in function call
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:
- Create a file called
test.phpwith the code sample above - Run
phpcs test.php --standard=PSR12 - 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
- [x] I have searched the issue list and am not opening a duplicate issue.
- [x] I have read the Contribution Guidelines and this is not a support question.
- [x] I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- [x] I have verified the issue still exists in the
masterbranch of PHP_CodeSniffer.
@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.