Skip to content

Commit

Permalink
✨ New Universal.WhiteSpace.CommaSpacing sniff
Browse files Browse the repository at this point in the history
New sniff to enforce no space before a comma and exactly one space, or a new line, after a comma.
Additionally,  the sniff also enforces that the comma should follow the code and not be placed after a trailing comment.

For the spacing part, the sniff makes the following exceptions:
1. A comma preceded or followed by a parenthesis, curly or square bracket.
    These will not be flagged to prevent conflicts with sniffs handling spacing around braces.
2. A comma preceded or followed by another comma, like for skipping items in a list assignment.
    These will not be flagged.
3. A comma preceded by a non-indented heredoc/nowdoc closer.
    In that case, unless the `php_version` config directive is set to a version higher than PHP 7.3.0, a new line before will be enforced to prevent parse errors on PHP < 7.3.

The sniff has a separate error code for when a comma is found with more than one space after it, followed by a trailing comment.
That way trailing comment alignment can be allowed by excluding that error code.

Additionally, the sniff uses modular error code suffixes for select situations, like `*InFunctionDeclaration`, `*InFunctionCall`, to allow for preventing duplicate messages if another sniff is already handling the comma spacing.

Includes raising the minimum PHPCSUtils version to `1.0.8` to prevent running into a particular bug in the `SpacesFixer`.

Note: a few of the test files will only run when the tests are run on PHP 7.3+ as the tests involve PHP 7.3+ flexible heredoc/nowdoc tokens, which is the one syntax which PHPCS cannot polyfill in the Tokenizer.

Includes fixers.
Includes unit tests.
Includes documentation.
Includes metrics.
  • Loading branch information
jrfnl committed Jul 18, 2023
1 parent dd63fe7 commit 3fc4ac2
Show file tree
Hide file tree
Showing 27 changed files with 1,816 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Minimum Requirements

* PHP 5.4 or higher.
* [PHP_CodeSniffer][phpcs-gh] version **3.7.1** or higher.
* [PHPCSUtils][phpcsutils-gh] version **1.0.7** or higher.
* [PHPCSUtils][phpcsutils-gh] version **1.0.8** or higher.


Installation
Expand Down
94 changes: 94 additions & 0 deletions Universal/Docs/WhiteSpace/CommaSpacingStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Comma Spacing"
>
<standard>
<![CDATA[
There should be no space before a comma and exactly one space, or a new line, after a comma.
The sniff makes the following exceptions to this rule:
1. A comma preceded or followed by a parenthesis, curly or square bracket.
These will not be flagged to prevent conflicts with sniffs handling spacing around braces.
2. A comma preceded or followed by another comma, like for skipping items in a list assignment.
These will not be flagged.
3. A comma preceded by a non-indented heredoc/nowdoc closer.
In that case, unless the `php_version` config directive is set to a version higher than PHP 7.3.0,
a new line before will be enforced to prevent parse errors on PHP < 7.3.
]]>
</standard>
<code_comparison>
<code title="Valid: Correct spacing.">
<![CDATA[
isset($param1<em>, </em>$param2<em>, </em>$param3);
function_call(
$param1<em>,</em>
$param2<em>,</em>
$param3
);
$array = array($item1<em>, </em>$item2<em>, </em>$item3);
$array = [
$item1<em>,</em>
$item2<em>,</em>
];
list(, $a<em>, </em>$b<em>,</em>,) = $array;
list(
,
$a<em>,</em>
$b<em>,</em>
) = $array;
]]>
</code>
<code title="Invalid: Incorrect spacing.">
<![CDATA[
unset($param1<em> , </em>$param2<em>,</em>$param3);
function_call(
$a
<em>,</em>$b
<em>,</em>$c
);
$array = array($item1<em>,</em>$item2<em> , </em>$item3);
$array = [
$item1,
$item2<em> ,</em>
];
list( ,$a, $b<em> ,</em>,) = $array;
list(
,
$a,
$b<em> ,</em>
) = $array;
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
A comma should follow the code and not be placed after a trailing comment.
]]>
</standard>
<code_comparison>
<code title="Valid: Comma after the code.">
<![CDATA[
function_call(
$param1<em>,</em> // Comment.
$param2<em>,</em> /* Comment. */
);
]]>
</code>
<code title="Invalid: Comma after a trailing comment.">
<![CDATA[
function_call(
$param1 // Comment.
<em>,</em>
$param2 /* Comment. */<em>,</em>
);
]]>
</code>
</code_comparison>
</documentation>
Loading

0 comments on commit 3fc4ac2

Please sign in to comment.