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
|
|
|
* PDepend cache directory name. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $pdepend = '.pdepend'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Config |
35
|
|
|
*/ |
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Config $config |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Config $config) |
42
|
|
|
{ |
43
|
|
|
$this->config = $config; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function execute() |
50
|
|
|
{ |
51
|
|
|
// all paths in build_path are relative to project root, which may not |
52
|
|
|
// be where this code is run from, so prepend the project root! |
53
|
|
|
$buildPath = $this->config['path'].DIRECTORY_SEPARATOR.$this->config['build_path']; |
54
|
|
|
|
55
|
|
|
exec('mkdir -p '.$buildPath.DIRECTORY_SEPARATOR.$this->pdepend, $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
|
|
|
set_error_handler(array($this, 'warningHandler'), E_WARNING); |
62
|
|
|
$path = $buildPath; |
63
|
|
|
$this->pdepend($path); |
64
|
|
|
restore_error_handler(); |
65
|
|
|
|
66
|
|
|
// if we expect these json files to be loaded client-side to render |
67
|
|
|
// the charts, might as well assume it'll fit in this machine's |
68
|
|
|
// memory to submit it to our API ;) |
69
|
|
|
$json = file_get_contents($path.DIRECTORY_SEPARATOR.$this->json); |
70
|
|
|
|
71
|
|
|
return json_decode($json, true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Runs pdepend to generate the metrics. |
76
|
|
|
* |
77
|
|
|
* @param string $path |
78
|
|
|
* |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
|
|
protected function pdepend($path) |
82
|
|
|
{ |
83
|
|
|
$jsonGenerator = new JsonGenerator(); |
84
|
|
|
$jsonGenerator->setLogFile($path.DIRECTORY_SEPARATOR.$this->json); |
85
|
|
|
|
86
|
|
|
$application = new Application(); |
87
|
|
|
|
88
|
|
|
// overwrite default config to ensure that cache files are stored in |
89
|
|
|
// different folders per build |
90
|
|
|
$config = $application->getConfiguration(); |
91
|
|
|
$config->cache->driver = 'file'; |
92
|
|
|
$config->cache->location = $path.DIRECTORY_SEPARATOR.$this->pdepend; |
93
|
|
|
|
94
|
|
|
$engine = $application->getEngine(); |
95
|
|
|
$engine->addReportGenerator($jsonGenerator); |
96
|
|
|
|
97
|
|
|
$engine->addDirectory($this->config['path']); |
98
|
|
|
|
99
|
|
|
// exclude directories are evaluated relative to where pdepend is being |
100
|
|
|
// run from, not what it is running on |
101
|
|
|
$converter = new PathConverter($this->config['path'], getcwd()); |
102
|
|
|
$exclude = array_map(array($converter, 'convert'), $this->config['exclude_folders']); |
103
|
|
|
$filter = new ExcludePathFilter($exclude); |
104
|
|
|
$engine->addFileFilter($filter); |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$engine->analyze(); |
108
|
|
|
} catch (\Exception $e) { |
109
|
|
|
throw new Exception('Unable to generate pdepend metrics.'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $errno |
115
|
|
|
* @param string $errstr |
116
|
|
|
* @param string $errfile |
117
|
|
|
* @param string $errline |
118
|
|
|
* @param array $errcontext |
119
|
|
|
* |
120
|
|
|
* @throws Exception |
121
|
|
|
*/ |
122
|
|
|
protected function warningHandler($errno, $errstr, $errfile, $errline, array $errcontext) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
if ( |
125
|
|
|
strpos($errstr, 'filesize(): stat failed') !== false && |
126
|
|
|
strpos($errfile, 'FileCacheDriver.php') !== false |
127
|
|
|
) { |
128
|
|
|
throw new Exception('Failed to load from cache; risk potentially incomplete metrics.'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.