1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cauditor\Analyzers\PDepend; |
4
|
|
|
|
5
|
|
|
use Cauditor\Analyzers\AnalyzerInterface; |
6
|
|
|
use Cauditor\Config; |
7
|
|
|
use Cauditor\Exception; |
8
|
|
|
use MatthiasMullie\PathConverter\Converter as PathConverter; |
9
|
|
|
use PDepend\Application; |
10
|
|
|
use PDepend\Input\ExcludePathFilter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Matthias Mullie <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved. |
15
|
|
|
* @license LICENSE MIT |
16
|
|
|
*/ |
17
|
|
|
class Analyzer implements AnalyzerInterface |
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
|
|
|
public function __construct(Config $config) |
40
|
|
|
{ |
41
|
|
|
$this->config = $config; |
42
|
|
|
|
43
|
|
|
// all paths in build_path are relative to project root, which may not |
44
|
|
|
// be where this code is run from, so prepend the project root! |
45
|
|
|
$this->buildPath = $this->config['path'].DIRECTORY_SEPARATOR.$this->config['build_path']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
* |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
|
public function execute() |
54
|
|
|
{ |
55
|
|
|
exec('mkdir -p '.$this->buildPath, $output, $result); |
56
|
|
|
if ($result !== 0) { |
57
|
|
|
throw new Exception('Unable to create build directory.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// let pdepend generate all metrics we'll need |
61
|
|
|
$this->pdepend(); |
62
|
|
|
|
63
|
|
|
// if we expect these json files to be loaded client-side to render |
64
|
|
|
// the charts, might as well assume it'll fit in this machine's |
65
|
|
|
// memory to submit it to our API ;) |
66
|
|
|
$json = file_get_contents($this->buildPath.DIRECTORY_SEPARATOR.$this->json); |
67
|
|
|
|
68
|
|
|
return json_decode($json); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Runs pdepend to generate the metrics. |
73
|
|
|
* |
74
|
|
|
* @throws Exception |
75
|
|
|
*/ |
76
|
|
|
protected function pdepend() |
77
|
|
|
{ |
78
|
|
|
$jsonGenerator = new JsonGenerator(); |
79
|
|
|
$jsonGenerator->setLogFile($this->buildPath.DIRECTORY_SEPARATOR.$this->json); |
80
|
|
|
|
81
|
|
|
$application = new Application(); |
82
|
|
|
$engine = $application->getEngine(); |
83
|
|
|
$engine->addReportGenerator($jsonGenerator); |
84
|
|
|
|
85
|
|
|
$engine->addDirectory($this->config['path']); |
86
|
|
|
|
87
|
|
|
// exclude directories are evaluated relative to where pdepend is being |
88
|
|
|
// run from, not what it is running on |
89
|
|
|
$converter = new PathConverter($this->config['path'], getcwd()); |
90
|
|
|
$exclude = array_map(array($converter, 'convert'), $this->config['exclude_folders']); |
91
|
|
|
$filter = new ExcludePathFilter($exclude); |
92
|
|
|
$engine->addFileFilter($filter); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$engine->analyze(); |
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
throw new Exception('Unable to generate pdepend metrics.'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|