From eebaa946470b5b5fada91ac39a8bff22752edc89 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 22 Sep 2018 10:09:00 +0200 Subject: [PATCH] Flexible doc strings: Fix some issues, add more tests --- lib/PhpParser/ParserAbstract.php | 27 +-- test/PhpParser/ParserTest.php | 27 +-- .../code/parser/scalar/flexibleDocString.test | 181 ++++++++++++++++++ 3 files changed, 212 insertions(+), 23 deletions(-) diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index d85ec243..8b7bba83 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -802,20 +802,25 @@ abstract class ParserAbstract implements Parser ); } - foreach ($contents as $i => $s) { - if ($s instanceof Node\Scalar\EncapsedStringPart) { - $s->value = $this->stripIndentation( - $s->value, $indentLen, $indentChar, - $i === 0, $i === \count($contents) - 1, $s->getAttributes() + $newContents = []; + foreach ($contents as $i => $part) { + if ($part instanceof Node\Scalar\EncapsedStringPart) { + $isLast = $i === \count($contents) - 1; + $part->value = $this->stripIndentation( + $part->value, $indentLen, $indentChar, + $i === 0, $isLast, $part->getAttributes() ); - $s->value = String_::parseEscapeSequences($s->value, null, $parseUnicodeEscape); + $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape); + if ($isLast) { + $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value); + } + if ('' === $part->value) { + continue; + } } + $newContents[] = $part; } - $s->value = preg_replace('~(\r\n|\n|\r)\z~', '', $s->value); - if ('' === $s->value) { - array_pop($contents); - } - return new Encapsed($contents, $attributes); + return new Encapsed($newContents, $attributes); } } diff --git a/test/PhpParser/ParserTest.php b/test/PhpParser/ParserTest.php index 9c4412dc..d3984eec 100644 --- a/test/PhpParser/ParserTest.php +++ b/test/PhpParser/ParserTest.php @@ -123,7 +123,7 @@ EOC; * @dataProvider provideTestExtraAttributes */ public function testExtraAttributes($code, $expectedAttributes) { - $parser = $this->getParser(new Lexer); + $parser = $this->getParser(new Lexer\Emulative); $stmts = $parser->parse("expr : $stmts[0]; $attributes = $node->getAttributes(); @@ -152,17 +152,20 @@ EOC; ['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], ['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], ['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], - ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], - ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR']], - ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], - ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], - ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], - ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], - ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff"]], - ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], - ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], - ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], - ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], + ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff", 'docIndentation' => '']], + ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']], + ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']], + ["<< String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => "\t"]], + ["<<<'STR'\n Foo\n STR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']], ["die", ['kind' => Expr\Exit_::KIND_DIE]], ["die('done')", ['kind' => Expr\Exit_::KIND_DIE]], ["exit", ['kind' => Expr\Exit_::KIND_EXIT]], diff --git a/test/code/parser/scalar/flexibleDocString.test b/test/code/parser/scalar/flexibleDocString.test index be1d5f89..4460912c 100644 --- a/test/code/parser/scalar/flexibleDocString.test +++ b/test/code/parser/scalar/flexibleDocString.test @@ -62,6 +62,52 @@ END; d\r\n e\n END; + +<<