Skip to content

Commit 61424af

Browse files
author
pepov
committed
implemented the bare minimum of the report to work with the crap4j hudson plugin
1 parent 6cd24da commit 61424af

File tree

1 file changed

+83
-213
lines changed

1 file changed

+83
-213
lines changed

PHP/CodeCoverage/Report/Crap4j.php

Lines changed: 83 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
class PHP_CodeCoverage_Report_Crap4j
99
{
10+
private $treshHold = 30;
11+
1012
/**
1113
* @param PHP_CodeCoverage $coverage
1214
* @param string $target
@@ -19,81 +21,25 @@ public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL
1921
$document->formatOutput = TRUE;
2022

2123
$root = $document->createElement('crap_result');
22-
2324
$document->appendChild($root);
2425

2526
$project = $document->createElement('project', is_string($name) ? $name : '');
26-
2727
$root->appendChild($project);
28-
2928
$root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int)$_SERVER['REQUEST_TIME'])));
3029

30+
$stats = $document->createElement('stats');
3131
$methodsNode = $document->createElement('methods');
3232

33-
$files = $coverage->getSummary();
34-
$packages = array();
35-
36-
$projectStatistics = array(
37-
'files' => 0,
38-
'loc' => 0,
39-
'ncloc' => 0,
40-
'classes' => 0,
41-
'methods' => 0,
42-
'coveredMethods' => 0,
43-
'conditionals' => 0,
44-
'coveredConditionals' => 0,
45-
'statements' => 0,
46-
'coveredStatements' => 0
47-
);
48-
49-
50-
/**
51-
* <methods>
52-
<method>
53-
<package>
54-
bancoagitar
55-
</package>
56-
<className>
57-
BancoAgitar
58-
</className>
59-
<methodName>
60-
&lt;init&gt;
61-
</methodName>
62-
<methodSignature>
63-
()V
64-
</methodSignature>
65-
<fullMethod>
66-
public void &lt;init&gt;()
67-
</fullMethod>
68-
<crap>
69-
1.00
70-
</crap>
71-
<complexity>
72-
1
73-
</complexity>
74-
<coverage>
75-
100.00
76-
</coverage>
77-
<crapLoad>
78-
0
79-
</crapLoad>
80-
</method>
81-
*/
33+
$files = $coverage->getSummary();
8234

83-
foreach ($files as $filename => $data) {
35+
$fullMethodCount = 0;
36+
$fullCrapMethodCount = 0;
37+
$fullCrapLoad = 0;
38+
$fullCrap = 0;
8439

85-
$namespace = 'global';
40+
foreach ($files as $filename => $data) {
8641

8742
if (file_exists($filename)) {
88-
$fileStatistics = array(
89-
'classes' => 0,
90-
'methods' => 0,
91-
'coveredMethods' => 0,
92-
'conditionals' => 0,
93-
'coveredConditionals' => 0,
94-
'statements' => 0,
95-
'coveredStatements' => 0
96-
);
9743

9844
$file = $document->createElement('file');
9945
$file->setAttribute('name', $filename);
@@ -131,81 +77,39 @@ public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL
13177

13278
foreach ($_class['methods'] as $methodName => $method) {
13379

80+
$methodCount = 0;
81+
$methodLines = 0;
82+
$methodLinesCovered = 0;
83+
$this->methodsDetails($method, $ignoredLines, $files, $filename, $classStatistics, $lines, $methodCount, $methodLines, $methodLinesCovered);
13484

135-
$methodCount = 0;
136-
$methodLines = 0;
137-
$methodLinesCovered = 0;
138-
139-
for ($i = $method['startLine'];
140-
$i <= $method['endLine'];
141-
$i++) {
142-
if (isset($ignoredLines[$i])) {
143-
continue;
144-
}
145-
146-
$add = TRUE;
147-
$count = 0;
148-
149-
if (isset($files[$filename][$i])) {
150-
if ($files[$filename][$i] != -2) {
151-
$classStatistics['statements']++;
152-
$methodLines++;
153-
}
154-
155-
if (is_array($files[$filename][$i])) {
156-
$classStatistics['coveredStatements']++;
157-
$methodLinesCovered++;
158-
$count = count($files[$filename][$i]);
159-
}
160-
161-
else if ($files[$filename][$i] == -2) {
162-
$add = FALSE;
163-
}
164-
} else {
165-
$add = FALSE;
166-
}
167-
168-
$methodCount = max($methodCount, $count);
169-
170-
if ($add) {
171-
$lines[$i] = array(
172-
'count' => $count,
173-
'type' => 'stmt'
174-
);
175-
}
176-
}
177-
85+
$fullMethodCount++;
17886

17987
$coveragePercent = PHP_CodeCoverage_Util::percent(
18088
$methodLinesCovered,
18189
$methodLines
18290
);
18391
$crap = PHP_CodeCoverage_Util::crap($method['ccn'], $coveragePercent);
92+
$fullCrap += $crap;
93+
94+
if ($crap >= $this->treshHold) {
95+
$fullCrapMethodCount++;
96+
}
18497

98+
$crapLoad = $this->getCrapLoad($crap, $method['ccn'], $coveragePercent);
99+
100+
$fullCrapLoad += $crapLoad;
185101

186102
$methodNode = $document->createElement('method');
187103

188104
$methodNode->appendChild($document->createElement('package', ''));
189105
$methodNode->appendChild($document->createElement('className', $className));
190106
$methodNode->appendChild($document->createElement('methodName', $methodName));
191-
$methodNode->appendChild($document->createElement('methodSignature', $method['signature']));
192-
$methodNode->appendChild($document->createElement('fullMethod', sprintf('%s(%s)', $methodName, $method['signature'])));
107+
$methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature'])));
108+
$methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature'])));
193109
$methodNode->appendChild($document->createElement('crap', $crap));
194110
$methodNode->appendChild($document->createElement('complexity', $method['ccn']));
195111
$methodNode->appendChild($document->createElement('coverage', $coveragePercent));
196-
$methodNode->appendChild($document->createElement('crapLoad', 0));
197-
198-
/*
199-
* public int getCrapLoad(float crapThreshold) {
200-
int crapLoad = 0;
201-
if (getCrap() >= crapThreshold) {
202-
int complexity = getComplexity();
203-
float coverage = getCoverage();
204-
crapLoad += complexity * (1.0 - coverage);
205-
crapLoad += complexity / crapThreshold;
206-
}
207-
return crapLoad;
208-
}*/
112+
$methodNode->appendChild($document->createElement('crapLoad', $crapLoad));
209113

210114
$methodsNode->appendChild($methodNode);
211115

@@ -215,106 +119,20 @@ public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL
215119
$classStatistics['coveredMethods']++;
216120
}
217121
}
218-
219-
/*if (!empty($package['fullPackage'])) {
220-
$class->setAttribute(
221-
'fullPackage', $package['fullPackage']
222-
);
223-
}
224-
225-
if (!empty($package['category'])) {
226-
$class->setAttribute(
227-
'category', $package['category']
228-
);
229-
}
230-
231-
if (!empty($package['package'])) {
232-
$class->setAttribute(
233-
'package', $package['package']
234-
);
235-
}
236-
237-
if (!empty($package['subpackage'])) {
238-
$class->setAttribute(
239-
'subpackage', $package['subpackage']
240-
);
241-
}*/
242122
}
243123
}
244124
}
245125

246-
$root->appendChild($methodsNode);
247-
248-
$stats = $document->createElement('stats');
249-
250126
$stats->appendChild($document->createElement('name', 'Method Crap Stats'));
251127

252-
$stats->appendChild($document->createElement('totalCrap', 0));
253-
$stats->appendChild($document->createElement('crap', 0));
254-
$stats->appendChild($document->createElement('median', 0));
255-
$stats->appendChild($document->createElement('average', 0));
256-
$stats->appendChild($document->createElement('stdDev', 0));
257-
$stats->appendChild($document->createElement('methodCount', 0));
258-
$stats->appendChild($document->createElement('crapMethodCount', 0));
259-
$stats->appendChild($document->createElement('crapMethodPercent', 0));
260-
$stats->appendChild($document->createElement('crapLoad', 0));
261-
$stats->appendChild($document->createElement('crapThreshold', 0));
262-
$stats->appendChild($document->createElement('crapLoad', 0));
263-
$stats->appendChild($document->createElement('globalCraploadAverage', 0));
264-
$stats->appendChild($document->createElement('globalCrapMethodAverage', 0));
265-
$stats->appendChild($document->createElement('globalTotalMethodAverage', 0));
266-
$stats->appendChild($document->createElement('globalAverageDiff', 0));
267-
$stats->appendChild($document->createElement('globalCraploadAverageDiff', 0));
268-
$stats->appendChild($document->createElement('globalCrapMethodAverageDiff', 0));
269-
$stats->appendChild($document->createElement('globalTotalMethodAverageDiff', 0));
128+
$stats->appendChild($document->createElement('methodCount', $fullMethodCount));
129+
$stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount));
130+
$stats->appendChild($document->createElement('crapLoad', $fullCrapLoad));
131+
$stats->appendChild($document->createElement('totalCrap', $fullCrap));
132+
$stats->appendChild($document->createElement('crapMethodPercent', 100 * $fullCrapMethodCount / $fullMethodCount));
270133

271134
$root->appendChild($stats);
272-
273-
/*
274-
275-
$metrics = $document->createElement('metrics');
276-
277-
$metrics->setAttribute('files', $projectStatistics['files']);
278-
$metrics->setAttribute('loc', $projectStatistics['loc']);
279-
$metrics->setAttribute('ncloc', $projectStatistics['ncloc']);
280-
$metrics->setAttribute('classes', $projectStatistics['classes']);
281-
$metrics->setAttribute('methods', $projectStatistics['methods']);
282-
283-
$metrics->setAttribute(
284-
'coveredmethods', $projectStatistics['coveredMethods']
285-
);
286-
287-
$metrics->setAttribute(
288-
'conditionals', $projectStatistics['conditionals']
289-
);
290-
291-
$metrics->setAttribute(
292-
'coveredconditionals', $projectStatistics['coveredConditionals']
293-
);
294-
295-
$metrics->setAttribute(
296-
'statements', $projectStatistics['statements']
297-
);
298-
299-
$metrics->setAttribute(
300-
'coveredstatements', $projectStatistics['coveredStatements']
301-
);
302-
303-
$metrics->setAttribute(
304-
'elements',
305-
$projectStatistics['conditionals'] +
306-
$projectStatistics['statements'] +
307-
$projectStatistics['methods']
308-
);
309-
310-
$metrics->setAttribute(
311-
'coveredelements',
312-
$projectStatistics['coveredConditionals'] +
313-
$projectStatistics['coveredStatements'] +
314-
$projectStatistics['coveredMethods']
315-
);
316-
317-
$project->appendChild($metrics);*/
135+
$root->appendChild($methodsNode);
318136

319137
if ($target !== NULL) {
320138
if (!is_dir(dirname($target))) {
@@ -326,4 +144,56 @@ public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL
326144
return $document->saveXML();
327145
}
328146
}
147+
148+
private function methodsDetails($method, $ignoredLines, $files, $filename, $classStatistics, $lines, &$methodCount, &$methodLines, &$methodLinesCovered)
149+
{
150+
for ($i = $method['startLine'];
151+
$i <= $method['endLine'];
152+
$i++) {
153+
if (isset($ignoredLines[$i])) {
154+
continue;
155+
}
156+
157+
$add = TRUE;
158+
$count = 0;
159+
160+
if (isset($files[$filename][$i])) {
161+
if ($files[$filename][$i] != -2) {
162+
$classStatistics['statements']++;
163+
$methodLines++;
164+
}
165+
166+
if (is_array($files[$filename][$i])) {
167+
$classStatistics['coveredStatements']++;
168+
$methodLinesCovered++;
169+
$count = count($files[$filename][$i]);
170+
}
171+
172+
else if ($files[$filename][$i] == -2) {
173+
$add = FALSE;
174+
}
175+
} else {
176+
$add = FALSE;
177+
}
178+
179+
$methodCount = max($methodCount, $count);
180+
181+
if ($add) {
182+
$lines[$i] = array(
183+
'count' => $count,
184+
'type' => 'stmt'
185+
);
186+
}
187+
}
188+
}
189+
190+
public function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent)
191+
{
192+
$crapLoad = 0;
193+
if ($crapValue > $this->treshHold) {
194+
$crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100);
195+
$crapLoad += $cyclomaticComplexity / $this->treshHold;
196+
}
197+
return $crapLoad;
198+
}
329199
}

0 commit comments

Comments
 (0)