New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NoTrailingCommaInSinglelineFunctionCallFixer - Introduction #6304
Merged
SpacePossum
merged 1 commit into
PHP-CS-Fixer:master
from
SpacePossum:master_NoTrailingCommaInSinglelineFunctionCallFixer
Feb 21, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this way, not:
as it's done in
NoTrailingCommaInSinglelineArrayFixer
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I follow, what would be the benefit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency, better fixing - now code:
would be changed to:
I cannot imagine anyone want to have the comma removed and keep it's trailing whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check, I didn't even know
removeTrailingWhitespace
existed, I think your case is covered through single-responsibility of the rule combined with priorities, but if there is more room for improvement than lets PR it.