upgrade zend (#1559)
[openemr.git] / vendor / phpunit / php-code-coverage / src / Report / Crap4j.php
blob7adf78fe39056b8617812398611c42e910815150
1 <?php
2 /*
3 * This file is part of the php-code-coverage package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
11 namespace SebastianBergmann\CodeCoverage\Report;
13 use SebastianBergmann\CodeCoverage\CodeCoverage;
14 use SebastianBergmann\CodeCoverage\Node\File;
15 use SebastianBergmann\CodeCoverage\InvalidArgumentException;
17 class Crap4j
19 /**
20 * @var int
22 private $threshold;
24 /**
25 * @param int $threshold
27 public function __construct($threshold = 30)
29 if (!is_int($threshold)) {
30 throw InvalidArgumentException::create(
32 'integer'
36 $this->threshold = $threshold;
39 /**
40 * @param CodeCoverage $coverage
41 * @param string $target
42 * @param string $name
44 * @return string
46 public function process(CodeCoverage $coverage, $target = null, $name = null)
48 $document = new \DOMDocument('1.0', 'UTF-8');
49 $document->formatOutput = true;
51 $root = $document->createElement('crap_result');
52 $document->appendChild($root);
54 $project = $document->createElement('project', is_string($name) ? $name : '');
55 $root->appendChild($project);
56 $root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME'])));
58 $stats = $document->createElement('stats');
59 $methodsNode = $document->createElement('methods');
61 $report = $coverage->getReport();
62 unset($coverage);
64 $fullMethodCount = 0;
65 $fullCrapMethodCount = 0;
66 $fullCrapLoad = 0;
67 $fullCrap = 0;
69 foreach ($report as $item) {
70 $namespace = 'global';
72 if (!$item instanceof File) {
73 continue;
76 $file = $document->createElement('file');
77 $file->setAttribute('name', $item->getPath());
79 $classes = $item->getClassesAndTraits();
81 foreach ($classes as $className => $class) {
82 foreach ($class['methods'] as $methodName => $method) {
83 $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']);
85 $fullCrap += $method['crap'];
86 $fullCrapLoad += $crapLoad;
87 $fullMethodCount++;
89 if ($method['crap'] >= $this->threshold) {
90 $fullCrapMethodCount++;
93 $methodNode = $document->createElement('method');
95 if (!empty($class['package']['namespace'])) {
96 $namespace = $class['package']['namespace'];
99 $methodNode->appendChild($document->createElement('package', $namespace));
100 $methodNode->appendChild($document->createElement('className', $className));
101 $methodNode->appendChild($document->createElement('methodName', $methodName));
102 $methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature'])));
103 $methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature'])));
104 $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap'])));
105 $methodNode->appendChild($document->createElement('complexity', $method['ccn']));
106 $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage'])));
107 $methodNode->appendChild($document->createElement('crapLoad', round($crapLoad)));
109 $methodsNode->appendChild($methodNode);
114 $stats->appendChild($document->createElement('name', 'Method Crap Stats'));
115 $stats->appendChild($document->createElement('methodCount', $fullMethodCount));
116 $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount));
117 $stats->appendChild($document->createElement('crapLoad', round($fullCrapLoad)));
118 $stats->appendChild($document->createElement('totalCrap', $fullCrap));
120 if ($fullMethodCount > 0) {
121 $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount);
122 } else {
123 $crapMethodPercent = 0;
126 $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent));
128 $root->appendChild($stats);
129 $root->appendChild($methodsNode);
131 $buffer = $document->saveXML();
133 if ($target !== null) {
134 if (!is_dir(dirname($target))) {
135 mkdir(dirname($target), 0777, true);
138 file_put_contents($target, $buffer);
141 return $buffer;
145 * @param float $crapValue
146 * @param int $cyclomaticComplexity
147 * @param float $coveragePercent
149 * @return float
151 private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent)
153 $crapLoad = 0;
155 if ($crapValue >= $this->threshold) {
156 $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100);
157 $crapLoad += $cyclomaticComplexity / $this->threshold;
160 return $crapLoad;
164 * @param float $value
166 * @return float
168 private function roundValue($value)
170 return round($value, 2);