Skip to content
Permalink
Browse files

NoUselessSprintfFixer - fix for whitespaces (#165)

  • Loading branch information...
kubawerlos committed Oct 23, 2019
1 parent aff40bd commit 14cd56910e8a393779500dbc5c90340ec313fe93
Showing with 21 additions and 13 deletions.
  1. +1 −1 README.md
  2. +15 −12 src/Fixer/NoUselessSprintfFixer.php
  3. +5 −0 tests/Fixer/NoUselessSprintfFixerTest.php
@@ -8,7 +8,7 @@

[![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)
[![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)
![Tests](https://img.shields.io/badge/tests-1351-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1352-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![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)

@@ -23,6 +23,11 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
@@ -56,24 +61,22 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}
/** @var int $prevIndex */
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearAt($prevIndex);
$this->removeTokenAndSiblingWhitespace($tokens, $prevIndex, 1);
}
$tokens->clearAt($index);
$tokens->clearAt($openParenthesis);
if ($tokens[$openParenthesis + 1]->isWhitespace()) {
$tokens->clearAt($openParenthesis + 1);
}
if ($tokens[$closeParenthesis - 1]->isWhitespace()) {
$tokens->clearAt($closeParenthesis - 1);
}
$tokens->clearAt($closeParenthesis);
$this->removeTokenAndSiblingWhitespace($tokens, $index, 1);
$this->removeTokenAndSiblingWhitespace($tokens, $openParenthesis, 1);
$this->removeTokenAndSiblingWhitespace($tokens, $closeParenthesis, -1);
}
}
public function getPriority(): int
private function removeTokenAndSiblingWhitespace(Tokens $tokens, int $index, int $direction): void
{
return 0;
$tokens->clearAt($index);
if ($tokens[$index + $direction]->isWhitespace()) {
$tokens->clearAt($index + $direction);
}
}
}
@@ -55,6 +55,11 @@ public function provideFixCases(): iterable
'<?php \sprintf($foo);',
];
yield [
'<?php $foo ;',
'<?php \ sprintf ( $foo ) ;',
];
yield [
'<?php $foo;',
'<?php SPRINTF($foo);',

0 comments on commit 14cd569

Please sign in to comment.
You can’t perform that action at this time.