From d373f8d0ebdedad2194fb837fd74c9ce3788add5 Mon Sep 17 00:00:00 2001 From: webimpress Date: Sat, 11 May 2019 18:26:06 +0100 Subject: [PATCH] StringClassReference - fixed regular expression to match FQCN --- .../Formatting/StringClassReferenceSniff.php | 15 +++------------ .../Formatting/StringClassReferenceUnitTest.inc | 2 ++ .../StringClassReferenceUnitTest.inc.fixed | 2 ++ 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/WebimpressCodingStandard/Sniffs/Formatting/StringClassReferenceSniff.php b/src/WebimpressCodingStandard/Sniffs/Formatting/StringClassReferenceSniff.php index 5186a5d4..983cfd6b 100644 --- a/src/WebimpressCodingStandard/Sniffs/Formatting/StringClassReferenceSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Formatting/StringClassReferenceSniff.php @@ -11,8 +11,8 @@ use function interface_exists; use function ltrim; use function preg_match; +use function str_replace; use function strpos; -use function strtr; use function substr; use function trait_exists; @@ -38,17 +38,8 @@ public function process(File $phpcsFile, $stackPtr) return; } - $name = strtr($tokens[$stackPtr]['content'], [ - '"' => '', - "'" => '', - '\\\\' => '\\', - ]); - - if (strpos($name, '\\\\') !== false - || preg_match('/[^\\a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]/', $name) - || substr($name, -1) === '\\' - || ltrim($name, '\\') === '' - ) { + $name = substr(str_replace('\\\\', '\\', $tokens[$stackPtr]['content']), 1, -1); + if (! preg_match('/^(\\\\?[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)+$/', $name)) { return; } diff --git a/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc b/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc index 4309c667..83e62e39 100644 --- a/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc +++ b/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc @@ -23,3 +23,5 @@ $spaceAtTheEnd = ' WebimpressCodingStandardTest\\Ruleset '; $namespaceSeparatorAtTheEnd = 'WebimpressCodingStandardTest\\Ruleset\\'; $justNamespaceSeparator = '\\'; + +$quotes = '"\DateTime"'; diff --git a/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc.fixed b/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc.fixed index 63d387cf..964d596f 100644 --- a/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc.fixed +++ b/test/Sniffs/Formatting/StringClassReferenceUnitTest.inc.fixed @@ -23,3 +23,5 @@ $spaceAtTheEnd = ' WebimpressCodingStandardTest\\Ruleset '; $namespaceSeparatorAtTheEnd = 'WebimpressCodingStandardTest\\Ruleset\\'; $justNamespaceSeparator = '\\'; + +$quotes = '"\DateTime"';