squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.62k stars 1.48k forks source link

Arrow functions not indented properly #3321

Open martinssipenko opened 3 years ago

martinssipenko commented 3 years ago

Describe the bug

I'm not 100% sure whether this is an actual bug, but I decided to create a ticket anyway.

I think sniffer should fail in case run on following code:

Code sample

$foo = fn() =>
new DateTime();

var_dump($foo);

Expected behavior

The "body" of the arrow function should be indented by one level:

$foo = fn() =>
    new DateTime();

var_dump($foo);

Versions (please complete the following information):

gsherwood commented 3 years ago

As arrow functions did not exist when any of the included standards were written, there are no rules that define how they should be formatted for all the various ways they can be used.

I could add a rule that if the statement part is on a new line then it must be on the line directly after, and indented one level, but then I'm writing new rules into a standard that I don't own.

What this really needs is a specific sniff for arrow functions that can be included into custom standards, but I don't tend to do that unless a big standard or project has already defined a formatting style so that I'm not supporting code for only a small number of users. It's quite possible, for example, that a big project comes out and says that arrow functions must be defined on a single line, in which case adding a rule for that may be worthwhile, but doesn't help with your use case.

I'll leave this as a feature request so it can be discussed, but PHPCS is working as expected for now.