You are here

public static function Kernel::stripComments in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Kernel.php \Symfony\Component\HttpKernel\Kernel::stripComments()

Removes comments from a PHP source string.

We don't use the PHP php_strip_whitespace() function as we want the content to be readable and well-formatted.

Parameters

string $source A PHP string:

Return value

string The PHP string with the comments removed

2 calls to Kernel::stripComments()
Kernel::dumpContainer in vendor/symfony/http-kernel/Kernel.php
Dumps the service container to PHP code in the cache.
KernelTest::testStripComments in vendor/symfony/http-kernel/Tests/KernelTest.php

File

vendor/symfony/http-kernel/Kernel.php, line 704

Class

Kernel
The Kernel is the heart of the Symfony system.

Namespace

Symfony\Component\HttpKernel

Code

public static function stripComments($source) {
  if (!function_exists('token_get_all')) {
    return $source;
  }
  $rawChunk = '';
  $output = '';
  $tokens = token_get_all($source);
  $ignoreSpace = false;
  for (reset($tokens); false !== ($token = current($tokens)); next($tokens)) {
    if (is_string($token)) {
      $rawChunk .= $token;
    }
    elseif (T_START_HEREDOC === $token[0]) {
      $output .= $rawChunk . $token[1];
      do {
        $token = next($tokens);
        $output .= $token[1];
      } while ($token[0] !== T_END_HEREDOC);
      $rawChunk = '';
    }
    elseif (T_WHITESPACE === $token[0]) {
      if ($ignoreSpace) {
        $ignoreSpace = false;
        continue;
      }

      // replace multiple new lines with a single newline
      $rawChunk .= preg_replace(array(
        '/\\n{2,}/S',
      ), "\n", $token[1]);
    }
    elseif (in_array($token[0], array(
      T_COMMENT,
      T_DOC_COMMENT,
    ))) {
      $ignoreSpace = true;
    }
    else {
      $rawChunk .= $token[1];

      // The PHP-open tag already has a new-line
      if (T_OPEN_TAG === $token[0]) {
        $ignoreSpace = true;
      }
    }
  }
  $output .= $rawChunk;
  return $output;
}