static function merge($project, $codeCoverageInformation) { $coverageDatabase = $project->getProperty('coverage.database'); if (!$coverageDatabase) { throw new BuildException("Property coverage.database is not set - please include coverage-setup in your build file"); } $database = new PhingFile($coverageDatabase); $props = new Properties(); $props->load($database); $coverageTotal = $codeCoverageInformation; foreach ($coverageTotal as $filename => $data) { if (version_compare(PHPUnit_Runner_Version::id(), '3.5.0') >= 0) { $ignoreLines = PHP_CodeCoverage_Util::getLinesToBeIgnored($filename); } else { // FIXME retrieve ignored lines for PHPUnit Version < 3.5.0 $ignoreLines = array(); } $lines = array(); $filename = strtolower($filename); if ($props->getProperty($filename) != null) { foreach ($data as $_line => $_data) { if (is_array($_data)) { $count = count($_data); } else { if (isset($ignoreLines[$_line])) { // line is marked as ignored $count = 1; } else { if ($_data == -1) { // not executed $count = -1; } else { if ($_data == -2) { // dead code $count = -2; } } } } $lines[$_line] = $count; } ksort($lines); $file = unserialize($props->getProperty($filename)); $left = $file['coverage']; $coverageMerged = CoverageMerger::mergeCodeCoverage($left, $lines); $file['coverage'] = $coverageMerged; $props->setProperty($filename, serialize($file)); } } $props->store($database); }
/** * @param PHP_CodeCoverage $coverage * @param string $target */ public function process(PHP_CodeCoverage $coverage, $target) { $target = PHP_CodeCoverage_Util::getDirectory($target); $files = $coverage->getSummary(); $commonPath = PHP_CodeCoverage_Util::reducePaths($files); $items = PHP_CodeCoverage_Util::buildDirectoryStructure($files); $root = new PHP_CodeCoverage_Report_HTML_Node_Directory($commonPath, NULL); $this->addItems($root, $items, $files); $this->renderDashboard($root, $target . 'index.dashboard.html', $this->options['title']); foreach ($root as $node) { if ($node instanceof PHP_CodeCoverage_Report_HTML_Node_Directory) { $this->renderDashboard($node, $target . PHP_CodeCoverage_Util::getSafeFilename($node->getId()) . '.dashboard.html', $node->getName(TRUE)); } } $root->render($target, $this->options['title'], $this->options['charset'], $this->options['lowUpperBound'], $this->options['highLowerBound'], $this->options['generator']); $this->copyFiles($target); }
/** * Calculates coverage statistics for the file. * */ protected function calculateStatistics() { $this->processClasses(); $this->processFunctions(); $max = count($this->codeLines); for ($lineNumber = 1; $lineNumber <= $max; $lineNumber++) { if (isset($this->startLines[$lineNumber])) { // Start line of a class. if (isset($this->startLines[$lineNumber]['methods'])) { $currentClass =& $this->startLines[$lineNumber]; } else { $currentMethod =& $this->startLines[$lineNumber]; } } if (isset($this->executedLines[$lineNumber])) { // Array: Line is executable and was executed. if (is_array($this->executedLines[$lineNumber])) { if (isset($currentClass)) { $currentClass['executableLines']++; $currentClass['executedLines']++; } if (isset($currentMethod)) { $currentMethod['executableLines']++; $currentMethod['executedLines']++; } $this->numExecutableLines++; $this->numExecutedLines++; } else { if ($this->executedLines[$lineNumber] == -1) { if (isset($currentClass)) { $currentClass['executableLines']++; } if (isset($currentMethod)) { $currentMethod['executableLines']++; } $this->numExecutableLines++; if (isset($this->ignoredLines[$lineNumber])) { if (isset($currentClass)) { $currentClass['executedLines']++; } if (isset($currentMethod)) { $currentMethod['executedLines']++; } $this->numExecutedLines++; } } } } if (isset($this->endLines[$lineNumber])) { // End line of a class. if (isset($this->endLines[$lineNumber]['methods'])) { unset($currentClass); } else { unset($currentMethod); } } } foreach ($this->classes as $className => &$class) { foreach ($class['methods'] as &$method) { if ($method['executableLines'] > 0) { $method['coverage'] = $method['executedLines'] / $method['executableLines'] * 100; } else { $method['coverage'] = 100; } $method['crap'] = PHP_CodeCoverage_Util::crap($method['ccn'], $method['coverage']); $class['ccn'] += $method['ccn']; } if ($className != '*') { if ($class['executableLines'] > 0) { $class['coverage'] = $class['executedLines'] / $class['executableLines'] * 100; } else { $class['coverage'] = 100; } if ($class['coverage'] == 100) { $this->numTestedClasses++; } $class['crap'] = PHP_CodeCoverage_Util::crap($class['ccn'], $class['coverage']); } } }
/** * @param PHP_CodeCoverage $coverage * @param string $target * @param string $name * @return string */ public function process(PHP_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 = array(); $report = $coverage->getReport(); unset($coverage); foreach ($report as $item) { $namespace = 'global'; if (!$item instanceof PHP_CodeCoverage_Report_Node_File) { continue; } $xmlFile = $xmlDocument->createElement('file'); $xmlFile->setAttribute('name', $item->getPath()); $classes = array_merge($item->getClasses(), $item->getTraits()); $coverage = $item->getCoverageData(); $lines = array(); $ignoredLines = $item->getIgnoredLines(); foreach ($classes as $className => $class) { $classStatements = 0; $coveredClassStatements = 0; $coveredMethods = 0; foreach ($class['methods'] as $methodName => $method) { $methodCount = 0; $methodLines = 0; $methodLinesCovered = 0; for ($i = $method['startLine']; $i <= $method['endLine']; $i++) { if (isset($ignoredLines[$i])) { continue; } $add = TRUE; $count = 0; if (isset($coverage[$i])) { if ($coverage[$i] !== NULL) { $classStatements++; $methodLines++; } else { $add = FALSE; } $count = count($coverage[$i]); if ($count > 0) { $coveredClassStatements++; $methodLinesCovered++; } } else { $add = FALSE; } $methodCount = max($methodCount, $count); if ($add) { $lines[$i] = array('count' => $count, 'type' => 'stmt'); } } if ($methodCount > 0) { $coveredMethods++; } $lines[$method['startLine']] = array('count' => $methodCount, 'crap' => PHP_CodeCoverage_Util::crap($method['ccn'], PHP_CodeCoverage_Util::percent($methodLinesCovered, $methodLines)), 'type' => 'method', '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('methods', count($class['methods'])); $xmlMetrics->setAttribute('coveredmethods', $coveredMethods); $xmlMetrics->setAttribute('conditionals', 0); $xmlMetrics->setAttribute('coveredconditionals', 0); $xmlMetrics->setAttribute('statements', $classStatements); $xmlMetrics->setAttribute('coveredstatements', $coveredClassStatements); $xmlMetrics->setAttribute('elements', count($class['methods']) + $classStatements); $xmlMetrics->setAttribute('coveredelements', $coveredMethods + $coveredClassStatements); $xmlClass->appendChild($xmlMetrics); } foreach ($coverage as $line => $data) { if ($data === NULL || isset($lines[$line]) || isset($ignoredLines[$line])) { continue; } $lines[$line] = array('count' => count($data), 'type' => 'stmt'); } ksort($lines); foreach ($lines as $line => $data) { if (isset($ignoredLines[$line])) { continue; } $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['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->getNumClasses()); $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->getNumClasses()); $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); if ($target !== NULL) { if (!is_dir(dirname($target))) { mkdir(dirname($target), 0777, TRUE); } return $xmlDocument->save($target); } else { return $xmlDocument->saveXML(); } }
/** * Calculates coverage statistics for the file. */ protected function calculateStatistics() { if ($this->cacheTokens) { $tokens = PHP_Token_Stream_CachingFactory::get($this->getPath()); } else { $tokens = new PHP_Token_Stream($this->getPath()); } $this->processClasses($tokens); $this->processTraits($tokens); $this->processFunctions($tokens); $this->linesOfCode = $tokens->getLinesOfCode(); unset($tokens); for ($lineNumber = 1; $lineNumber <= $this->linesOfCode['loc']; $lineNumber++) { if (isset($this->startLines[$lineNumber])) { // Start line of a class. if (isset($this->startLines[$lineNumber]['className'])) { $currentClass =& $this->startLines[$lineNumber]; } else { if (isset($this->startLines[$lineNumber]['traitName'])) { $currentTrait =& $this->startLines[$lineNumber]; } else { if (isset($this->startLines[$lineNumber]['methodName'])) { $currentMethod =& $this->startLines[$lineNumber]; } else { if (isset($this->startLines[$lineNumber]['functionName'])) { $currentFunction =& $this->startLines[$lineNumber]; } } } } } if (!isset($this->ignoredLines[$lineNumber]) && isset($this->coverageData[$lineNumber]) && $this->coverageData[$lineNumber] !== NULL) { if (isset($currentClass)) { $currentClass['executableLines']++; } if (isset($currentTrait)) { $currentTrait['executableLines']++; } if (isset($currentMethod)) { $currentMethod['executableLines']++; } if (isset($currentFunction)) { $currentFunction['executableLines']++; } $this->numExecutableLines++; if (count($this->coverageData[$lineNumber]) > 0 || isset($this->ignoredLines[$lineNumber])) { if (isset($currentClass)) { $currentClass['executedLines']++; } if (isset($currentTrait)) { $currentTrait['executedLines']++; } if (isset($currentMethod)) { $currentMethod['executedLines']++; } if (isset($currentFunction)) { $currentFunction['executedLines']++; } $this->numExecutedLines++; } } if (isset($this->endLines[$lineNumber])) { // End line of a class. if (isset($this->endLines[$lineNumber]['className'])) { unset($currentClass); } else { if (isset($this->endLines[$lineNumber]['traitName'])) { unset($currentTrait); } else { if (isset($this->endLines[$lineNumber]['methodName'])) { unset($currentMethod); } else { if (isset($this->endLines[$lineNumber]['functionName'])) { unset($currentFunction); } } } } } } foreach ($this->traits as $traitName => &$trait) { foreach ($trait['methods'] as &$method) { if ($method['executableLines'] > 0) { $method['coverage'] = $method['executedLines'] / $method['executableLines'] * 100; } else { $method['coverage'] = 100; } $method['crap'] = PHP_CodeCoverage_Util::crap($method['ccn'], $method['coverage']); $trait['ccn'] += $method['ccn']; } if ($trait['executableLines'] > 0) { $trait['coverage'] = $trait['executedLines'] / $trait['executableLines'] * 100; } else { $trait['coverage'] = 100; } if ($trait['coverage'] == 100) { $this->numTestedClasses++; } $trait['crap'] = PHP_CodeCoverage_Util::crap($trait['ccn'], $trait['coverage']); } foreach ($this->classes as $className => &$class) { foreach ($class['methods'] as &$method) { if ($method['executableLines'] > 0) { $method['coverage'] = $method['executedLines'] / $method['executableLines'] * 100; } else { $method['coverage'] = 100; } $method['crap'] = PHP_CodeCoverage_Util::crap($method['ccn'], $method['coverage']); $class['ccn'] += $method['ccn']; } if ($class['executableLines'] > 0) { $class['coverage'] = $class['executedLines'] / $class['executableLines'] * 100; } else { $class['coverage'] = 100; } if ($class['coverage'] == 100) { $this->numTestedClasses++; } $class['crap'] = PHP_CodeCoverage_Util::crap($class['ccn'], $class['coverage']); } }
/** * @param string $target * @param string $title * @param string $charset * @param integer $lowUpperBound * @param integer $highLowerBound * @param string $generator */ protected function doRender($target, $title, $charset, $lowUpperBound, $highLowerBound, $generator) { $cleanId = PHP_CodeCoverage_Util::getSafeFilename($this->getId()); $file = $target . $cleanId . '.html'; $template = new Text_Template(PHP_CodeCoverage_Report_HTML::$templatePath . 'directory.html'); $this->setTemplateVars($template, $title, $charset, $generator); $template->setVar(array('total_item' => $this->renderTotalItem($lowUpperBound, $highLowerBound), 'items' => $this->renderItems($lowUpperBound, $highLowerBound), 'low_upper_bound' => $lowUpperBound, 'high_lower_bound' => $highLowerBound)); $template->renderTo($file); $this->directories = array(); $this->files = array(); }
public static function merge($project, $codeCoverageInformation) { $props = self::_getDatabase($project); $coverageTotal = $codeCoverageInformation; foreach ($coverageTotal as $filename => $data) { $ignoreLines = PHP_CodeCoverage_Util::getLinesToBeIgnored($filename); $lines = array(); $filename = strtolower($filename); if ($props->getProperty($filename) != null) { foreach ($data as $_line => $_data) { if (is_array($_data)) { $count = count($_data); } else { if (isset($ignoreLines[$_line])) { // line is marked as ignored $count = 1; } else { if ($_data == -1) { // not executed $count = -1; } else { if ($_data == -2) { // dead code $count = -2; } } } } $lines[$_line] = $count; } ksort($lines); $file = unserialize($props->getProperty($filename)); $left = $file['coverage']; $coverageMerged = CoverageMerger::mergeCodeCoverage($left, $lines); $file['coverage'] = $coverageMerged; $props->setProperty($filename, serialize($file)); } } $props->store(); }
protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $presicion) { $format = '%' . $presicion . 's'; return PHP_CodeCoverage_Util::percent($numberOfCoveredElements, $totalNumberOfElements, true, true) . ' (' . sprintf($format, $numberOfCoveredElements) . '/' . sprintf($format, $totalNumberOfElements) . ')'; }
/** * @covers PHP_CodeCoverage_Util::percent */ public function testPercent() { $this->assertEquals(100, PHP_CodeCoverage_Util::percent(100, 0)); $this->assertEquals(100, PHP_CodeCoverage_Util::percent(100, 100)); $this->assertEquals('100.00%', PHP_CodeCoverage_Util::percent(100, 100, TRUE)); }
/** * @param array $items * @param Text_Template $template * * @return string */ protected function renderTraitOrClassItems(array $items, Text_Template $template, Text_Template $methodItemTemplate) { if (empty($items)) { return ''; } $buffer = ''; foreach ($items as $name => $item) { $numMethods = count($item['methods']); $numTestedMethods = 0; foreach ($item['methods'] as $method) { if ($method['executedLines'] == $method['executableLines']) { $numTestedMethods++; } } $buffer .= $this->renderItemTemplate($template, array('name' => $name, 'numClasses' => 1, 'numTestedClasses' => $numTestedMethods == $numMethods ? 1 : 0, 'numMethods' => $numMethods, 'numTestedMethods' => $numTestedMethods, 'linesExecutedPercent' => PHP_CodeCoverage_Util::percent($item['executedLines'], $item['executableLines'], false), 'linesExecutedPercentAsString' => PHP_CodeCoverage_Util::percent($item['executedLines'], $item['executableLines'], true), 'numExecutedLines' => $item['executedLines'], 'numExecutableLines' => $item['executableLines'], 'testedMethodsPercent' => PHP_CodeCoverage_Util::percent($numTestedMethods, $numMethods, false), 'testedMethodsPercentAsString' => PHP_CodeCoverage_Util::percent($numTestedMethods, $numMethods, true), 'testedClassesPercent' => PHP_CodeCoverage_Util::percent($numTestedMethods == $numMethods ? 1 : 0, 1, false), 'testedClassesPercentAsString' => PHP_CodeCoverage_Util::percent($numTestedMethods == $numMethods ? 1 : 0, 1, true), 'crap' => $item['crap'])); foreach ($item['methods'] as $method) { $buffer .= $this->renderFunctionOrMethodItem($methodItemTemplate, $method, ' '); } } return $buffer; }
/** * Prefills the blacklist with source files used by PHPUnit * and PHP_CodeCoverage. */ public function __construct() { $functions = array('file_iterator_autoload', 'php_codecoverage_autoload', 'php_invoker_autoload', 'php_timer_autoload', 'php_tokenstream_autoload', 'phpunit_autoload', 'phpunit_dbunit_autoload', 'phpunit_mockobject_autoload', 'phpunit_selenium_autoload', 'phpunit_story_autoload', 'text_template_autoload'); foreach ($functions as $function) { if (function_exists($function)) { $this->addFilesToBlacklist($function()); } } $file = PHP_CodeCoverage_Util::fileExistsInIncludePath('SymfonyComponents/YAML/sfYaml.php'); if ($file) { $this->addFileToBlacklist($file); } $file = PHP_CodeCoverage_Util::fileExistsInIncludePath('SymfonyComponents/YAML/sfYamlDumper.php'); if ($file) { $this->addFileToBlacklist($file); } }
/** * @param Text_Template $template * @return string */ protected function renderFunctionOrMethodItem(Text_Template $template, array $item, $indent = '') { $numTestedItems = $item['executedLines'] == $item['executableLines'] ? 1 : 0; return $this->renderItemTemplate($template, array('name' => sprintf('%s<a href="#%d"><abbr title="%s">%s</abbr></a>', $indent, $item['startLine'], htmlspecialchars($item['signature']), isset($item['functionName']) ? $item['functionName'] : $item['methodName']), 'numMethods' => 1, 'numTestedMethods' => $numTestedItems, 'linesExecutedPercent' => PHP_CodeCoverage_Util::percent($item['executedLines'], $item['executableLines'], false), 'linesExecutedPercentAsString' => PHP_CodeCoverage_Util::percent($item['executedLines'], $item['executableLines'], true), 'numExecutedLines' => $item['executedLines'], 'numExecutableLines' => $item['executableLines'], 'testedMethodsPercent' => PHP_CodeCoverage_Util::percent($numTestedItems, 1, false), 'testedMethodsPercentAsString' => PHP_CodeCoverage_Util::percent($numTestedItems, 1, true), 'crap' => $item['crap'])); }
/** * Constructor. * * @param string $name * @param PHP_CodeCoverage_Report_Node $parent * @param array $coverageData * @param array $testData * @param boolean $cacheTokens * @throws PHP_CodeCoverage_Exception */ public function __construct($name, PHP_CodeCoverage_Report_Node $parent, array $coverageData, array $testData, $cacheTokens) { if (!is_bool($cacheTokens)) { throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(1, 'boolean'); } parent::__construct($name, $parent); $this->coverageData = $coverageData; $this->testData = $testData; $this->ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored($this->getPath(), $cacheTokens); $this->cacheTokens = $cacheTokens; $this->calculateStatistics(); }
/** * @param Text_Template $template * @param string $title * @param string $charset * @param string $generator */ protected function setTemplateVars(Text_Template $template, $title, $charset, $generator) { $dashboard = ''; if ($this instanceof PHP_CodeCoverage_Report_HTML_Node_Directory) { $dashboard = sprintf('<a href="%s">dashboard</a>', PHP_CodeCoverage_Util::getSafeFilename($this->getId()) . '.dashboard.html'); } $template->setVar(array('title' => $title, 'charset' => $charset, 'link' => $this->getLink(TRUE), 'dashboard_link' => $dashboard, 'num_executable_lines' => $this->getNumExecutableLines(), 'num_executed_lines' => $this->getNumExecutedLines(), 'lines_executed_percent' => $this->getLineExecutedPercent(), 'date' => date('D M j G:i:s T Y', $_SERVER['REQUEST_TIME']), 'version' => '@package_version@', 'php_version' => PHP_VERSION, 'generator' => $generator)); }
/** * Applies the @covers annotation filtering. * * @param array $data * @param mixed $id */ protected function applyCoversAnnotationFilter(&$data, $id) { if ($id instanceof PHPUnit_Framework_TestCase) { $testClassName = get_class($id); $linesToBeCovered = PHP_CodeCoverage_Util::getLinesToBeCovered($testClassName, $id->getName()); if ($this->mapTestClassNameToCoveredClassName && empty($linesToBeCovered)) { $testedClass = substr($testClassName, 0, -4); if (class_exists($testedClass)) { $class = new ReflectionClass($testedClass); $linesToBeCovered = array($class->getFileName() => range($class->getStartLine(), $class->getEndLine())); } } } else { $linesToBeCovered = array(); } if (!empty($linesToBeCovered)) { $data = array_intersect_key($data, $linesToBeCovered); foreach (array_keys($data) as $filename) { $data[$filename] = array_intersect_key($data[$filename], array_flip($linesToBeCovered[$filename])); } } else { if ($this->forceCoversAnnotation) { $data = array(); } } }
public function setNumFunctions($count, $tested) { $this->functionsNode->setAttribute('count', $count); $this->functionsNode->setAttribute('tested', $tested); $this->functionsNode->setAttribute('percent', PHP_CodeCoverage_Util::percent($tested, $count, true)); }
/** * Returns the percentage of executed lines. * * @param boolean $asString * @return integer */ public function getLineExecutedPercent($asString = TRUE) { return PHP_CodeCoverage_Util::percent($this->getNumExecutedLines(), $this->getNumExecutableLines(), $asString); }
/** * @param PHP_CodeCoverage $coverage * @param string $target * @param string $name * @return string */ public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL) { $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $root = $document->createElement('coverage'); $root->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']); $document->appendChild($root); $project = $document->createElement('project'); $project->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']); if (is_string($name)) { $project->setAttribute('name', $name); } $root->appendChild($project); $files = $coverage->getSummary(); $packages = array(); $projectStatistics = array('files' => 0, 'loc' => 0, 'ncloc' => 0, 'classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); foreach ($files as $filename => $data) { $namespace = 'global'; if (file_exists($filename)) { $fileStatistics = array('classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); $file = $document->createElement('file'); $file->setAttribute('name', $filename); $tokens = PHP_Token_Stream_CachingFactory::get($filename); $classesInFile = $tokens->getClasses(); $linesOfCode = $tokens->getLinesOfCode(); unset($tokens); $ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored($filename); $lines = array(); foreach ($classesInFile as $className => $_class) { $classStatistics = array('methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); foreach ($_class['methods'] as $methodName => $method) { $classStatistics['methods']++; $methodCount = 0; $methodLines = 0; $methodLinesCovered = 0; for ($i = $method['startLine']; $i <= $method['endLine']; $i++) { if (isset($ignoredLines[$i])) { continue; } $add = TRUE; $count = 0; if (isset($files[$filename][$i])) { if ($files[$filename][$i] != -2) { $classStatistics['statements']++; $methodLines++; } if (is_array($files[$filename][$i])) { $classStatistics['coveredStatements']++; $methodLinesCovered++; $count = count($files[$filename][$i]); } else { if ($files[$filename][$i] == -2) { $add = FALSE; } } } else { $add = FALSE; } $methodCount = max($methodCount, $count); if ($add) { $lines[$i] = array('count' => $count, 'type' => 'stmt'); } } if ($methodCount > 0) { $classStatistics['coveredMethods']++; } $lines[$method['startLine']] = array('count' => $methodCount, 'crap' => PHP_CodeCoverage_Util::crap($method['ccn'], PHP_CodeCoverage_Util::percent($methodLinesCovered, $methodLines)), 'type' => 'method', 'name' => $methodName); } $package = PHP_CodeCoverage_Util::getPackageInformation($className, $_class['docblock']); if (!empty($package['namespace'])) { $namespace = $package['namespace']; } $class = $document->createElement('class'); $class->setAttribute('name', $className); $class->setAttribute('namespace', $namespace); if (!empty($package['fullPackage'])) { $class->setAttribute('fullPackage', $package['fullPackage']); } if (!empty($package['category'])) { $class->setAttribute('category', $package['category']); } if (!empty($package['package'])) { $class->setAttribute('package', $package['package']); } if (!empty($package['subpackage'])) { $class->setAttribute('subpackage', $package['subpackage']); } $file->appendChild($class); $metrics = $document->createElement('metrics'); $metrics->setAttribute('methods', $classStatistics['methods']); $metrics->setAttribute('coveredmethods', $classStatistics['coveredMethods']); $metrics->setAttribute('conditionals', $classStatistics['conditionals']); $metrics->setAttribute('coveredconditionals', $classStatistics['coveredConditionals']); $metrics->setAttribute('statements', $classStatistics['statements']); $metrics->setAttribute('coveredstatements', $classStatistics['coveredStatements']); $metrics->setAttribute('elements', $classStatistics['conditionals'] + $classStatistics['statements'] + $classStatistics['methods']); $metrics->setAttribute('coveredelements', $classStatistics['coveredConditionals'] + $classStatistics['coveredStatements'] + $classStatistics['coveredMethods']); $class->appendChild($metrics); $fileStatistics['methods'] += $classStatistics['methods']; $fileStatistics['coveredMethods'] += $classStatistics['coveredMethods']; $fileStatistics['conditionals'] += $classStatistics['conditionals']; $fileStatistics['coveredConditionals'] += $classStatistics['coveredConditionals']; $fileStatistics['statements'] += $classStatistics['statements']; $fileStatistics['coveredStatements'] += $classStatistics['coveredStatements']; $fileStatistics['classes']++; } foreach ($data as $_line => $_data) { if (isset($lines[$_line]) || isset($ignoredLines[$_line])) { continue; } if ($_data != -2) { $fileStatistics['statements']++; if (is_array($_data)) { $count = count($_data); $fileStatistics['coveredStatements']++; } else { $count = 0; } $lines[$_line] = array('count' => $count, 'type' => 'stmt'); } } ksort($lines); foreach ($lines as $_line => $_data) { if (isset($ignoredLines[$_line])) { continue; } $line = $document->createElement('line'); $line->setAttribute('num', $_line); $line->setAttribute('type', $_data['type']); if (isset($_data['name'])) { $line->setAttribute('name', $_data['name']); } if (isset($_data['crap'])) { $line->setAttribute('crap', $_data['crap']); } $line->setAttribute('count', $_data['count']); $file->appendChild($line); } $metrics = $document->createElement('metrics'); $metrics->setAttribute('loc', $linesOfCode['loc']); $metrics->setAttribute('ncloc', $linesOfCode['ncloc']); $metrics->setAttribute('classes', $fileStatistics['classes']); $metrics->setAttribute('methods', $fileStatistics['methods']); $metrics->setAttribute('coveredmethods', $fileStatistics['coveredMethods']); $metrics->setAttribute('conditionals', $fileStatistics['conditionals']); $metrics->setAttribute('coveredconditionals', $fileStatistics['coveredConditionals']); $metrics->setAttribute('statements', $fileStatistics['statements']); $metrics->setAttribute('coveredstatements', $fileStatistics['coveredStatements']); $metrics->setAttribute('elements', $fileStatistics['conditionals'] + $fileStatistics['statements'] + $fileStatistics['methods']); $metrics->setAttribute('coveredelements', $fileStatistics['coveredConditionals'] + $fileStatistics['coveredStatements'] + $fileStatistics['coveredMethods']); $file->appendChild($metrics); if ($namespace == 'global') { $project->appendChild($file); } else { if (!isset($packages[$namespace])) { $packages[$namespace] = $document->createElement('package'); $packages[$namespace]->setAttribute('name', $namespace); $project->appendChild($packages[$namespace]); } $packages[$namespace]->appendChild($file); } $projectStatistics['loc'] += $linesOfCode['loc']; $projectStatistics['ncloc'] += $linesOfCode['ncloc']; $projectStatistics['classes'] += $fileStatistics['classes']; $projectStatistics['methods'] += $fileStatistics['methods']; $projectStatistics['coveredMethods'] += $fileStatistics['coveredMethods']; $projectStatistics['conditionals'] += $fileStatistics['conditionals']; $projectStatistics['coveredConditionals'] += $fileStatistics['coveredConditionals']; $projectStatistics['statements'] += $fileStatistics['statements']; $projectStatistics['coveredStatements'] += $fileStatistics['coveredStatements']; $projectStatistics['files']++; } } $metrics = $document->createElement('metrics'); $metrics->setAttribute('files', $projectStatistics['files']); $metrics->setAttribute('loc', $projectStatistics['loc']); $metrics->setAttribute('ncloc', $projectStatistics['ncloc']); $metrics->setAttribute('classes', $projectStatistics['classes']); $metrics->setAttribute('methods', $projectStatistics['methods']); $metrics->setAttribute('coveredmethods', $projectStatistics['coveredMethods']); $metrics->setAttribute('conditionals', $projectStatistics['conditionals']); $metrics->setAttribute('coveredconditionals', $projectStatistics['coveredConditionals']); $metrics->setAttribute('statements', $projectStatistics['statements']); $metrics->setAttribute('coveredstatements', $projectStatistics['coveredStatements']); $metrics->setAttribute('elements', $projectStatistics['conditionals'] + $projectStatistics['statements'] + $projectStatistics['methods']); $metrics->setAttribute('coveredelements', $projectStatistics['coveredConditionals'] + $projectStatistics['coveredStatements'] + $projectStatistics['coveredMethods']); $project->appendChild($metrics); if ($target !== NULL) { return $document->save($target); } else { return $document->saveXML(); } }
/** * @param PHP_CodeCoverage $coverage * @param string $target */ public function process(PHP_CodeCoverage $coverage, $target) { $target = PHP_CodeCoverage_Util::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 PHP_CodeCoverage_Report_HTML_Renderer_Dashboard($this->templatePath, $this->charset, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound); $directory = new PHP_CodeCoverage_Report_HTML_Renderer_Directory($this->templatePath, $this->charset, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound); $file = new PHP_CodeCoverage_Report_HTML_Renderer_File($this->templatePath, $this->charset, $this->generator, $date, $this->lowUpperBound, $this->highLowerBound, $this->highlight, $this->yui); $dashboard->render($report, $target . 'index.dashboard.html', $this->title); $directory->render($report, $target . 'index.html', $this->title); foreach ($report as $node) { $id = $node->getId(); if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) { $dashboard->render($node, $target . $id . '.dashboard.html'); $directory->render($node, $target . $id . '.html'); } else { $file->render($node, $target . $id . '.html'); } } $this->copyFiles($target); }