Skip to content

Commit 928c733

Browse files
authored
Deprecate NoUselessSprintfFixer (#426)
1 parent 787a1fd commit 928c733

File tree

5 files changed

+31
-74
lines changed

5 files changed

+31
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v2.4.0 - [Unreleased]
44
- Update PHP CS Fixer to v2.17
5+
- Deprecate NoUselessSprintfFixer - use "no_useless_sprintf"
56
- Deprecate OperatorLinebreakFixer - use "operator_linebreak"
67
- Remove PhpCsFixerCustomFixers\Analyzer\ReferenceAnalyzer - use PhpCsFixer\Tokenizer\Analyzer\ReferenceAnalyzer
78
- Remove PhpCsFixerCustomFixers\Analyzer\SwitchAnalyzer - use PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ There must be no useless parenthesis.
281281

282282
#### NoUselessSprintfFixer
283283
Function `sprintf` without parameters should not be used.
284+
DEPRECATED: use `no_useless_sprintf` instead.
284285
*Risky: when the function `sprintf` is overridden.*
285286
```diff
286287
<?php

src/Fixer/NoUselessSprintfFixer.php

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@
1313

1414
namespace PhpCsFixerCustomFixers\Fixer;
1515

16+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1617
use PhpCsFixer\FixerDefinition\CodeSample;
1718
use PhpCsFixer\FixerDefinition\FixerDefinition;
1819
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
19-
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
20-
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
21-
use PhpCsFixer\Tokenizer\Token;
2220
use PhpCsFixer\Tokenizer\Tokens;
2321

24-
final class NoUselessSprintfFixer extends AbstractFixer
22+
/**
23+
* @deprecated
24+
*/
25+
final class NoUselessSprintfFixer extends AbstractFixer implements DeprecatedFixerInterface
2526
{
27+
/** @var \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer */
28+
private $fixer;
29+
30+
public function __construct()
31+
{
32+
$this->fixer = new \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer();
33+
}
34+
2635
public function getDefinition(): FixerDefinitionInterface
2736
{
2837
return new FixerDefinition(
@@ -35,79 +44,29 @@ public function getDefinition(): FixerDefinitionInterface
3544

3645
public function getPriority(): int
3746
{
38-
return 0;
47+
return $this->fixer->getPriority();
3948
}
4049

4150
public function isCandidate(Tokens $tokens): bool
4251
{
43-
return $tokens->isTokenKindFound(T_STRING);
52+
return $this->fixer->isCandidate($tokens);
4453
}
4554

4655
public function isRisky(): bool
4756
{
48-
return true;
57+
return $this->fixer->isRisky();
4958
}
5059

5160
public function fix(\SplFileInfo $file, Tokens $tokens): void
5261
{
53-
$argumentsAnalyzer = new ArgumentsAnalyzer();
54-
$functionsAnalyzer = new FunctionsAnalyzer();
55-
56-
for ($index = $tokens->count() - 1; $index > 0; $index--) {
57-
/** @var Token $token */
58-
$token = $tokens[$index];
59-
60-
if (!$token->equals([T_STRING, 'sprintf'], false)) {
61-
continue;
62-
}
63-
64-
if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
65-
continue;
66-
}
67-
68-
/** @var int $openParenthesisIndex */
69-
$openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(']);
70-
71-
$closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
72-
73-
if ($argumentsAnalyzer->countArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex) !== 1) {
74-
continue;
75-
}
76-
77-
/** @var int $afterOpenParenthesisIndex */
78-
$afterOpenParenthesisIndex = $tokens->getNextMeaningfulToken($openParenthesisIndex);
79-
80-
/** @var Token $afterOpenParenthesisToken */
81-
$afterOpenParenthesisToken = $tokens[$afterOpenParenthesisIndex];
82-
83-
if ($afterOpenParenthesisToken->isGivenKind(T_ELLIPSIS)) {
84-
continue;
85-
}
86-
87-
/** @var int $prevIndex */
88-
$prevIndex = $tokens->getPrevMeaningfulToken($index);
89-
90-
/** @var Token $prevToken */
91-
$prevToken = $tokens[$prevIndex];
92-
93-
if ($prevToken->isGivenKind(T_NS_SEPARATOR)) {
94-
$this->removeTokenAndSiblingWhitespace($tokens, $prevIndex, 1);
95-
}
96-
$this->removeTokenAndSiblingWhitespace($tokens, $index, 1);
97-
$this->removeTokenAndSiblingWhitespace($tokens, $openParenthesisIndex, 1);
98-
$this->removeTokenAndSiblingWhitespace($tokens, $closeParenthesisIndex, -1);
99-
}
62+
$this->fixer->fix($file, $tokens);
10063
}
10164

102-
private function removeTokenAndSiblingWhitespace(Tokens $tokens, int $index, int $direction): void
65+
/**
66+
* @return string[]
67+
*/
68+
public function getSuccessorsNames(): array
10369
{
104-
$tokens->clearAt($index);
105-
106-
/** @var Token $siblingToken */
107-
$siblingToken = $tokens[$index + $direction];
108-
109-
if ($siblingToken->isWhitespace()) {
110-
$tokens->clearAt($index + $direction);
111-
}
70+
return [$this->fixer->getName()];
11271
}
11372
}

tests/Fixer/AbstractFixerTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final public function testFixerDefinitionRiskyDescriptionDoesNotEndWithDot(): vo
7272
final public function testFixerDefinitionHasExactlyOneCodeSample(): void
7373
{
7474
if ($this->fixer instanceof DeprecatedFixerInterface) {
75-
self::assertGreaterThan(1, \count($this->fixer->getDefinition()->getCodeSamples()));
75+
self::assertGreaterThanOrEqual(1, \count($this->fixer->getDefinition()->getCodeSamples()));
7676
} else {
7777
self::assertCount(1, $this->fixer->getDefinition()->getCodeSamples());
7878
}

tests/Fixer/NoUselessSprintfFixerTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function testIsRisky(): void
2525
self::assertTrue($this->fixer->isRisky());
2626
}
2727

28+
public function testSuccessorName(): void
29+
{
30+
self::assertContains('no_useless_sprintf', $this->fixer->getSuccessorsNames());
31+
}
32+
2833
/**
2934
* @dataProvider provideFixCases
3035
*/
@@ -72,15 +77,6 @@ public static function provideFixCases(): iterable
7277
'<?php sprintf(sprintf(sprintf($foo), 7));',
7378
];
7479

75-
yield [
76-
'<?php
77-
$foo = "Foo";',
78-
'<?php
79-
$foo = sprintf(
80-
"Foo"
81-
);',
82-
];
83-
8480
yield [
8581
'<?php
8682
$foo = "Foo";
@@ -100,7 +96,7 @@ public static function provideFixCases(): iterable
10096

10197
if (PHP_MAJOR_VERSION < 8) {
10298
yield [
103-
'<?php $foo ;',
99+
'<?php $foo ;',
104100
'<?php \ sprintf ( $foo ) ;',
105101
];
106102
}

0 commit comments

Comments
 (0)