Skip to content

Commit 3a2a7a8

Browse files
authored
feature: NoTrailingCommaInSinglelineFixer - Introduction (#6529)
1 parent c59974e commit 3a2a7a8

18 files changed

+748
-196
lines changed

doc/list.rst

+18-3
Original file line numberDiff line numberDiff line change
@@ -1601,21 +1601,36 @@ List of Available Rules
16011601

16021602
Remove trailing commas in list function calls.
16031603

1604-
Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_
1604+
*warning deprecated* Use ``no_trailing_comma_in_singleline`` instead.
16051605

16061606
`Source PhpCsFixer\\Fixer\\ControlStructure\\NoTrailingCommaInListCallFixer <./../src/Fixer/ControlStructure/NoTrailingCommaInListCallFixer.php>`_
1607+
- `no_trailing_comma_in_singleline <./rules/basic/no_trailing_comma_in_singleline.rst>`_
1608+
1609+
If a list of values separated by a comma is contained on a single line, then the last item MUST NOT have a trailing comma.
1610+
1611+
Configuration options:
1612+
1613+
- | ``elements``
1614+
| Which elements to fix.
1615+
| Allowed values: a subset of ``['arguments', 'array_destructuring', 'array', 'group_import']``
1616+
| Default value: ``['arguments', 'array_destructuring', 'array', 'group_import']``
1617+
1618+
1619+
Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_
1620+
1621+
`Source PhpCsFixer\\Fixer\\Basic\\NoTrailingCommaInSinglelineFixer <./../src/Fixer/Basic/NoTrailingCommaInSinglelineFixer.php>`_
16071622
- `no_trailing_comma_in_singleline_array <./rules/array_notation/no_trailing_comma_in_singleline_array.rst>`_
16081623

16091624
PHP single-line arrays should not have trailing comma.
16101625

1611-
Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_
1626+
*warning deprecated* Use ``no_trailing_comma_in_singleline`` instead.
16121627

16131628
`Source PhpCsFixer\\Fixer\\ArrayNotation\\NoTrailingCommaInSinglelineArrayFixer <./../src/Fixer/ArrayNotation/NoTrailingCommaInSinglelineArrayFixer.php>`_
16141629
- `no_trailing_comma_in_singleline_function_call <./rules/function_notation/no_trailing_comma_in_singleline_function_call.rst>`_
16151630

16161631
When making a method or function call on a single line there MUST NOT be a trailing comma after the last argument.
16171632

1618-
Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_
1633+
*warning deprecated* Use ``no_trailing_comma_in_singleline`` instead.
16191634

16201635
`Source PhpCsFixer\\Fixer\\FunctionNotation\\NoTrailingCommaInSinglelineFunctionCallFixer <./../src/Fixer/FunctionNotation/NoTrailingCommaInSinglelineFunctionCallFixer.php>`_
16211636
- `no_trailing_whitespace <./rules/whitespace/no_trailing_whitespace.rst>`_

doc/ruleSets/Symfony.rst

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ Rules
6868
- `no_superfluous_phpdoc_tags <./../rules/phpdoc/no_superfluous_phpdoc_tags.rst>`_
6969
config:
7070
``['allow_mixed' => true, 'allow_unused_params' => true]``
71-
- `no_trailing_comma_in_list_call <./../rules/control_structure/no_trailing_comma_in_list_call.rst>`_
72-
- `no_trailing_comma_in_singleline_array <./../rules/array_notation/no_trailing_comma_in_singleline_array.rst>`_
73-
- `no_trailing_comma_in_singleline_function_call <./../rules/function_notation/no_trailing_comma_in_singleline_function_call.rst>`_
71+
- `no_trailing_comma_in_singleline <./../rules/basic/no_trailing_comma_in_singleline.rst>`_
7472
- `no_unneeded_control_parentheses <./../rules/control_structure/no_unneeded_control_parentheses.rst>`_
7573
config:
7674
``['statements' => ['break', 'clone', 'continue', 'echo_print', 'others', 'return', 'switch_case', 'yield', 'yield_from']]``

doc/rules/array_notation/no_trailing_comma_in_singleline_array.rst

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Rule ``no_trailing_comma_in_singleline_array``
44

55
PHP single-line arrays should not have trailing comma.
66

7+
Warning
8+
-------
9+
10+
This rule is deprecated and will be removed on next major version
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
You should use ``no_trailing_comma_in_singleline`` instead.
14+
715
Examples
816
--------
917

@@ -17,14 +25,3 @@ Example #1
1725
<?php
1826
-$a = array('sample', );
1927
+$a = array('sample');
20-
21-
Rule sets
22-
---------
23-
24-
The rule is part of the following rule sets:
25-
26-
@PhpCsFixer
27-
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_array`` rule.
28-
29-
@Symfony
30-
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_array`` rule.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
========================================
2+
Rule ``no_trailing_comma_in_singleline``
3+
========================================
4+
5+
If a list of values separated by a comma is contained on a single line, then the
6+
last item MUST NOT have a trailing comma.
7+
8+
Configuration
9+
-------------
10+
11+
``elements``
12+
~~~~~~~~~~~~
13+
14+
Which elements to fix.
15+
16+
Allowed values: a subset of ``['arguments', 'array_destructuring', 'array', 'group_import']``
17+
18+
Default value: ``['arguments', 'array_destructuring', 'array', 'group_import']``
19+
20+
Examples
21+
--------
22+
23+
Example #1
24+
~~~~~~~~~~
25+
26+
*Default* configuration.
27+
28+
.. code-block:: diff
29+
30+
--- Original
31+
+++ New
32+
<?php
33+
-foo($a,);
34+
-$foo = array(1,);
35+
-[$foo, $bar,] = $array;
36+
-use a\{ClassA, ClassB,};
37+
+foo($a);
38+
+$foo = array(1);
39+
+[$foo, $bar] = $array;
40+
+use a\{ClassA, ClassB};
41+
42+
Example #2
43+
~~~~~~~~~~
44+
45+
With configuration: ``['elements' => ['array_destructuring']]``.
46+
47+
.. code-block:: diff
48+
49+
--- Original
50+
+++ New
51+
<?php
52+
foo($a,);
53+
-[$foo, $bar,] = $array;
54+
+[$foo, $bar] = $array;
55+
56+
Rule sets
57+
---------
58+
59+
The rule is part of the following rule sets:
60+
61+
@PhpCsFixer
62+
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline`` rule with the default config.
63+
64+
@Symfony
65+
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline`` rule with the default config.

doc/rules/control_structure/no_trailing_comma_in_list_call.rst

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Rule ``no_trailing_comma_in_list_call``
44

55
Remove trailing commas in list function calls.
66

7+
Warning
8+
-------
9+
10+
This rule is deprecated and will be removed on next major version
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
13+
You should use ``no_trailing_comma_in_singleline`` instead.
14+
715
Examples
816
--------
917

@@ -17,14 +25,3 @@ Example #1
1725
<?php
1826
-list($a, $b,) = foo();
1927
+list($a, $b) = foo();
20-
21-
Rule sets
22-
---------
23-
24-
The rule is part of the following rule sets:
25-
26-
@PhpCsFixer
27-
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_trailing_comma_in_list_call`` rule.
28-
29-
@Symfony
30-
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_trailing_comma_in_list_call`` rule.

doc/rules/function_notation/no_trailing_comma_in_singleline_function_call.rst

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Rule ``no_trailing_comma_in_singleline_function_call``
55
When making a method or function call on a single line there MUST NOT be a
66
trailing comma after the last argument.
77

8+
Warning
9+
-------
10+
11+
This rule is deprecated and will be removed on next major version
12+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
14+
You should use ``no_trailing_comma_in_singleline`` instead.
15+
816
Examples
917
--------
1018

@@ -18,14 +26,3 @@ Example #1
1826
<?php
1927
-foo($a,);
2028
+foo($a);
21-
22-
Rule sets
23-
---------
24-
25-
The rule is part of the following rule sets:
26-
27-
@PhpCsFixer
28-
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_function_call`` rule.
29-
30-
@Symfony
31-
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_function_call`` rule.

doc/rules/index.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Array Notation
4848
- `no_multiline_whitespace_around_double_arrow <./array_notation/no_multiline_whitespace_around_double_arrow.rst>`_
4949

5050
Operator ``=>`` should not be surrounded by multi-line whitespaces.
51-
- `no_trailing_comma_in_singleline_array <./array_notation/no_trailing_comma_in_singleline_array.rst>`_
51+
- `no_trailing_comma_in_singleline_array <./array_notation/no_trailing_comma_in_singleline_array.rst>`_ *(deprecated)*
5252

5353
PHP single-line arrays should not have trailing comma.
5454
- `no_whitespace_before_comma_in_array <./array_notation/no_whitespace_before_comma_in_array.rst>`_
@@ -79,6 +79,9 @@ Basic
7979
- `no_multiple_statements_per_line <./basic/no_multiple_statements_per_line.rst>`_
8080

8181
There must not be more than one statement per line.
82+
- `no_trailing_comma_in_singleline <./basic/no_trailing_comma_in_singleline.rst>`_
83+
84+
If a list of values separated by a comma is contained on a single line, then the last item MUST NOT have a trailing comma.
8285
- `non_printable_character <./basic/non_printable_character.rst>`_ *(risky)*
8386

8487
Remove Zero-width space (ZWSP), Non-breaking space (NBSP) and other invisible unicode symbols.
@@ -269,7 +272,7 @@ Control Structure
269272
- `no_superfluous_elseif <./control_structure/no_superfluous_elseif.rst>`_
270273

271274
Replaces superfluous ``elseif`` with ``if``.
272-
- `no_trailing_comma_in_list_call <./control_structure/no_trailing_comma_in_list_call.rst>`_
275+
- `no_trailing_comma_in_list_call <./control_structure/no_trailing_comma_in_list_call.rst>`_ *(deprecated)*
273276

274277
Remove trailing commas in list function calls.
275278
- `no_unneeded_control_parentheses <./control_structure/no_unneeded_control_parentheses.rst>`_
@@ -352,7 +355,7 @@ Function Notation
352355
- `no_spaces_after_function_name <./function_notation/no_spaces_after_function_name.rst>`_
353356

354357
When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.
355-
- `no_trailing_comma_in_singleline_function_call <./function_notation/no_trailing_comma_in_singleline_function_call.rst>`_
358+
- `no_trailing_comma_in_singleline_function_call <./function_notation/no_trailing_comma_in_singleline_function_call.rst>`_ *(deprecated)*
356359

357360
When making a method or function call on a single line there MUST NOT be a trailing comma after the last argument.
358361
- `no_unreachable_default_argument_value <./function_notation/no_unreachable_default_argument_value.rst>`_ *(risky)*

src/Fixer/ArrayNotation/NoTrailingCommaInSinglelineArrayFixer.php

+12-40
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414

1515
namespace PhpCsFixer\Fixer\ArrayNotation;
1616

17-
use PhpCsFixer\AbstractFixer;
17+
use PhpCsFixer\AbstractProxyFixer;
18+
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
19+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1820
use PhpCsFixer\FixerDefinition\CodeSample;
1921
use PhpCsFixer\FixerDefinition\FixerDefinition;
2022
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21-
use PhpCsFixer\Tokenizer\CT;
22-
use PhpCsFixer\Tokenizer\Tokens;
23-
use PhpCsFixer\Tokenizer\TokensAnalyzer;
2423

2524
/**
25+
* @deprecated
26+
*
2627
* @author Dariusz Rumiński <[email protected]>
2728
* @author Sebastiaan Stok <[email protected]>
2829
*/
29-
final class NoTrailingCommaInSinglelineArrayFixer extends AbstractFixer
30+
final class NoTrailingCommaInSinglelineArrayFixer extends AbstractProxyFixer implements DeprecatedFixerInterface
3031
{
3132
/**
3233
* {@inheritdoc}
@@ -42,48 +43,19 @@ public function getDefinition(): FixerDefinitionInterface
4243
/**
4344
* {@inheritdoc}
4445
*/
45-
public function isCandidate(Tokens $tokens): bool
46+
public function getSuccessorsNames(): array
4647
{
47-
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
48+
return array_keys($this->proxyFixers);
4849
}
4950

5051
/**
5152
* {@inheritdoc}
5253
*/
53-
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
54-
{
55-
$tokensAnalyzer = new TokensAnalyzer($tokens);
56-
57-
for ($index = 0, $c = $tokens->count(); $index < $c; ++$index) {
58-
if ($tokensAnalyzer->isArray($index)) {
59-
$this->fixArray($tokens, $index);
60-
}
61-
}
62-
}
63-
64-
private function fixArray(Tokens $tokens, int $index): void
54+
protected function createProxyFixers(): array
6555
{
66-
$tokensAnalyzer = new TokensAnalyzer($tokens);
67-
68-
if ($tokensAnalyzer->isArrayMultiLine($index)) {
69-
return;
70-
}
71-
72-
$startIndex = $index;
73-
74-
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
75-
$startIndex = $tokens->getNextTokenOfKind($startIndex, ['(']);
76-
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
77-
} else {
78-
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
79-
}
80-
81-
$beforeEndIndex = $tokens->getPrevMeaningfulToken($endIndex);
82-
$beforeEndToken = $tokens[$beforeEndIndex];
56+
$fixer = new NoTrailingCommaInSinglelineFixer();
57+
$fixer->configure(['elements' => ['array']]);
8358

84-
if ($beforeEndToken->equals(',')) {
85-
$tokens->removeTrailingWhitespace($beforeEndIndex);
86-
$tokens->clearAt($beforeEndIndex);
87-
}
59+
return [$fixer];
8860
}
8961
}

0 commit comments

Comments
 (0)