Skip to content

Commit cda28ee

Browse files
authored
Use Tokens::ensureWhitespaceAtIndex to simplify code (#655)
1 parent b846a61 commit cda28ee

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

src/TokenRemover.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace PhpCsFixerCustomFixers;
1515

1616
use PhpCsFixer\Preg;
17-
use PhpCsFixer\Tokenizer\Token;
1817
use PhpCsFixer\Tokenizer\Tokens;
1918

2019
/**
@@ -90,13 +89,10 @@ private static function handleWhitespaceBefore(Tokens $tokens, int $index): bool
9089
}
9190
$contentWithoutTrailingSpaces = Preg::replace('/\h+$/', '', $tokens[$index]->getContent());
9291

92+
/** @var string $contentWithoutTrailingSpacesAndNewline */
9393
$contentWithoutTrailingSpacesAndNewline = Preg::replace('/\R$/', '', $contentWithoutTrailingSpaces, 1);
9494

95-
if ($contentWithoutTrailingSpacesAndNewline === '') {
96-
$tokens->clearAt($index);
97-
} else {
98-
$tokens[$index] = new Token([\T_WHITESPACE, $contentWithoutTrailingSpacesAndNewline]);
99-
}
95+
$tokens->ensureWhitespaceAtIndex($index, 0, $contentWithoutTrailingSpacesAndNewline);
10096

10197
return $contentWithoutTrailingSpaces !== $contentWithoutTrailingSpacesAndNewline;
10298
}
@@ -105,14 +101,9 @@ private static function handleWhitespaceAfter(Tokens $tokens, int $index, bool $
105101
{
106102
$pattern = $wasNewlineRemoved ? '/^\h+/' : '/^\h*\R/';
107103

104+
/** @var string $newContent */
108105
$newContent = Preg::replace($pattern, '', $tokens[$index]->getContent());
109106

110-
if ($newContent === '') {
111-
$tokens->clearAt($index);
112-
113-
return;
114-
}
115-
116-
$tokens[$index] = new Token([\T_WHITESPACE, $newContent]);
107+
$tokens->ensureWhitespaceAtIndex($index, 0, $newContent);
117108
}
118109
}

0 commit comments

Comments
 (0)