Exemple #1
0
 /**
  * @param CodeCoverage $coverage
  * @param string       $target
  *
  * @throws RuntimeException
  */
 public function process(CodeCoverage $coverage, $target)
 {
     if (substr($target, -1, 1) != DIRECTORY_SEPARATOR) {
         $target .= DIRECTORY_SEPARATOR;
     }
     $this->target = $target;
     $this->initTargetDirectory($target);
     $report = $coverage->getReport();
     $this->project = new Project($coverage->getReport()->getName());
     $this->processTests($coverage->getTests());
     $this->processDirectory($report, $this->project);
     $index = $this->project->asDom();
     $index->formatOutput = true;
     $index->preserveWhiteSpace = false;
     $index->save($target . '/index.xml');
 }
Exemple #2
0
 /**
  * @param CodeCoverage $coverage
  * @param string       $target
  */
 public function process(CodeCoverage $coverage, $target)
 {
     $target = $this->getDirectory($target);
     $report = $coverage->getReport();
     unset($coverage);
     if (!isset($_SERVER['REQUEST_TIME'])) {
         $_SERVER['REQUEST_TIME'] = time();
     }
     $date = date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']);
     $dashboard = new Dashboard($this->templatePath, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound);
     $directory = new Directory($this->templatePath, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound);
     $file = new File($this->templatePath, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound);
     $directory->render($report, $target . 'index.html');
     $dashboard->render($report, $target . 'dashboard.html');
     foreach ($report as $node) {
         $id = $node->getId();
         if ($node instanceof DirectoryNode) {
             if (!file_exists($target . $id)) {
                 mkdir($target . $id, 0777, true);
             }
             $directory->render($node, $target . $id . '/index.html');
             $dashboard->render($node, $target . $id . '/dashboard.html');
         } else {
             $dir = dirname($target . $id);
             if (!file_exists($dir)) {
                 mkdir($dir, 0777, true);
             }
             $file->render($node, $target . $id . '.html');
         }
     }
     $this->copyFiles($target);
 }
Exemple #3
0
 /**
  * @param CodeCoverage $coverage
  * @param bool         $showColors
  *
  * @return string
  */
 public function process(CodeCoverage $coverage, $showColors = false)
 {
     $output = PHP_EOL . PHP_EOL;
     $report = $coverage->getReport();
     unset($coverage);
     $colors = ['header' => '', 'classes' => '', 'methods' => '', 'lines' => '', 'reset' => '', 'eol' => ''];
     if ($showColors) {
         $colors['classes'] = $this->getCoverageColor($report->getNumTestedClassesAndTraits(), $report->getNumClassesAndTraits());
         $colors['methods'] = $this->getCoverageColor($report->getNumTestedMethods(), $report->getNumMethods());
         $colors['lines'] = $this->getCoverageColor($report->getNumExecutedLines(), $report->getNumExecutableLines());
         $colors['reset'] = $this->colors['reset'];
         $colors['header'] = $this->colors['header'];
         $colors['eol'] = $this->colors['eol'];
     }
     $classes = sprintf('  Classes: %6s (%d/%d)', Util::percent($report->getNumTestedClassesAndTraits(), $report->getNumClassesAndTraits(), true), $report->getNumTestedClassesAndTraits(), $report->getNumClassesAndTraits());
     $methods = sprintf('  Methods: %6s (%d/%d)', Util::percent($report->getNumTestedMethods(), $report->getNumMethods(), true), $report->getNumTestedMethods(), $report->getNumMethods());
     $lines = sprintf('  Lines:   %6s (%d/%d)', Util::percent($report->getNumExecutedLines(), $report->getNumExecutableLines(), true), $report->getNumExecutedLines(), $report->getNumExecutableLines());
     $padding = max(array_map('strlen', [$classes, $methods, $lines]));
     if ($this->showOnlySummary) {
         $title = 'Code Coverage Report Summary:';
         $padding = max($padding, strlen($title));
         $output .= $this->format($colors['header'], $padding, $title);
     } else {
         $date = date('  Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
         $title = 'Code Coverage Report:';
         $output .= $this->format($colors['header'], $padding, $title);
         $output .= $this->format($colors['header'], $padding, $date);
         $output .= $this->format($colors['header'], $padding, '');
         $output .= $this->format($colors['header'], $padding, ' Summary:');
     }
     $output .= $this->format($colors['classes'], $padding, $classes);
     $output .= $this->format($colors['methods'], $padding, $methods);
     $output .= $this->format($colors['lines'], $padding, $lines);
     if ($this->showOnlySummary) {
         return $output . PHP_EOL;
     }
     $classCoverage = [];
     foreach ($report as $item) {
         if (!$item instanceof File) {
             continue;
         }
         $classes = $item->getClassesAndTraits();
         foreach ($classes as $className => $class) {
             $classStatements = 0;
             $coveredClassStatements = 0;
             $coveredMethods = 0;
             $classMethods = 0;
             foreach ($class['methods'] as $method) {
                 if ($method['executableLines'] == 0) {
                     continue;
                 }
                 $classMethods++;
                 $classStatements += $method['executableLines'];
                 $coveredClassStatements += $method['executedLines'];
                 if ($method['coverage'] == 100) {
                     $coveredMethods++;
                 }
             }
             if (!empty($class['package']['namespace'])) {
                 $namespace = '\\' . $class['package']['namespace'] . '::';
             } elseif (!empty($class['package']['fullPackage'])) {
                 $namespace = '@' . $class['package']['fullPackage'] . '::';
             } else {
                 $namespace = '';
             }
             $classCoverage[$namespace . $className] = ['namespace' => $namespace, 'className ' => $className, 'methodsCovered' => $coveredMethods, 'methodCount' => $classMethods, 'statementsCovered' => $coveredClassStatements, 'statementCount' => $classStatements];
         }
     }
     ksort($classCoverage);
     $methodColor = '';
     $linesColor = '';
     $resetColor = '';
     foreach ($classCoverage as $fullQualifiedPath => $classInfo) {
         if ($classInfo['statementsCovered'] != 0 || $this->showUncoveredFiles) {
             if ($showColors) {
                 $methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']);
                 $linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']);
                 $resetColor = $colors['reset'];
             }
             $output .= PHP_EOL . $fullQualifiedPath . PHP_EOL . '  ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], 2) . $resetColor . ' ' . '  ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], 3) . $resetColor;
         }
     }
     return $output . PHP_EOL;
 }
Exemple #4
0
 /**
  * @param CodeCoverage $coverage
  * @param string       $target
  * @param string       $name
  *
  * @return string
  */
 public function process(CodeCoverage $coverage, $target = null, $name = null)
 {
     $xmlDocument = new \DOMDocument('1.0', 'UTF-8');
     $xmlDocument->formatOutput = true;
     $xmlCoverage = $xmlDocument->createElement('coverage');
     $xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']);
     $xmlDocument->appendChild($xmlCoverage);
     $xmlProject = $xmlDocument->createElement('project');
     $xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']);
     if (is_string($name)) {
         $xmlProject->setAttribute('name', $name);
     }
     $xmlCoverage->appendChild($xmlProject);
     $packages = [];
     $report = $coverage->getReport();
     unset($coverage);
     foreach ($report as $item) {
         if (!$item instanceof File) {
             continue;
         }
         /* @var File $item */
         $xmlFile = $xmlDocument->createElement('file');
         $xmlFile->setAttribute('name', $item->getPath());
         $classes = $item->getClassesAndTraits();
         $coverage = $item->getCoverageData();
         $lines = [];
         $namespace = 'global';
         foreach ($classes as $className => $class) {
             $classStatements = 0;
             $coveredClassStatements = 0;
             $coveredMethods = 0;
             $classMethods = 0;
             foreach ($class['methods'] as $methodName => $method) {
                 if ($method['executableLines'] == 0) {
                     continue;
                 }
                 $classMethods++;
                 $classStatements += $method['executableLines'];
                 $coveredClassStatements += $method['executedLines'];
                 if ($method['coverage'] == 100) {
                     $coveredMethods++;
                 }
                 $methodCount = 0;
                 foreach (range($method['startLine'], $method['endLine']) as $line) {
                     if (isset($coverage[$line]) && $coverage[$line] !== null) {
                         $methodCount = max($methodCount, count($coverage[$line]));
                     }
                 }
                 $lines[$method['startLine']] = ['ccn' => $method['ccn'], 'count' => $methodCount, 'crap' => $method['crap'], 'type' => 'method', 'visibility' => $method['visibility'], 'name' => $methodName];
             }
             if (!empty($class['package']['namespace'])) {
                 $namespace = $class['package']['namespace'];
             }
             $xmlClass = $xmlDocument->createElement('class');
             $xmlClass->setAttribute('name', $className);
             $xmlClass->setAttribute('namespace', $namespace);
             if (!empty($class['package']['fullPackage'])) {
                 $xmlClass->setAttribute('fullPackage', $class['package']['fullPackage']);
             }
             if (!empty($class['package']['category'])) {
                 $xmlClass->setAttribute('category', $class['package']['category']);
             }
             if (!empty($class['package']['package'])) {
                 $xmlClass->setAttribute('package', $class['package']['package']);
             }
             if (!empty($class['package']['subpackage'])) {
                 $xmlClass->setAttribute('subpackage', $class['package']['subpackage']);
             }
             $xmlFile->appendChild($xmlClass);
             $xmlMetrics = $xmlDocument->createElement('metrics');
             $xmlMetrics->setAttribute('complexity', $class['ccn']);
             $xmlMetrics->setAttribute('methods', $classMethods);
             $xmlMetrics->setAttribute('coveredmethods', $coveredMethods);
             $xmlMetrics->setAttribute('conditionals', 0);
             $xmlMetrics->setAttribute('coveredconditionals', 0);
             $xmlMetrics->setAttribute('statements', $classStatements);
             $xmlMetrics->setAttribute('coveredstatements', $coveredClassStatements);
             $xmlMetrics->setAttribute('elements', $classMethods + $classStatements);
             $xmlMetrics->setAttribute('coveredelements', $coveredMethods + $coveredClassStatements);
             $xmlClass->appendChild($xmlMetrics);
         }
         foreach ($coverage as $line => $data) {
             if ($data === null || isset($lines[$line])) {
                 continue;
             }
             $lines[$line] = ['count' => count($data), 'type' => 'stmt'];
         }
         ksort($lines);
         foreach ($lines as $line => $data) {
             $xmlLine = $xmlDocument->createElement('line');
             $xmlLine->setAttribute('num', $line);
             $xmlLine->setAttribute('type', $data['type']);
             if (isset($data['name'])) {
                 $xmlLine->setAttribute('name', $data['name']);
             }
             if (isset($data['visibility'])) {
                 $xmlLine->setAttribute('visibility', $data['visibility']);
             }
             if (isset($data['ccn'])) {
                 $xmlLine->setAttribute('complexity', $data['ccn']);
             }
             if (isset($data['crap'])) {
                 $xmlLine->setAttribute('crap', $data['crap']);
             }
             $xmlLine->setAttribute('count', $data['count']);
             $xmlFile->appendChild($xmlLine);
         }
         $linesOfCode = $item->getLinesOfCode();
         $xmlMetrics = $xmlDocument->createElement('metrics');
         $xmlMetrics->setAttribute('loc', $linesOfCode['loc']);
         $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']);
         $xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits());
         $xmlMetrics->setAttribute('methods', $item->getNumMethods());
         $xmlMetrics->setAttribute('coveredmethods', $item->getNumTestedMethods());
         $xmlMetrics->setAttribute('conditionals', 0);
         $xmlMetrics->setAttribute('coveredconditionals', 0);
         $xmlMetrics->setAttribute('statements', $item->getNumExecutableLines());
         $xmlMetrics->setAttribute('coveredstatements', $item->getNumExecutedLines());
         $xmlMetrics->setAttribute('elements', $item->getNumMethods() + $item->getNumExecutableLines());
         $xmlMetrics->setAttribute('coveredelements', $item->getNumTestedMethods() + $item->getNumExecutedLines());
         $xmlFile->appendChild($xmlMetrics);
         if ($namespace == 'global') {
             $xmlProject->appendChild($xmlFile);
         } else {
             if (!isset($packages[$namespace])) {
                 $packages[$namespace] = $xmlDocument->createElement('package');
                 $packages[$namespace]->setAttribute('name', $namespace);
                 $xmlProject->appendChild($packages[$namespace]);
             }
             $packages[$namespace]->appendChild($xmlFile);
         }
     }
     $linesOfCode = $report->getLinesOfCode();
     $xmlMetrics = $xmlDocument->createElement('metrics');
     $xmlMetrics->setAttribute('files', count($report));
     $xmlMetrics->setAttribute('loc', $linesOfCode['loc']);
     $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']);
     $xmlMetrics->setAttribute('classes', $report->getNumClassesAndTraits());
     $xmlMetrics->setAttribute('methods', $report->getNumMethods());
     $xmlMetrics->setAttribute('coveredmethods', $report->getNumTestedMethods());
     $xmlMetrics->setAttribute('conditionals', 0);
     $xmlMetrics->setAttribute('coveredconditionals', 0);
     $xmlMetrics->setAttribute('statements', $report->getNumExecutableLines());
     $xmlMetrics->setAttribute('coveredstatements', $report->getNumExecutedLines());
     $xmlMetrics->setAttribute('elements', $report->getNumMethods() + $report->getNumExecutableLines());
     $xmlMetrics->setAttribute('coveredelements', $report->getNumTestedMethods() + $report->getNumExecutedLines());
     $xmlProject->appendChild($xmlMetrics);
     $buffer = $xmlDocument->saveXML();
     if ($target !== null) {
         if (!is_dir(dirname($target))) {
             mkdir(dirname($target), 0777, true);
         }
         file_put_contents($target, $buffer);
     }
     return $buffer;
 }
Exemple #5
0
 /**
  * @param CodeCoverage $coverage
  * @param string       $target
  * @param string       $name
  *
  * @return string
  */
 public function process(CodeCoverage $coverage, $target = null, $name = null)
 {
     $document = new \DOMDocument('1.0', 'UTF-8');
     $document->formatOutput = true;
     $root = $document->createElement('crap_result');
     $document->appendChild($root);
     $project = $document->createElement('project', is_string($name) ? $name : '');
     $root->appendChild($project);
     $root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME'])));
     $stats = $document->createElement('stats');
     $methodsNode = $document->createElement('methods');
     $report = $coverage->getReport();
     unset($coverage);
     $fullMethodCount = 0;
     $fullCrapMethodCount = 0;
     $fullCrapLoad = 0;
     $fullCrap = 0;
     foreach ($report as $item) {
         $namespace = 'global';
         if (!$item instanceof File) {
             continue;
         }
         $file = $document->createElement('file');
         $file->setAttribute('name', $item->getPath());
         $classes = $item->getClassesAndTraits();
         foreach ($classes as $className => $class) {
             foreach ($class['methods'] as $methodName => $method) {
                 $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']);
                 $fullCrap += $method['crap'];
                 $fullCrapLoad += $crapLoad;
                 $fullMethodCount++;
                 if ($method['crap'] >= $this->threshold) {
                     $fullCrapMethodCount++;
                 }
                 $methodNode = $document->createElement('method');
                 if (!empty($class['package']['namespace'])) {
                     $namespace = $class['package']['namespace'];
                 }
                 $methodNode->appendChild($document->createElement('package', $namespace));
                 $methodNode->appendChild($document->createElement('className', $className));
                 $methodNode->appendChild($document->createElement('methodName', $methodName));
                 $methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature'])));
                 $methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature'])));
                 $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap'])));
                 $methodNode->appendChild($document->createElement('complexity', $method['ccn']));
                 $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage'])));
                 $methodNode->appendChild($document->createElement('crapLoad', round($crapLoad)));
                 $methodsNode->appendChild($methodNode);
             }
         }
     }
     $stats->appendChild($document->createElement('name', 'Method Crap Stats'));
     $stats->appendChild($document->createElement('methodCount', $fullMethodCount));
     $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount));
     $stats->appendChild($document->createElement('crapLoad', round($fullCrapLoad)));
     $stats->appendChild($document->createElement('totalCrap', $fullCrap));
     if ($fullMethodCount > 0) {
         $crapMethodPercent = $this->roundValue(100 * $fullCrapMethodCount / $fullMethodCount);
     } else {
         $crapMethodPercent = 0;
     }
     $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent));
     $root->appendChild($stats);
     $root->appendChild($methodsNode);
     $buffer = $document->saveXML();
     if ($target !== null) {
         if (!is_dir(dirname($target))) {
             mkdir(dirname($target), 0777, true);
         }
         file_put_contents($target, $buffer);
     }
     return $buffer;
 }