-
-
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 #5280 NoUselessSprintfFixer - Introduction (SpacePossum)
This PR was merged into the 2.17-dev branch. Discussion ---------- NoUselessSprintfFixer - Introduction Commits ------- ecd1525 NoUselessSprintfFixer - Introduction
- Loading branch information
Showing
15 changed files
with
335 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
=========================== | ||
Rule ``no_useless_sprintf`` | ||
=========================== | ||
|
||
There must be no ``sprintf`` calls with only the first argument. | ||
|
||
.. warning:: Using this rule is risky. | ||
|
||
Risky when if the ``sprintf`` function is overridden. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
@@ -1,2 +1,2 @@ | ||
<?php | ||
-$foo = sprintf('bar'); | ||
+$foo = 'bar'; | ||
Rule sets | ||
--------- | ||
|
||
The rule is part of the following rule sets: | ||
|
||
@PhpCsFixer:risky | ||
Using the `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_ rule set will enable the ``no_useless_sprintf`` rule. | ||
|
||
@Symfony:risky | ||
Using the `@Symfony:risky <./../../ruleSets/SymfonyRisky.rst>`_ rule set will enable the ``no_useless_sprintf`` 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/* | ||
* 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\Tokenizer\Analyzer\ArgumentsAnalyzer; | ||
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoUselessSprintfFixer extends AbstractFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition() | ||
{ | ||
return new FixerDefinition( | ||
'There must be no `sprintf` calls with only the first argument.', | ||
[ | ||
new CodeSample( | ||
"<?php\n\$foo = sprintf('bar');\n" | ||
), | ||
], | ||
null, | ||
'Risky when if the `sprintf` function is overridden.' | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isTokenKindFound(T_STRING); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isRisky() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before MethodArgumentSpaceFixer, NativeFunctionCasingFixer, NoExtraBlankLinesFixer, NoSpacesInsideParenthesisFixer. | ||
*/ | ||
public function getPriority() | ||
{ | ||
return 3; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
$functionAnalyzer = new FunctionsAnalyzer(); | ||
$argumentsAnalyzer = new ArgumentsAnalyzer(); | ||
|
||
for ($index = \count($tokens) - 1; $index > 0; --$index) { | ||
if (!$tokens[$index]->isGivenKind(T_STRING)) { | ||
continue; | ||
} | ||
|
||
if ('sprintf' !== strtolower($tokens[$index]->getContent())) { | ||
continue; | ||
} | ||
|
||
if (!$functionAnalyzer->isGlobalFunctionCall($tokens, $index)) { | ||
continue; | ||
} | ||
|
||
$openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(']); | ||
|
||
if ($tokens[$tokens->getNextMeaningfulToken($openParenthesisIndex)]->isGivenKind(T_ELLIPSIS)) { | ||
continue; | ||
} | ||
|
||
$closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex); | ||
|
||
if (1 !== $argumentsAnalyzer->countArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex)) { | ||
continue; | ||
} | ||
|
||
$tokens->clearTokenAndMergeSurroundingWhitespace($closeParenthesisIndex); | ||
|
||
$prevMeaningfulTokenIndex = $tokens->getPrevMeaningfulToken($closeParenthesisIndex); | ||
|
||
if ($tokens[$prevMeaningfulTokenIndex]->equals(',')) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($prevMeaningfulTokenIndex); | ||
} | ||
|
||
$tokens->clearTokenAndMergeSurroundingWhitespace($openParenthesisIndex); | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($index); | ||
|
||
$prevMeaningfulTokenIndex = $tokens->getPrevMeaningfulToken($index); | ||
|
||
if ($tokens[$prevMeaningfulTokenIndex]->isGivenKind(T_NS_SEPARATOR)) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($prevMeaningfulTokenIndex); | ||
} | ||
} | ||
} | ||
} |
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
119 changes: 119 additions & 0 deletions
119
tests/Fixer/FunctionNotation/NoUselessSprintfFixerTest.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,119 @@ | ||
<?php | ||
|
||
/* | ||
* 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\Tests\Fixer\FunctionNotation; | ||
|
||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer | ||
*/ | ||
final class NoUselessSprintfFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @dataProvider provideFixCases | ||
* | ||
* @param string $expected | ||
* @param null|string $input | ||
*/ | ||
public function testFix($expected, $input = null) | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideFixCases() | ||
{ | ||
yield 'simple' => [ | ||
'<?php echo "bar";', | ||
'<?php echo sprintf("bar");', | ||
]; | ||
|
||
yield 'simple II' => [ | ||
"<?php echo 'bar' ?>", | ||
"<?php echo sprintf('bar') ?>", | ||
]; | ||
|
||
yield 'simple III' => [ | ||
'<?php echo $bar;', | ||
'<?php echo sprintf($bar);', | ||
]; | ||
|
||
yield 'simple IV' => [ | ||
'<?php echo 1;', | ||
'<?php echo sprintf(1);', | ||
]; | ||
|
||
yield 'minimal' => [ | ||
'<?php $foo;', | ||
'<?php sprintf($foo);', | ||
]; | ||
|
||
yield 'case insensitive' => [ | ||
'<?php echo "bar";', | ||
'<?php echo SPRINTF("bar");', | ||
]; | ||
|
||
yield 'nested' => [ | ||
'<?php echo /* test */"bar";', | ||
'<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));', | ||
]; | ||
|
||
yield [ | ||
'<?php namespace Foo { | ||
function sprintf($foo) { | ||
echo $foo; | ||
} | ||
}', | ||
]; | ||
|
||
yield [ | ||
'<?php namespace Foo; | ||
function sprintf($foo) { | ||
echo $foo; | ||
} | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
echo \Foo\sprintf("abc"); | ||
echo $bar->sprintf($foo); | ||
echo Bar::sprintf($foo); | ||
echo sprint(...$foo); | ||
echo sprint("%d", 1); | ||
echo sprint("%d%d%d", 1, 2, 3); | ||
echo sprint(); | ||
echo sprint[2]("foo"); | ||
', | ||
]; | ||
|
||
if (\PHP_VERSION_ID > 70300) { | ||
yield 'trailing comma' => [ | ||
'<?php echo "bar";', | ||
'<?php echo sprintf("bar",);', | ||
]; | ||
} | ||
|
||
if (\PHP_VERSION_ID < 80000) { | ||
yield [ | ||
'<?php echo "bar";', | ||
'<?php echo \ sprintf("bar");', | ||
]; | ||
|
||
yield [ | ||
'<?php echo A /* 1 */ \ B \ sprintf("bar");', | ||
]; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Fixtures/Integration/priority/no_useless_sprintf,method_argument_space.test
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,11 @@ | ||
--TEST-- | ||
Integration of fixers: no_useless_sprintf,method_argument_space. | ||
--RULESET-- | ||
{"no_useless_sprintf": true, "method_argument_space": true} | ||
--REQUIREMENTS-- | ||
{"php": 70300} | ||
--EXPECT-- | ||
<?php $a = $a ; | ||
|
||
--INPUT-- | ||
<?php $a = sprintf( $a ,); |
Oops, something went wrong.