function Compiler::stripWhitespace
Removes whitespace from a PHP source string while preserving line numbers.
Parameters
string $source A PHP string:
Return value
string The PHP string with the whitespace removed
1 call to Compiler::stripWhitespace()
- Compiler::addFile in vendor/
composer/ composer/ src/ Composer/ Compiler.php
File
-
vendor/
composer/ composer/ src/ Composer/ Compiler.php, line 252
Class
- Compiler
- The Compiler class compiles composer into a phar
Namespace
ComposerCode
private function stripWhitespace(string $source) : string {
if (!function_exists('token_get_all')) {
return $source;
}
$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
}
elseif (in_array($token[0], [
T_COMMENT,
T_DOC_COMMENT,
])) {
$output .= str_repeat("\n", substr_count($token[1], "\n"));
}
elseif (T_WHITESPACE === $token[0]) {
// reduce wide spaces
$whitespace = Preg::replace('{[ \\t]+}', ' ', $token[1]);
// normalize newlines to \n
$whitespace = Preg::replace('{(?:\\r\\n|\\r|\\n)}', "\n", $whitespace);
// trim leading spaces
$whitespace = Preg::replace('{\\n +}', "\n", $whitespace);
$output .= $whitespace;
}
else {
$output .= $token[1];
}
}
return $output;
}