Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Compiler.php

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

Composer

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal