From fe0b6edf186705147a147adbefac1dfd1d61c05f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 21 Aug 2021 17:45:32 +0200 Subject: [PATCH] Undo new stripping code and fix regex --- src/Composer/Autoload/ClassMapGenerator.php | 57 +++++++++------------ 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 6d9c00338958..35ef950dafcc 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -246,7 +246,31 @@ private static function findClasses($path) } // strip heredocs/nowdocs - $contents = self::stripHereNowDocs($contents); + $contents = preg_replace('{ + # opening heredoc/nowdoc delimiter (word-chars) + <<<[ \t]*([\'"]?)(\w+)\\1 + # needs to be followed by a newline + (?:\r\n|\n|\r) + # the meat of it, matching line by line until end delimiter + (?: + # a valid line is either.. + (?: + # purely non-word characters (which cannot be the delimiter) + [^\w\r\n]+ + # non-word or non-space char, then anything goes for the rest of the line + | [^\s\w][^\r\n]+ + # white-space (possessive match) not followed by the delimiter, then anything goes for the rest of the line + | \s*+(?!\\2)[^\r\n]+ + # white-space but no new lines + | [\t\f\v ]+ + ) + # end of line(s) + [\r\n]+ + )* + # end delimiter + \s* \\2 (?=\s*[;,.)]) + }x', 'null', $contents); + // strip strings $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents); // strip leading non-php code if needed @@ -303,35 +327,4 @@ private static function findClasses($path) return $classes; } - - /** - * Strip heredoc and nowdoc blocks from the contents of a file. - * - * @param string $contents File contents. - * - * @return string The cleaned up file contents. - */ - private static function stripHereNowDocs($contents) - { - // Find a heredoc/nowdoc start marker an its offset in the file. - $result = preg_match('{<<<[ \t]*([\'"]?)(?P\w+)\\1[\r\n]}', $contents, $startMatches, PREG_OFFSET_CAPTURE); - if ($result < 1) { - return $contents; - } - - $offset = ($startMatches['marker'][1] + strlen($startMatches['marker'][0])); - $pattern = '`[\r\n]+[ \t]*' . preg_quote($startMatches['marker'][0], '`') . '(?=\s*[;,.)])`'; - - // Find the corresponding heredoc/nowdoc end marker an its offset in the file. - $result = preg_match($pattern, $contents, $endMatches, PREG_OFFSET_CAPTURE, $offset); - if ($result < 1) { - return $contents; - } - - // Strip the complete heredoc/nowdoc and replace it with "null". - $contents = substr_replace($contents, 'null', $startMatches[0][1], (($endMatches[0][1] + strlen($endMatches[0][0])) - $startMatches[0][1])); - - // Recurse to strip the next heredoc/nowdoc until there are none left. - return self::stripHereNowDocs($contents); - } }