|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cauditor; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Matthias Mullie <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved. |
|
8
|
|
|
* @license LICENSE MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
class Converter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Processes XML node by node, extracts the relevant metrics & writes them |
|
14
|
|
|
* piecemeal to a JSON file. |
|
15
|
|
|
* |
|
16
|
|
|
* @param XMLReader $reader XML document to read from. |
|
17
|
|
|
* |
|
18
|
|
|
* @return string |
|
19
|
|
|
* |
|
20
|
|
|
* @throws Exception |
|
21
|
|
|
*/ |
|
22
|
|
|
public function convert(XMLReader $reader) |
|
23
|
|
|
{ |
|
24
|
|
|
$reader->readNext('metrics'); |
|
25
|
|
|
|
|
26
|
|
|
$data = array( |
|
27
|
|
|
'loc' => $reader->getAttribute('eloc'), |
|
28
|
|
|
'noc' => $reader->getAttribute('noc'), |
|
29
|
|
|
'nom' => $reader->getAttribute('nom'), |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$json = json_encode($data); |
|
33
|
|
|
// data will be stored as json, but we need to fetch children, so |
|
34
|
|
|
// don't write the closing `]` & `}` yet |
|
35
|
|
|
return substr($json, 0, -1).',"children":['.$this->convertPackages($reader).']}'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function convertPackages(XMLReader $reader) |
|
39
|
|
|
{ |
|
40
|
|
|
$i = 0; |
|
41
|
|
|
$content = ''; |
|
42
|
|
|
while ($reader->readNext('package', 'metrics')) { |
|
43
|
|
|
$data = array('name' => $reader->getAttribute('name')); |
|
44
|
|
|
|
|
45
|
|
|
$json = json_encode($data); |
|
46
|
|
|
// add `,` between multiple nodes |
|
47
|
|
|
$json = $i++ === 0 ? $json : ','.$json; |
|
48
|
|
|
// data will be stored as json, but we need to fetch children, so |
|
49
|
|
|
// don't write the closing `]` & `}` yet |
|
50
|
|
|
$content .= substr($json, 0, -1).',"children":['.$this->convertClasses($reader).']}'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $content; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function convertClasses(XMLReader $reader) |
|
57
|
|
|
{ |
|
58
|
|
|
$i = 0; |
|
59
|
|
|
$content = ''; |
|
60
|
|
|
while ($reader->readNext('class', 'package')) { |
|
61
|
|
|
$data = array( |
|
62
|
|
|
'name' => $reader->getAttribute('name'), |
|
63
|
|
|
'loc' => (int) $reader->getAttribute('eloc'), |
|
64
|
|
|
'ca' => (int) $reader->getAttribute('ca'), |
|
65
|
|
|
'ce' => (int) $reader->getAttribute('ce'), |
|
66
|
|
|
'i' => (float) number_format((int) $reader->getAttribute('ce') / (((int) $reader->getAttribute('ce') + (int) $reader->getAttribute('ca')) ?: 1), 2), |
|
67
|
|
|
'dit' => (int) $reader->getAttribute('dit'), |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$json = json_encode($data); |
|
71
|
|
|
// add `,` between multiple nodes |
|
72
|
|
|
$json = $i++ === 0 ? $json : ','.$json; |
|
73
|
|
|
// data will be stored as json, but we need to fetch children, so |
|
74
|
|
|
// don't write the closing `]` & `}` yet |
|
75
|
|
|
$content .= substr($json, 0, -1).',"children":['.$this->convertMethods($reader).']}'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $content; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function convertMethods(XMLReader $reader) |
|
82
|
|
|
{ |
|
83
|
|
|
$i = 0; |
|
84
|
|
|
$content = ''; |
|
85
|
|
|
while ($reader->readNext('method', 'class')) { |
|
86
|
|
|
$data = array( |
|
87
|
|
|
'name' => $reader->getAttribute('name'), |
|
88
|
|
|
'loc' => (int) $reader->getAttribute('eloc'), |
|
89
|
|
|
'ccn' => (int) $reader->getAttribute('ccn2'), |
|
90
|
|
|
'npath' => (int) $reader->getAttribute('npath'), |
|
91
|
|
|
'he' => (float) number_format($reader->getAttribute('he'), 2), |
|
92
|
|
|
'hi' => (float) number_format($reader->getAttribute('hi'), 2), |
|
93
|
|
|
'mi' => (float) number_format($reader->getAttribute('mi'), 2), |
|
94
|
|
|
); |
|
95
|
|
|
$json = json_encode($data); |
|
96
|
|
|
// add `,` between multiple nodes |
|
97
|
|
|
$json = $i++ === 0 ? $json : ','.$json; |
|
98
|
|
|
|
|
99
|
|
|
$content .= $json; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $content; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|