-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #6304 NoTrailingCommaInSinglelineFunctionCallFixer - Introduc…
…tion (SpacePossum) This PR was merged into the master branch. Discussion ---------- NoTrailingCommaInSinglelineFunctionCallFixer - Introduction closes #6141 Commits ------- cb4c02a NoTrailingCommaInSinglelineFunctionCallFixer - Introduction
- Loading branch information
Showing
16 changed files
with
530 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
doc/rules/function_notation/no_trailing_comma_in_singleline_function_call.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
====================================================== | ||
Rule ``no_trailing_comma_in_singleline_function_call`` | ||
====================================================== | ||
|
||
When making a method or function call on a single line there MUST NOT be a | ||
trailing comma after the last argument. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
-foo($a,); | ||
+foo($a); | ||
Rule sets | ||
--------- | ||
|
||
The rule is part of the following rule sets: | ||
|
||
@PhpCsFixer | ||
Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_function_call`` rule. | ||
|
||
@Symfony | ||
Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``no_trailing_comma_in_singleline_function_call`` rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/Fixer/FunctionNotation/NoTrailingCommaInSinglelineFunctionCallFixer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\FunctionNotation; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoTrailingCommaInSinglelineFunctionCallFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'When making a method or function call on a single line there MUST NOT be a trailing comma after the last argument.', | ||
[new CodeSample("<?php\nfoo(\$a,);\n")] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before NoSpacesInsideParenthesisFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 3; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAnyTokenKindsFound([T_STRING, T_VARIABLE, T_CLASS, T_UNSET, T_ISSET, T_LIST]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = \count($tokens) - 1; $index > 0; --$index) { | ||
if (!$tokens[$index]->equals(')')) { | ||
continue; | ||
} | ||
|
||
$trailingCommaIndex = $tokens->getPrevMeaningfulToken($index); | ||
|
||
if (!$tokens[$trailingCommaIndex]->equals(',')) { | ||
continue; | ||
} | ||
|
||
$callIndex = $tokens->getPrevMeaningfulToken( // get before "parenthesis open index" | ||
$tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index) | ||
); | ||
|
||
if ($tokens[$callIndex]->isGivenKind([T_VARIABLE, T_CLASS, T_UNSET, T_ISSET, T_LIST])) { | ||
$this->clearCommaIfNeeded($tokens, $callIndex, $index, $trailingCommaIndex); | ||
|
||
continue; | ||
} | ||
|
||
if ($tokens[$callIndex]->isGivenKind(T_STRING)) { | ||
if (!AttributeAnalyzer::isAttribute($tokens, $callIndex)) { | ||
$this->clearCommaIfNeeded($tokens, $callIndex, $index, $trailingCommaIndex); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if ($tokens[$callIndex]->equalsAny([')', ']', [CT::T_DYNAMIC_VAR_BRACE_CLOSE], [CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE]])) { | ||
$block = Tokens::detectBlockType($tokens[$callIndex]); | ||
if ( | ||
Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE === $block['type'] | ||
|| Tokens::BLOCK_TYPE_DYNAMIC_VAR_BRACE === $block['type'] | ||
|| Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE === $block['type'] | ||
|| Tokens::BLOCK_TYPE_PARENTHESIS_BRACE === $block['type'] | ||
) { | ||
$this->clearCommaIfNeeded($tokens, $callIndex, $index, $trailingCommaIndex); | ||
|
||
// continue; implicit | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function clearCommaIfNeeded(Tokens $tokens, int $startIndex, int $endIndex, int $commaIndex): void | ||
{ | ||
if (!$tokens->isPartialCodeMultiline($startIndex, $endIndex)) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($commaIndex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.