1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Runs phploc a tool for quickly measuring the size of PHP projects.
|
22
|
|
*
|
23
|
|
* @package phing.tasks.ext.phploc
|
24
|
|
* @author Raphael Stolt <raphael.stolt@gmail.com>
|
25
|
|
*/
|
26
|
|
class PHPLocTask extends Task
|
27
|
|
{
|
28
|
|
use FileSetAware;
|
29
|
|
|
30
|
|
/**
|
31
|
|
* @var array
|
32
|
|
*/
|
33
|
|
protected $suffixesToCheck = ['php'];
|
34
|
|
|
35
|
|
/**
|
36
|
|
* @var array
|
37
|
|
*/
|
38
|
|
protected $acceptedReportTypes = ['cli', 'txt', 'xml', 'csv'];
|
39
|
|
|
40
|
|
/**
|
41
|
|
* @var null
|
42
|
|
*/
|
43
|
|
protected $reportDirectory = null;
|
44
|
|
|
45
|
|
/**
|
46
|
|
* @var string
|
47
|
|
*/
|
48
|
|
protected $reportType = 'cli';
|
49
|
|
|
50
|
|
/**
|
51
|
|
* @var string
|
52
|
|
*/
|
53
|
|
protected $reportFileName = 'phploc-report';
|
54
|
|
|
55
|
|
/**
|
56
|
|
* @var bool
|
57
|
|
*/
|
58
|
|
protected $countTests = false;
|
59
|
|
|
60
|
|
/**
|
61
|
|
* @var null|PhingFile
|
62
|
|
*/
|
63
|
|
protected $fileToCheck = null;
|
64
|
|
|
65
|
|
/**
|
66
|
|
* @var array
|
67
|
|
*/
|
68
|
|
protected $filesToCheck = [];
|
69
|
|
|
70
|
|
/**
|
71
|
|
* @var PHPLocFormatterElement[]
|
72
|
|
*/
|
73
|
|
protected $formatterElements = [];
|
74
|
|
|
75
|
|
/**
|
76
|
|
* @var string
|
77
|
|
*/
|
78
|
|
private $pharLocation = "";
|
79
|
|
|
80
|
|
/**
|
81
|
|
* @param string $suffixListOrSingleSuffix
|
82
|
|
*/
|
83
|
0
|
public function setSuffixes($suffixListOrSingleSuffix)
|
84
|
|
{
|
85
|
0
|
if (strpos($suffixListOrSingleSuffix, ',')) {
|
86
|
0
|
$suffixes = explode(',', $suffixListOrSingleSuffix);
|
87
|
0
|
$this->suffixesToCheck = array_map('trim', $suffixes);
|
88
|
|
} else {
|
89
|
0
|
$this->suffixesToCheck[] = trim($suffixListOrSingleSuffix);
|
90
|
|
}
|
91
|
|
}
|
92
|
|
|
93
|
|
/**
|
94
|
|
* @param PhingFile $file
|
95
|
|
*/
|
96
|
0
|
public function setFile(PhingFile $file)
|
97
|
|
{
|
98
|
0
|
$this->fileToCheck = trim($file);
|
99
|
|
}
|
100
|
|
|
101
|
|
/**
|
102
|
|
* @param boolean $countTests
|
103
|
|
*/
|
104
|
0
|
public function setCountTests($countTests)
|
105
|
|
{
|
106
|
0
|
$this->countTests = StringHelper::booleanValue($countTests);
|
107
|
|
}
|
108
|
|
|
109
|
|
/**
|
110
|
|
* @param string $type
|
111
|
|
*/
|
112
|
0
|
public function setReportType($type)
|
113
|
|
{
|
114
|
0
|
$this->reportType = trim($type);
|
115
|
|
}
|
116
|
|
|
117
|
|
/**
|
118
|
|
* @param string $name
|
119
|
|
*/
|
120
|
0
|
public function setReportName($name)
|
121
|
|
{
|
122
|
0
|
$this->reportFileName = trim($name);
|
123
|
|
}
|
124
|
|
|
125
|
|
/**
|
126
|
|
* @param string $directory
|
127
|
|
*/
|
128
|
0
|
public function setReportDirectory($directory)
|
129
|
|
{
|
130
|
0
|
$this->reportDirectory = trim($directory);
|
131
|
|
}
|
132
|
|
|
133
|
|
/**
|
134
|
|
* @param string $pharLocation
|
135
|
|
*/
|
136
|
0
|
public function setPharLocation($pharLocation)
|
137
|
|
{
|
138
|
0
|
$this->pharLocation = $pharLocation;
|
139
|
|
}
|
140
|
|
|
141
|
|
/**
|
142
|
|
* @param PHPLocFormatterElement $formatterElement
|
143
|
|
*/
|
144
|
0
|
public function addFormatter(PHPLocFormatterElement $formatterElement)
|
145
|
|
{
|
146
|
0
|
$this->formatterElements[] = $formatterElement;
|
147
|
|
}
|
148
|
|
|
149
|
|
/**
|
150
|
|
* @throws BuildException
|
151
|
|
*/
|
152
|
0
|
protected function loadDependencies()
|
153
|
|
{
|
154
|
0
|
if (!empty($this->pharLocation)) {
|
155
|
|
// hack to prevent PHPLOC from starting in CLI mode and halting Phing
|
156
|
|
eval(
|
157
|
0
|
"namespace SebastianBergmann\PHPLOC\CLI;
|
158
|
|
class Application
|
159
|
|
{
|
160
|
|
public function run() {}
|
161
|
|
}"
|
162
|
|
);
|
163
|
|
|
164
|
0
|
ob_start();
|
165
|
0
|
include $this->pharLocation;
|
166
|
0
|
ob_end_clean();
|
167
|
|
}
|
168
|
|
|
169
|
0
|
if (!class_exists('\SebastianBergmann\PHPLOC\Analyser')) {
|
170
|
0
|
if (!@include_once 'SebastianBergmann/PHPLOC/autoload.php') {
|
171
|
0
|
throw new BuildException(
|
172
|
0
|
'PHPLocTask depends on PHPLoc being installed and on include_path.',
|
173
|
0
|
$this->getLocation()
|
174
|
|
);
|
175
|
|
}
|
176
|
|
}
|
177
|
|
}
|
178
|
|
|
179
|
0
|
public function main()
|
180
|
|
{
|
181
|
0
|
$this->loadDependencies();
|
182
|
|
|
183
|
0
|
$this->validateProperties();
|
184
|
|
|
185
|
0
|
if (count($this->filesets) > 0) {
|
186
|
0
|
foreach ($this->filesets as $fileSet) {
|
187
|
0
|
$directoryScanner = $fileSet->getDirectoryScanner($this->project);
|
188
|
0
|
$files = $directoryScanner->getIncludedFiles();
|
189
|
0
|
$directory = $fileSet->getDir($this->project)->getPath();
|
190
|
|
|
191
|
0
|
foreach ($files as $file) {
|
192
|
0
|
if ($this->isFileSuffixSet($file)) {
|
193
|
0
|
$this->filesToCheck[] = $directory . DIRECTORY_SEPARATOR . $file;
|
194
|
|
}
|
195
|
|
}
|
196
|
|
}
|
197
|
|
|
198
|
0
|
$this->filesToCheck = array_unique($this->filesToCheck);
|
199
|
|
}
|
200
|
|
|
201
|
0
|
$this->runPhpLocCheck();
|
202
|
|
}
|
203
|
|
|
204
|
|
/**
|
205
|
|
* @throws BuildException
|
206
|
|
*/
|
207
|
0
|
private function validateProperties()
|
208
|
|
{
|
209
|
0
|
if ($this->fileToCheck === null && count($this->filesets) === 0) {
|
210
|
0
|
throw new BuildException('Missing either a nested fileset or the attribute "file" set.');
|
211
|
|
}
|
212
|
|
|
213
|
0
|
if ($this->fileToCheck !== null) {
|
214
|
0
|
if (!file_exists($this->fileToCheck)) {
|
215
|
0
|
throw new BuildException("File to check doesn't exist.");
|
216
|
|
}
|
217
|
|
|
218
|
0
|
if (!$this->isFileSuffixSet($this->fileToCheck)) {
|
219
|
0
|
throw new BuildException('Suffix of file to check is not defined in "suffixes" attribute.');
|
220
|
|
}
|
221
|
|
|
222
|
0
|
if (count($this->filesets) > 0) {
|
223
|
0
|
throw new BuildException('Either use a nested fileset or "file" attribute; not both.');
|
224
|
|
}
|
225
|
|
}
|
226
|
|
|
227
|
0
|
if (count($this->suffixesToCheck) === 0) {
|
228
|
0
|
throw new BuildException('No file suffix defined.');
|
229
|
|
}
|
230
|
|
|
231
|
0
|
if (count($this->formatterElements) == 0) {
|
232
|
0
|
if ($this->reportType === null) {
|
233
|
0
|
throw new BuildException('No report type or formatters defined.');
|
234
|
|
}
|
235
|
|
|
236
|
0
|
if ($this->reportType !== null && !in_array($this->reportType, $this->acceptedReportTypes)) {
|
237
|
0
|
throw new BuildException('Unaccepted report type defined.');
|
238
|
|
}
|
239
|
|
|
240
|
0
|
if ($this->reportType !== 'cli' && $this->reportDirectory === null) {
|
241
|
0
|
throw new BuildException('No report output directory defined.');
|
242
|
|
}
|
243
|
|
|
244
|
0
|
if ($this->reportDirectory !== null && !is_dir($this->reportDirectory)) {
|
245
|
0
|
$reportOutputDir = new PhingFile($this->reportDirectory);
|
246
|
|
|
247
|
|
$logMessage = "Report output directory doesn't exist, creating: "
|
248
|
0
|
. $reportOutputDir->getAbsolutePath() . '.';
|
249
|
|
|
250
|
0
|
$this->log($logMessage);
|
251
|
0
|
$reportOutputDir->mkdirs();
|
252
|
|
}
|
253
|
|
|
254
|
0
|
if ($this->reportType !== 'cli') {
|
255
|
0
|
$this->reportFileName .= '.' . $this->reportType;
|
256
|
|
}
|
257
|
|
|
258
|
0
|
$formatterElement = new PHPLocFormatterElement();
|
259
|
0
|
$formatterElement->setType($this->reportType);
|
260
|
0
|
$formatterElement->setUseFile($this->reportDirectory !== null);
|
261
|
0
|
$formatterElement->setToDir($this->reportDirectory);
|
262
|
0
|
$formatterElement->setOutfile($this->reportFileName);
|
263
|
0
|
$this->formatterElements[] = $formatterElement;
|
264
|
|
}
|
265
|
|
}
|
266
|
|
|
267
|
|
/**
|
268
|
|
* @param string $filename
|
269
|
|
*
|
270
|
|
* @return boolean
|
271
|
|
*/
|
272
|
0
|
protected function isFileSuffixSet($filename)
|
273
|
|
{
|
274
|
0
|
return in_array(pathinfo($filename, PATHINFO_EXTENSION), $this->suffixesToCheck);
|
275
|
|
}
|
276
|
|
|
277
|
0
|
protected function runPhpLocCheck()
|
278
|
|
{
|
279
|
0
|
$files = $this->getFilesToCheck();
|
280
|
0
|
$count = $this->getCountForFiles($files);
|
281
|
|
|
282
|
0
|
foreach ($this->formatterElements as $formatterElement) {
|
283
|
0
|
$formatter = PHPLocFormatterFactory::createFormatter($formatterElement);
|
284
|
|
|
285
|
0
|
if ($formatterElement->getType() != 'cli') {
|
286
|
|
$logMessage = 'Writing report to: '
|
287
|
0
|
. $formatterElement->getToDir() . DIRECTORY_SEPARATOR . $formatterElement->getOutfile();
|
288
|
|
|
289
|
0
|
$this->log($logMessage);
|
290
|
|
}
|
291
|
|
|
292
|
0
|
$formatter->printResult($count, $this->countTests);
|
293
|
|
}
|
294
|
|
}
|
295
|
|
|
296
|
|
/**
|
297
|
|
* @return SplFileInfo[]
|
298
|
|
*/
|
299
|
0
|
protected function getFilesToCheck()
|
300
|
|
{
|
301
|
0
|
$files = [];
|
302
|
|
|
303
|
0
|
if (count($this->filesToCheck) > 0) {
|
304
|
0
|
foreach ($this->filesToCheck as $file) {
|
305
|
0
|
$files[] = new SplFileInfo($file);
|
306
|
|
}
|
307
|
0
|
} elseif ($this->fileToCheck !== null) {
|
308
|
0
|
$files = [new SplFileInfo($this->fileToCheck)];
|
309
|
|
}
|
310
|
|
|
311
|
0
|
return $files;
|
312
|
|
}
|
313
|
|
|
314
|
|
/**
|
315
|
|
* @param SplFileInfo[] $files
|
316
|
|
*
|
317
|
|
* @return array
|
318
|
|
*/
|
319
|
0
|
protected function getCountForFiles(array $files)
|
320
|
|
{
|
321
|
0
|
$analyserClass = '\\SebastianBergmann\\PHPLOC\\Analyser';
|
322
|
0
|
$analyser = new $analyserClass();
|
323
|
|
|
324
|
0
|
return $analyser->countFiles($files, $this->countTests);
|
325
|
|
}
|
326
|
|
}
|