Skip to content

Commit

Permalink
Undo new stripping code and fix regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Aug 21, 2021
1 parent 3344844 commit fe0b6ed
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/Composer/Autoload/ClassMapGenerator.php
Expand Up @@ -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
Expand Down Expand Up @@ -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<marker>\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);
}
}

0 comments on commit fe0b6ed

Please sign in to comment.