1414namespace PhpCsFixerCustomFixers ;
1515
1616use PhpCsFixer \Preg ;
17- use PhpCsFixer \Tokenizer \Token ;
1817use 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