You are here

private function PHP_CodeCoverage::getLinesToBeIgnored in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/phpunit/php-code-coverage/src/CodeCoverage.php \PHP_CodeCoverage::getLinesToBeIgnored()

Returns the lines of a source file that should be ignored.

@since Method available since Release 2.0.0

Parameters

string $filename:

Return value

array

Throws

PHP_CodeCoverage_Exception

1 call to PHP_CodeCoverage::getLinesToBeIgnored()
PHP_CodeCoverage::applyIgnoredLinesFilter in vendor/phpunit/php-code-coverage/src/CodeCoverage.php
Applies the "ignored lines" filtering.

File

vendor/phpunit/php-code-coverage/src/CodeCoverage.php, line 658

Class

PHP_CodeCoverage
Provides collection functionality for PHP code coverage information.

Code

private function getLinesToBeIgnored($filename) {
  if (!is_string($filename)) {
    throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(1, 'string');
  }
  if (!isset($this->ignoredLines[$filename])) {
    $this->ignoredLines[$filename] = array();
    if ($this->disableIgnoredLines) {
      return $this->ignoredLines[$filename];
    }
    $ignore = false;
    $stop = false;
    $lines = file($filename);
    $numLines = count($lines);
    foreach ($lines as $index => $line) {
      if (!trim($line)) {
        $this->ignoredLines[$filename][] = $index + 1;
      }
    }
    if ($this->cacheTokens) {
      $tokens = PHP_Token_Stream_CachingFactory::get($filename);
    }
    else {
      $tokens = new PHP_Token_Stream($filename);
    }
    $classes = array_merge($tokens
      ->getClasses(), $tokens
      ->getTraits());
    $tokens = $tokens
      ->tokens();
    foreach ($tokens as $token) {
      switch (get_class($token)) {
        case 'PHP_Token_COMMENT':
        case 'PHP_Token_DOC_COMMENT':
          $_token = trim($token);
          $_line = trim($lines[$token
            ->getLine() - 1]);
          if ($_token == '// @codeCoverageIgnore' || $_token == '//@codeCoverageIgnore') {
            $ignore = true;
            $stop = true;
          }
          elseif ($_token == '// @codeCoverageIgnoreStart' || $_token == '//@codeCoverageIgnoreStart') {
            $ignore = true;
          }
          elseif ($_token == '// @codeCoverageIgnoreEnd' || $_token == '//@codeCoverageIgnoreEnd') {
            $stop = true;
          }
          if (!$ignore) {
            $start = $token
              ->getLine();
            $end = $start + substr_count($token, "\n");

            // Do not ignore the first line when there is a token
            // before the comment
            if (0 !== strpos($_token, $_line)) {
              $start++;
            }
            for ($i = $start; $i < $end; $i++) {
              $this->ignoredLines[$filename][] = $i;
            }

            // A DOC_COMMENT token or a COMMENT token starting with "/*"
            // does not contain the final \n character in its text
            if (isset($lines[$i - 1]) && 0 === strpos($_token, '/*') && '*/' === substr(trim($lines[$i - 1]), -2)) {
              $this->ignoredLines[$filename][] = $i;
            }
          }
          break;
        case 'PHP_Token_INTERFACE':
        case 'PHP_Token_TRAIT':
        case 'PHP_Token_CLASS':
        case 'PHP_Token_FUNCTION':
          $docblock = $token
            ->getDocblock();
          $this->ignoredLines[$filename][] = $token
            ->getLine();
          if (strpos($docblock, '@codeCoverageIgnore') || strpos($docblock, '@deprecated')) {
            $endLine = $token
              ->getEndLine();
            for ($i = $token
              ->getLine(); $i <= $endLine; $i++) {
              $this->ignoredLines[$filename][] = $i;
            }
          }
          elseif ($token instanceof PHP_Token_INTERFACE || $token instanceof PHP_Token_TRAIT || $token instanceof PHP_Token_CLASS) {
            if (empty($classes[$token
              ->getName()]['methods'])) {
              for ($i = $token
                ->getLine(); $i <= $token
                ->getEndLine(); $i++) {
                $this->ignoredLines[$filename][] = $i;
              }
            }
            else {
              $firstMethod = array_shift($classes[$token
                ->getName()]['methods']);
              do {
                $lastMethod = array_pop($classes[$token
                  ->getName()]['methods']);
              } while ($lastMethod !== null && substr($lastMethod['signature'], 0, 18) == 'anonymous function');
              if ($lastMethod === null) {
                $lastMethod = $firstMethod;
              }
              for ($i = $token
                ->getLine(); $i < $firstMethod['startLine']; $i++) {
                $this->ignoredLines[$filename][] = $i;
              }
              for ($i = $token
                ->getEndLine(); $i > $lastMethod['endLine']; $i--) {
                $this->ignoredLines[$filename][] = $i;
              }
            }
          }
          break;
        case 'PHP_Token_NAMESPACE':
          $this->ignoredLines[$filename][] = $token
            ->getEndLine();

        // Intentional fallthrough
        case 'PHP_Token_OPEN_TAG':
        case 'PHP_Token_CLOSE_TAG':
        case 'PHP_Token_USE':
          $this->ignoredLines[$filename][] = $token
            ->getLine();
          break;
      }
      if ($ignore) {
        $this->ignoredLines[$filename][] = $token
          ->getLine();
        if ($stop) {
          $ignore = false;
          $stop = false;
        }
      }
    }
    $this->ignoredLines[$filename][] = $numLines + 1;
    $this->ignoredLines[$filename] = array_unique($this->ignoredLines[$filename]);
    sort($this->ignoredLines[$filename]);
  }
  return $this->ignoredLines[$filename];
}