|
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 Analyzer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* pdepend XML filename. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $xml = 'pdepend.xml'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* cauditor JSON filename. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $json = 'cauditor.json'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $buildPath; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Config |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Config $config |
|
38
|
|
|
* |
|
39
|
|
|
* @throws Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Config $config) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->config = $config; |
|
44
|
|
|
|
|
45
|
|
|
// all paths in build_path are relative to project root, which may not |
|
46
|
|
|
// be where this code is run from, so prepend the project root! |
|
47
|
|
|
$this->buildPath = $this->config['path'].DIRECTORY_SEPARATOR.$this->config['build_path']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $api |
|
52
|
|
|
* |
|
53
|
|
|
* @return string|bool API response (on success) or false (on failure) |
|
54
|
|
|
* |
|
55
|
|
|
* @throws Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function run($api) |
|
58
|
|
|
{ |
|
59
|
|
|
exec('mkdir -p '.$this->buildPath, $output, $result); |
|
60
|
|
|
if ($result !== 0) { |
|
61
|
|
|
throw new Exception('Unable to create build directory.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// let pdepend generate all metrics we'll need |
|
65
|
|
|
$this->pdepend(); |
|
66
|
|
|
|
|
67
|
|
|
// convert pdepend xml into cauditor json & store to file |
|
68
|
|
|
$json = $this->convert($this->buildPath.DIRECTORY_SEPARATOR.$this->xml); |
|
69
|
|
|
file_put_contents($this->buildPath.DIRECTORY_SEPARATOR.$this->json, $json); |
|
70
|
|
|
|
|
71
|
|
|
$data = $this->sniff(); |
|
72
|
|
|
// if we expect these json files to be loaded client-side to render |
|
73
|
|
|
// the charts, might as well assume it'll fit in this machine's |
|
74
|
|
|
// memory to submit it to our API ;) |
|
75
|
|
|
$data['json'] = $json; |
|
76
|
|
|
|
|
77
|
|
|
return $this->transmit($api, $data); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Runs pdepend to generate the metrics. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function pdepend() |
|
86
|
|
|
{ |
|
87
|
|
|
$path = $this->config['path']; |
|
88
|
|
|
$xml = $this->buildPath.DIRECTORY_SEPARATOR.$this->xml; |
|
89
|
|
|
$exclude = implode(',', $this->config['exclude_folders']); |
|
90
|
|
|
|
|
91
|
|
|
$command = "vendor/bin/pdepend --summary-xml=$xml --ignore=$exclude $path"; |
|
92
|
|
|
exec($command, $output, $result); |
|
93
|
|
|
if ($result !== 0) { |
|
94
|
|
|
throw new Exception('Unable to generate pdepend metrics.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Fetch build data from CI. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string[] |
|
102
|
|
|
* |
|
103
|
|
|
* @throws Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function sniff() |
|
106
|
|
|
{ |
|
107
|
|
|
$build = exec('vendor/bin/ci-sniffer', $output, $result); |
|
108
|
|
|
if ($result !== 0) { |
|
109
|
|
|
throw new Exception('Unable to get build details.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return (array) json_decode($build); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Transform pdepend output into the (more succinct) format cauditor |
|
117
|
|
|
* understands. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $xml |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
* |
|
123
|
|
|
* @throws Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function convert($xml) |
|
126
|
|
|
{ |
|
127
|
|
|
$reader = new XMLReader(); |
|
128
|
|
|
$reader->open($xml); |
|
129
|
|
|
|
|
130
|
|
|
$converter = new Converter(); |
|
131
|
|
|
$json = $converter->convert($reader); |
|
132
|
|
|
|
|
133
|
|
|
$reader->close(); |
|
134
|
|
|
|
|
135
|
|
|
return $json; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Submit the data to cauditor API. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $api |
|
142
|
|
|
* @param string[] $data |
|
143
|
|
|
* |
|
144
|
|
|
* @return string|bool API response (on success) or false (on failure) |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function transmit($api, $data) |
|
147
|
|
|
{ |
|
148
|
|
|
$options = array( |
|
149
|
|
|
CURLOPT_URL => $api, |
|
150
|
|
|
CURLOPT_FOLLOWLOCATION => 1, |
|
151
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
152
|
|
|
CURLOPT_POST => 1, |
|
153
|
|
|
CURLOPT_POSTFIELDS => $data, |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$curl = curl_init(); |
|
157
|
|
|
curl_setopt_array($curl, $options); |
|
158
|
|
|
$result = curl_exec($curl); |
|
159
|
|
|
curl_close($curl); |
|
160
|
|
|
|
|
161
|
|
|
return $result; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|