コード例 #1
0
 /**
  * Calculates the coverage threshold
  *
  * @param string $filename The filename to analyse
  * @param array $coverageInformation Array with coverage information
  * @throws BuildException
  */
 protected function calculateCoverageThreshold($filename, $coverageInformation)
 {
     $classes = PHPUnitUtil::getDefinedClasses($filename, $this->_classpath);
     if (is_array($classes)) {
         foreach ($classes as $className) {
             // Skip class if excluded from coverage threshold validation
             if ($this->_excludes !== null) {
                 if (in_array($className, $this->_excludes->getExcludedClasses())) {
                     continue;
                 }
             }
             $reflection = new ReflectionClass($className);
             $classStartLine = $reflection->getStartLine();
             // Strange PHP5 reflection bug, classes without parent class
             // or implemented interfaces seem to start one line off
             if ($reflection->getParentClass() === null && count($reflection->getInterfaces()) === 0) {
                 unset($coverageInformation[$classStartLine + 1]);
             } else {
                 unset($coverageInformation[$classStartLine]);
             }
             reset($coverageInformation);
             $methods = $reflection->getMethods();
             foreach ($methods as $method) {
                 // PHP5 reflection considers methods of a parent class
                 // to be part of a subclass, we don't
                 if ($method->getDeclaringClass()->getName() != $reflection->getName()) {
                     continue;
                 }
                 // Skip method if excluded from coverage threshold validation
                 if ($this->_excludes !== null) {
                     $excludedMethods = $this->_excludes->getExcludedMethods();
                     if (isset($excludedMethods[$className])) {
                         if (in_array($method->getName(), $excludedMethods[$className]) || in_array($method->getName() . '()', $excludedMethods[$className])) {
                             continue;
                         }
                     }
                 }
                 $methodStartLine = $method->getStartLine();
                 $methodEndLine = $method->getEndLine();
                 // small fix for XDEBUG_CC_UNUSED
                 if (isset($coverageInformation[$methodStartLine])) {
                     unset($coverageInformation[$methodStartLine]);
                 }
                 if (isset($coverageInformation[$methodEndLine])) {
                     unset($coverageInformation[$methodEndLine]);
                 }
                 if ($method->isAbstract()) {
                     continue;
                 }
                 $lineNr = key($coverageInformation);
                 while ($lineNr !== null && $lineNr < $methodStartLine) {
                     next($coverageInformation);
                     $lineNr = key($coverageInformation);
                 }
                 $methodStatementsCovered = 0;
                 $methodStatementCount = 0;
                 while ($lineNr !== null && $lineNr <= $methodEndLine) {
                     $methodStatementCount++;
                     $lineCoverageInfo = $coverageInformation[$lineNr];
                     // set covered when CODE is other than -1 (not executed)
                     if ($lineCoverageInfo > 0 || $lineCoverageInfo === -2) {
                         $methodStatementsCovered++;
                     }
                     next($coverageInformation);
                     $lineNr = key($coverageInformation);
                 }
                 if ($methodStatementCount > 0) {
                     $methodCoverage = $methodStatementsCovered / $methodStatementCount * 100;
                 } else {
                     $methodCoverage = 0;
                 }
                 if ($methodCoverage < $this->_perMethod && !$method->isAbstract()) {
                     throw new BuildException('The coverage (' . round($methodCoverage, 2) . '%) ' . 'for method "' . $method->getName() . '" is lower' . ' than the specified threshold (' . $this->_perMethod . '%), see file: "' . $filename . '"');
                 } elseif ($methodCoverage < $this->_perMethod && $method->isAbstract() && $this->_verbose === true) {
                     $this->log('Skipped coverage threshold for abstract method "' . $method->getName() . '"');
                 }
                 // store the minimum coverage value for logging (see #466)
                 if ($this->_minMethodCoverageFound !== null) {
                     if ($this->_minMethodCoverageFound > $methodCoverage) {
                         $this->_minMethodCoverageFound = $methodCoverage;
                     }
                 } else {
                     $this->_minMethodCoverageFound = $methodCoverage;
                 }
             }
             $classStatementCount = count($coverageInformation);
             $classStatementsCovered = count(array_filter($coverageInformation, array($this, 'filterCovered')));
             if ($classStatementCount > 0) {
                 $classCoverage = $classStatementsCovered / $classStatementCount * 100;
             } else {
                 $classCoverage = 0;
             }
             if ($classCoverage < $this->_perClass && !$reflection->isAbstract()) {
                 throw new BuildException('The coverage (' . round($classCoverage, 2) . '%) for class "' . $reflection->getName() . '" is lower than the ' . 'specified threshold (' . $this->_perClass . '%), ' . 'see file: "' . $filename . '"');
             } elseif ($classCoverage < $this->_perClass && $reflection->isAbstract() && $this->_verbose === true) {
                 $this->log('Skipped coverage threshold for abstract class "' . $reflection->getName() . '"');
             }
             // store the minimum coverage value for logging (see #466)
             if ($this->_minClassCoverageFound !== null) {
                 if ($this->_minClassCoverageFound > $classCoverage) {
                     $this->_minClassCoverageFound = $classCoverage;
                 }
             } else {
                 $this->_minClassCoverageFound = $classCoverage;
             }
             $this->_projectStatementCount += $classStatementCount;
             $this->_projectStatementsCovered += $classStatementsCovered;
         }
     }
 }