From c8892714b20e4bd60cfa6c8ec29bdbf08f36ac4d Mon Sep 17 00:00:00 2001 From: Dominik Pesch Date: Thu, 17 Oct 2019 10:07:32 +0200 Subject: [PATCH 1/2] Added test for issue #2173 This test shows the false positive for `hyphen + shorthand` combination outside a character class which should not be escaped. --- .../Fixture/false_positive.php.inc | 23 +++++++++++++++++++ .../RegexDashEscapeRectorTest.php | 1 + 2 files changed, 24 insertions(+) create mode 100644 packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/false_positive.php.inc diff --git a/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/false_positive.php.inc b/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/false_positive.php.inc new file mode 100644 index 000000000000..c2a4f6b31314 --- /dev/null +++ b/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/Fixture/false_positive.php.inc @@ -0,0 +1,23 @@ + +----- + diff --git a/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/RegexDashEscapeRectorTest.php b/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/RegexDashEscapeRectorTest.php index a32c858aa6eb..ce5f079874d1 100644 --- a/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/RegexDashEscapeRectorTest.php +++ b/packages/Php73/tests/Rector/FuncCall/RegexDashEscapeRector/RegexDashEscapeRectorTest.php @@ -26,6 +26,7 @@ public function provideDataForTest(): Iterator yield [__DIR__ . '/Fixture/external_const.php.inc']; yield [__DIR__ . '/Fixture/variable.php.inc']; yield [__DIR__ . '/Fixture/multiple_variables.php.inc']; + yield [__DIR__ . '/Fixture/false_positive.php.inc']; } protected function getRectorClass(): string From 148e98efa739da6148df5707ce514fa6454ed485 Mon Sep 17 00:00:00 2001 From: Dominik Pesch Date: Thu, 17 Oct 2019 10:12:23 +0200 Subject: [PATCH 2/2] Fix escaping with adding opening and closing brackets This patch fixes this issue. But I suspect that there will be more edge cases which will not be fixed with this small patch. Perhaps it will be better to assure, that the escaping will only executed on hyphen in character classes. --- .../Php73/src/Rector/FuncCall/RegexDashEscapeRector.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/Php73/src/Rector/FuncCall/RegexDashEscapeRector.php b/packages/Php73/src/Rector/FuncCall/RegexDashEscapeRector.php index 74b28b91a693..4e9d6a225824 100644 --- a/packages/Php73/src/Rector/FuncCall/RegexDashEscapeRector.php +++ b/packages/Php73/src/Rector/FuncCall/RegexDashEscapeRector.php @@ -24,13 +24,13 @@ final class RegexDashEscapeRector extends AbstractRector /** * @var string */ - private const LEFT_HAND_UNESCAPED_DASH_PATTERN = '#(\\\\(w|s|d))-(?!\])#i'; + private const LEFT_HAND_UNESCAPED_DASH_PATTERN = '#(\[.*?\\\\(w|s|d))-(?!\])#i'; /** * @var string * @see https://regex101.com/r/TBVme9/1 */ - private const RIGHT_HAND_UNESCAPED_DASH_PATTERN = '#(?value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_PATTERN, '\-$2'); + $stringNode->value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_PATTERN, '\-$1]'); // helped needed to skip re-escaping regular expression $stringNode->setAttribute('is_regular_pattern', true); }