|
1
|
|
|
<?php |
|
2
|
|
|
namespace phphound\integration; |
|
3
|
|
|
|
|
4
|
|
|
use phphound\AnalysisResult; |
|
5
|
|
|
use Sabre\Xml\Reader; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Base class for integrations of PHPHound with third party analysis tools. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class AbstractIntegration |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Directory path to where the bin scripts are located. |
|
14
|
|
|
* @var string directory path. |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $binariesPath; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Temporary file wherein the output will be written |
|
20
|
|
|
* @var string temporary file path. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $temporaryFilePath; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Paths to be ignored during runtime. |
|
26
|
|
|
* @var array target file or directory paths to be ignored. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $ignoredPaths; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Analysis result for this integration. |
|
32
|
|
|
* @var AnalysisResult result object. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $result; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Stores binaries path. |
|
38
|
|
|
* @param string $binariesPath where the bin scripts are located. |
|
39
|
|
|
* @param string $temporaryDirPath where temporary files will be created. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($binariesPath, $temporaryDirPath) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->binariesPath = $binariesPath; |
|
44
|
|
|
$this->temporaryFilePath = tempnam($temporaryDirPath, 'PHP-Hound'); |
|
45
|
|
|
$this->ignoredPaths = []; |
|
46
|
|
|
$this->result = new AnalysisResult; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Ignore informed targets during execution. |
|
51
|
|
|
* @param array $targets target file or directory paths to be ignored. |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setIgnoredPaths(array $targets) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->ignoredPaths = $targets; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Analysis results for this integration. |
|
61
|
|
|
* @return AnalysisResult analysis result object. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getAnalysisResult() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Creates and execute tool command, returning output results. |
|
70
|
|
|
* @param string[] $targetPaths file/directory paths to be analysed. |
|
71
|
|
|
* @return string CLI JSON output. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function run($targetPaths) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->executeCommand($targetPaths); |
|
76
|
|
|
$this->processResults(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Prepare and execute command. |
|
81
|
|
|
* @param string[] $targetPaths file/directory paths to be analysed. |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function executeCommand($targetPaths) |
|
85
|
|
|
{ |
|
86
|
|
|
exec($this->getCommand($targetPaths)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Convert tool output into PHP Hound array output. |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function processResults() |
|
94
|
|
|
{ |
|
95
|
|
|
$content = $this->getOutputContent(); |
|
96
|
|
|
if (empty($content)) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
$xml = new Reader; |
|
100
|
|
|
$xml->xml($content); |
|
101
|
|
|
$this->addIssuesFromXml($xml); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Tool raw output. |
|
106
|
|
|
* @return string raw output contents. |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getOutputContent() |
|
109
|
|
|
{ |
|
110
|
|
|
return file_get_contents($this->temporaryFilePath); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Integration description. |
|
115
|
|
|
* @return string description. |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract public function getDescription(); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Create integration command to be run on the shell. |
|
121
|
|
|
* @param string[] $targetPaths file/directory paths to be analysed. |
|
122
|
|
|
* @return string shell command. |
|
123
|
|
|
*/ |
|
124
|
|
|
abstract public function getCommand($targetPaths); |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Read issues from the XML output. |
|
128
|
|
|
* @param Reader $xml XML reader object. |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
|
|
abstract protected function addIssuesFromXml(Reader $xml); |
|
132
|
|
|
} |
|
133
|
|
|
|