From b30eccb246f5ae1349e47046a173e3bf56fdef20 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 18 Apr 2020 20:57:50 +0200 Subject: [PATCH] [PHP 7.3] Fix regex slash escaping --- .../Fixture/slashes.php.inc | 27 +++++++++++++++++++ .../Printer/BetterStandardPrinter.php | 12 ++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 rules/php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/slashes.php.inc diff --git a/rules/php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/slashes.php.inc b/rules/php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/slashes.php.inc new file mode 100644 index 00000000000..eae255f466c --- /dev/null +++ b/rules/php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/slashes.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index affc74e51fa..eddab5be3c0 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -266,9 +266,15 @@ final class BetterStandardPrinter extends Standard */ protected function pScalar_String(String_ $node): string { - $kind = $node->getAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED); - if ($kind === String_::KIND_DOUBLE_QUOTED && $node->getAttribute('is_regular_pattern')) { - return '"' . $node->value . '"'; + if ($node->getAttribute('is_regular_pattern')) { + $kind = $node->getAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED); + if ($kind === String_::KIND_DOUBLE_QUOTED) { + return '"' . $node->value . '"'; + } + + if ($kind === String_::KIND_SINGLE_QUOTED) { + return "'" . $node->value . "'"; + } } return parent::pScalar_String($node);