Skip to content

Commit 14cd569

Browse files
authored
NoUselessSprintfFixer - fix for whitespaces (#165)
1 parent aff40bd commit 14cd569

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
1010
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
11-
![Tests](https://img.shields.io/badge/tests-1351-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-1352-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
1414

src/Fixer/NoUselessSprintfFixer.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function getDefinition(): FixerDefinitionInterface
2323
);
2424
}
2525

26+
public function getPriority(): int
27+
{
28+
return 0;
29+
}
30+
2631
public function isCandidate(Tokens $tokens): bool
2732
{
2833
return $tokens->isTokenKindFound(T_STRING);
@@ -56,24 +61,22 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5661
continue;
5762
}
5863

64+
/** @var int $prevIndex */
5965
$prevIndex = $tokens->getPrevMeaningfulToken($index);
6066
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
61-
$tokens->clearAt($prevIndex);
67+
$this->removeTokenAndSiblingWhitespace($tokens, $prevIndex, 1);
6268
}
63-
$tokens->clearAt($index);
64-
$tokens->clearAt($openParenthesis);
65-
if ($tokens[$openParenthesis + 1]->isWhitespace()) {
66-
$tokens->clearAt($openParenthesis + 1);
67-
}
68-
if ($tokens[$closeParenthesis - 1]->isWhitespace()) {
69-
$tokens->clearAt($closeParenthesis - 1);
70-
}
71-
$tokens->clearAt($closeParenthesis);
69+
$this->removeTokenAndSiblingWhitespace($tokens, $index, 1);
70+
$this->removeTokenAndSiblingWhitespace($tokens, $openParenthesis, 1);
71+
$this->removeTokenAndSiblingWhitespace($tokens, $closeParenthesis, -1);
7272
}
7373
}
7474

75-
public function getPriority(): int
75+
private function removeTokenAndSiblingWhitespace(Tokens $tokens, int $index, int $direction): void
7676
{
77-
return 0;
77+
$tokens->clearAt($index);
78+
if ($tokens[$index + $direction]->isWhitespace()) {
79+
$tokens->clearAt($index + $direction);
80+
}
7881
}
7982
}

tests/Fixer/NoUselessSprintfFixerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function provideFixCases(): iterable
5555
'<?php \sprintf($foo);',
5656
];
5757

58+
yield [
59+
'<?php $foo ;',
60+
'<?php \ sprintf ( $foo ) ;',
61+
];
62+
5863
yield [
5964
'<?php $foo;',
6065
'<?php SPRINTF($foo);',

0 commit comments

Comments
 (0)