Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e98b85d

Browse files
committed
Merge branch '2.2'
2 parents 5b874a0 + 7f1b755 commit e98b85d

2 files changed

Lines changed: 81 additions & 8 deletions

File tree

src/Tokenizer/Tokens.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,8 @@ public function clearEmptyTokens()
325325
public function ensureWhitespaceAtIndex($index, $indexOffset, $whitespace)
326326
{
327327
$removeLastCommentLine = function (Token $token, $indexOffset) {
328-
// because comments tokens are greedy and may consume single \n if we are putting whitespace after it let trim that \n
329-
if (1 === $indexOffset && $token->isComment()) {
330-
$token->setContent(preg_replace(
331-
"#\r\n$|\n$#",
332-
'',
333-
$token->getContent(),
334-
1
335-
));
328+
if (1 === $indexOffset && $token->isGivenKind(T_OPEN_TAG)) {
329+
$token->setContent(rtrim($token->getContent()));
336330
}
337331
};
338332

tests/Tokenizer/TokensTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,85 @@ public function assertFindBlockEnd($expectedIndex, $source, $type, $searchIndex)
859859
$this->assertFalse($detectedType['isStart']);
860860
}
861861

862+
/**
863+
* @param string $expected valid PHP code
864+
* @param string $input valid PHP code
865+
* @param int $index token index
866+
* @param int $offset
867+
* @param string $whiteSpace white space
868+
*
869+
* @dataProvider provideEnsureWhitespaceAtIndexCases
870+
*/
871+
public function testEnsureWhitespaceAtIndex($expected, $input, $index, $offset, $whiteSpace)
872+
{
873+
$tokens = Tokens::fromCode($input);
874+
$tokens->ensureWhitespaceAtIndex($index, $offset, $whiteSpace);
875+
876+
$this->assertSame($expected, $tokens->generateCode());
877+
}
878+
879+
public function provideEnsureWhitespaceAtIndexCases()
880+
{
881+
return [
882+
[
883+
'<?php ',
884+
'<?php',
885+
0,
886+
1,
887+
' ',
888+
],
889+
[
890+
"<?php\n",
891+
'<?php',
892+
0,
893+
1,
894+
"\n",
895+
],
896+
[
897+
"<?php\t",
898+
'<?php',
899+
0,
900+
1,
901+
"\t",
902+
],
903+
[
904+
'<?php
905+
//
906+
echo $a;',
907+
'<?php
908+
//
909+
echo $a;',
910+
2,
911+
1,
912+
"\n ",
913+
],
914+
[
915+
'<?php
916+
echo $a;',
917+
'<?php
918+
echo $a;',
919+
0,
920+
1,
921+
"\n ",
922+
],
923+
[
924+
'<?php
925+
echo $a;',
926+
'<?php echo $a;',
927+
0,
928+
1,
929+
"\n",
930+
],
931+
[
932+
"<?php\techo \$a;",
933+
'<?php echo $a;',
934+
0,
935+
1,
936+
"\t",
937+
],
938+
];
939+
}
940+
862941
/**
863942
* @param null|Token[] $expected
864943
* @param null|Token[] $input

0 commit comments

Comments
 (0)