PHPCSStandards / PHP_CodeSniffer

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

Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested match structures + minor efficiency tweak #513

Closed jrfnl closed 1 month ago

jrfnl commented 1 month ago

Description

Follow up on #497, see https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/497#pullrequestreview-2063014326

Generic/FunctionCallArgumentSpacing: efficiency fix - skip over arrow functions

PHP 7.4 introduced arrow functions.

While arrow functions being passed as an argument in a function call will not lead to false positives for this sniff - at least, I haven't been able to come up with a code sample in which it would [^1] -, skipping over them is still beneficial as it prevents unnecessary token walking.

Mind: the scope_closer of the arrow function may be the comma separating the arrow function argument from the next function call argument, so we need to step one token back to prevent false negatives on those comma's.

Either way, this is now handled.

Includes unit tests.

[^1]: comma's in arrow functions will always be nested within parentheses, within a short array or within a nested closure or anonymous class, all of which the sniff already ignores.

Generic/FunctionCallArgumentSpacing: bug fix - ignore commas in nested match structures

PHP 8.0 introduced match control structures, which can be passed in a function call (though probably/hopefully this is not very common as it makes for harder to read code).

The comma's within match control structures should be checked by a sniff which handled that control structure and should not be treated as comma's belonging to the function call.

As things are, this is currently not the case, which leads to false positives.

Fixed now.

Includes test.

Suggested changelog entry

Related issues/external references

Related to #497

Types of changes