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 f44a254
Showing 1 changed file with 1 addition and 32 deletions.
33 changes: 1 addition & 32 deletions src/Composer/Autoload/ClassMapGenerator.php
Expand Up @@ -246,7 +246,7 @@ private static function findClasses($path)
}

// strip heredocs/nowdocs
$contents = self::stripHereNowDocs($contents);
$contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?(?=[\r\n]+[ \t]*\\2))[\r\n]+[ \t]*\\2(?=\s*[;,.)])}s', 'null', $contents);
// strip strings
$contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
// strip leading non-php code if needed
Expand Down Expand Up @@ -303,35 +303,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 f44a254

Please sign in to comment.