Passed
Push — master ( d62b89...abb8c8 )
by Michiel
06:34
created

PhpDependTask::setConfigFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
use PDepend\Application;
21
use PDepend\Util\Log;
22
use Phing\Exception\BuildException;
23
use Phing\Io\File;
24
use Phing\Task;
25
use Phing\Type\Element\FileSetAware;
26
use Phing\Util\StringHelper;
27
28
/**
29
 * Runs the PHP_Depend software analyzer and metric tool.
30
 * Performs static code analysis on a given source base.
31
 *
32
 * @package phing.tasks.ext.pdepend
33
 * @author  Benjamin Schultz <[email protected]>
34
 * @since   2.4.1
35
 */
36
class PhpDependTask extends Task
37
{
38
    use FileSetAware;
39
40
    /**
41
     * A php source code filename or directory
42
     *
43
     * @var File
44
     */
45
    protected $file = null;
46
47
    /**
48
     * List of allowed file extensions. Default file extensions are <b>php</b>
49
     * and <p>php5</b>.
50
     *
51
     * @var array<string>
52
     */
53
    protected $allowedFileExtensions = ['php', 'php5'];
54
55
    /**
56
     * List of exclude directories. Default exclude dirs are <b>.git</b>,
57
     * <b>.svn</b> and <b>CVS</b>.
58
     *
59
     * @var array<string>
60
     */
61
    protected $excludeDirectories = ['.git', '.svn', 'CVS'];
62
63
    /**
64
     * List of exclude packages
65
     *
66
     * @var array<string>
67
     */
68
    protected $excludePackages = [];
69
70
    /**
71
     * Should the parse ignore doc comment annotations?
72
     *
73
     * @var boolean
74
     */
75
    protected $withoutAnnotations = false;
76
77
    /**
78
     * Should PHP_Depend treat <b>+global</b> as a regular project package?
79
     *
80
     * @var boolean
81
     */
82
    protected $supportBadDocumentation = false;
83
84
    /**
85
     * Flag for enable/disable debugging
86
     *
87
     * @var boolean
88
     */
89
    protected $debug = false;
90
91
    /**
92
     * PHP_Depend configuration file
93
     *
94
     * @var File
95
     */
96
    protected $configFile = null;
97
98
    /**
99
     * Logger elements
100
     *
101
     * @var PhpDependLoggerElement[]
102
     */
103
    protected $loggers = [];
104
105
    /**
106
     * Analyzer elements
107
     *
108
     * @var PhpDependAnalyzerElement[]
109
     */
110
    protected $analyzers = [];
111
112
    /**
113
     * Flag that determines whether to halt on error
114
     *
115
     * @var boolean
116
     */
117
    protected $haltonerror = false;
118
119
    /**
120
     * @var bool
121
     */
122
    private $oldVersion = false;
123
124
    /**
125
     * @var string
126
     */
127
    protected $pharLocation = "";
128
129
    /**
130
     * Load the necessary environment for running PHP_Depend
131
     *
132
     * @throws BuildException
133
     */
134 2
    protected function requireDependencies()
135
    {
136 2
        if (!empty($this->pharLocation)) {
137
            include_once 'phar://' . $this->pharLocation . '/vendor/autoload.php';
138
        }
139
140
        // check 2.x version (composer/phar)
141 2
        if (class_exists('PDepend\\TextUI\\Runner')) {
142 2
            return;
143
        }
144
145
        throw new BuildException("This task requires PDepend 2.x", $this->getLocation());
146
    }
147
148
    /**
149
     * Set the input source file or directory
150
     *
151
     * @param File $file The input source file or directory
152
     */
153
    public function setFile(File $file)
154
    {
155
        $this->file = $file;
156
    }
157
158
    /**
159
     * Sets a list of filename extensions for valid php source code files
160
     *
161
     * @param string $fileExtensions List of valid file extensions
162
     */
163
    public function setAllowedFileExtensions($fileExtensions)
164
    {
165
        $this->allowedFileExtensions = [];
166
167
        $token = ' ,;';
168
        $ext = strtok($fileExtensions, $token);
169
170
        while ($ext !== false) {
171
            $this->allowedFileExtensions[] = $ext;
172
            $ext = strtok($token);
173
        }
174
    }
175
176
    /**
177
     * Sets a list of exclude directories
178
     *
179
     * @param string $excludeDirectories List of exclude directories
180
     */
181
    public function setExcludeDirectories($excludeDirectories)
182
    {
183
        $this->excludeDirectories = [];
184
185
        $token = ' ,;';
186
        $pattern = strtok($excludeDirectories, $token);
187
188
        while ($pattern !== false) {
189
            $this->excludeDirectories[] = $pattern;
190
            $pattern = strtok($token);
191
        }
192
    }
193
194
    /**
195
     * Sets a list of exclude packages
196
     *
197
     * @param string $excludePackages Exclude packages
198
     */
199
    public function setExcludePackages($excludePackages)
200
    {
201
        $this->excludePackages = [];
202
203
        $token = ' ,;';
204
        $pattern = strtok($excludePackages, $token);
205
206
        while ($pattern !== false) {
207
            $this->excludePackages[] = $pattern;
208
            $pattern = strtok($token);
209
        }
210
    }
211
212
    /**
213
     * Should the parser ignore doc comment annotations?
214
     *
215
     * @param boolean $withoutAnnotations
216
     */
217
    public function setWithoutAnnotations($withoutAnnotations)
218
    {
219
        $this->withoutAnnotations = StringHelper::booleanValue($withoutAnnotations);
220
    }
221
222
    /**
223
     * Should PHP_Depend support projects with a bad documentation. If this
224
     * option is set to <b>true</b>, PHP_Depend will treat the default package
225
     * <b>+global</b> as a regular project package.
226
     *
227
     * @param boolean $supportBadDocumentation
228
     */
229
    public function setSupportBadDocumentation($supportBadDocumentation)
230
    {
231
        $this->supportBadDocumentation = StringHelper::booleanValue($supportBadDocumentation);
232
    }
233
234
    /**
235
     * Set debugging On/Off
236
     *
237
     * @param boolean $debug
238
     */
239
    public function setDebug($debug)
240
    {
241
        $this->debug = StringHelper::booleanValue($debug);
242
    }
243
244
    /**
245
     * Set halt on error
246
     *
247
     * @param boolean $haltonerror
248
     */
249
    public function setHaltonerror($haltonerror)
250
    {
251
        $this->haltonerror = StringHelper::booleanValue($haltonerror);
252
    }
253
254
    /**
255
     * Set the configuration file
256
     *
257
     * @param File $configFile The configuration file
258
     */
259
    public function setConfigFile(File $configFile)
260
    {
261
        $this->configFile = $configFile;
262
    }
263
264
    /**
265
     * Create object for nested logger element
266
     *
267
     * @return PhpDependLoggerElement
268
     */
269 2
    public function createLogger()
270
    {
271 2
        $num = array_push($this->loggers, new PhpDependLoggerElement());
272
273 2
        return $this->loggers[$num - 1];
274
    }
275
276
    /**
277
     * Create object for nested analyzer element
278
     *
279
     * @return PhpDependAnalyzerElement
280
     */
281 1
    public function createAnalyzer()
282
    {
283 1
        $num = array_push($this->analyzers, new PhpDependAnalyzerElement());
284
285 1
        return $this->analyzers[$num - 1];
286
    }
287
288
    /**
289
     * @param string $pharLocation
290
     */
291
    public function setPharLocation($pharLocation)
292
    {
293
        $this->pharLocation = $pharLocation;
294
    }
295
296
    /**
297
     * Executes PHP_Depend_TextUI_Runner against PhingFile or a FileSet
298
     *
299
     * @throws BuildException
300
     */
301 2
    public function main()
302
    {
303 2
        $this->requireDependencies();
304
305 2
        if (!isset($this->file) and count($this->filesets) == 0) {
306
            throw new BuildException('Missing either a nested fileset or attribute "file" set');
307
        }
308
309 2
        if (count($this->loggers) == 0) {
310
            throw new BuildException('Missing nested "logger" element');
311
        }
312
313 2
        $this->validateLoggers();
314 2
        $this->validateAnalyzers();
315
316 2
        $filesToParse = $this->getFilesToParse();
317
318 2
        $runner = $this->createRunner();
319 2
        $runner->setSourceArguments($filesToParse);
320
321 2
        foreach ($this->loggers as $logger) {
322
            // Register logger
323 2
            if ($this->oldVersion) {
324
                $runner->addLogger(
325
                    $logger->getType(),
326
                    $logger->getOutfile()->__toString()
327
                );
328
            } else {
329 2
                $runner->addReportGenerator(
330 2
                    $logger->getType(),
331 2
                    $logger->getOutfile()->__toString()
332
                );
333
            }
334
        }
335
336 2
        foreach ($this->analyzers as $analyzer) {
337
            // Register additional analyzer
338 1
            $runner->addOption(
339 1
                $analyzer->getType(),
340 1
                $analyzer->getValue()
341
            );
342
        }
343
344
        // Disable annotation parsing
345 2
        if ($this->withoutAnnotations) {
346
            $runner->setWithoutAnnotations();
347
        }
348
349
        // Enable bad documentation support
350 2
        if ($this->supportBadDocumentation) {
351
            $runner->setSupportBadDocumentation();
352
        }
353
354
        // Check for suffix
355 2
        if (count($this->allowedFileExtensions) > 0) {
356 2
            $runner->setFileExtensions($this->allowedFileExtensions);
357
        }
358
359
        // Check for ignore directories
360 2
        if (count($this->excludeDirectories) > 0) {
361 2
            $runner->setExcludeDirectories($this->excludeDirectories);
362
        }
363
364
        // Check for exclude packages
365 2
        if (count($this->excludePackages) > 0) {
366
            $runner->setExcludePackages($this->excludePackages);
367
        }
368
369 2
        $runner->run();
370
371 2
        if ($runner->hasParseErrors() === true) {
372
            $this->log('Following errors occurred:');
373
374
            foreach ($runner->getParseErrors() as $error) {
375
                $this->log($error);
376
            }
377
378
            if ($this->haltonerror === true) {
379
                throw new BuildException('Errors occurred during parse process');
380
            }
381
        }
382 2
    }
383
384
    /**
385
     * Validates the available loggers
386
     *
387
     * @throws BuildException
388
     */
389 2
    protected function validateLoggers()
390
    {
391 2
        foreach ($this->loggers as $logger) {
392 2
            if ($logger->getType() === '') {
393
                throw new BuildException('Logger missing required "type" attribute');
394
            }
395
396 2
            if ($logger->getOutfile() === null) {
397
                throw new BuildException('Logger requires "outfile" attribute');
398
            }
399
        }
400 2
    }
401
402
    /**
403
     * Validates the available analyzers
404
     *
405
     * @throws BuildException
406
     */
407 2
    protected function validateAnalyzers()
408
    {
409 2
        foreach ($this->analyzers as $analyzer) {
410 1
            if ($analyzer->getType() === '') {
411
                throw new BuildException('Analyzer missing required "type" attribute');
412
            }
413
414 1
            if (count($analyzer->getValue()) === 0) {
0 ignored issues
show
Bug introduced by
$analyzer->getValue() of type string is incompatible with the type Countable|array expected by parameter $value of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

414
            if (count(/** @scrutinizer ignore-type */ $analyzer->getValue()) === 0) {
Loading history...
415
                throw new BuildException('Analyzer missing required "value" attribute');
416
            }
417
        }
418 2
    }
419
420
    /**
421
     * @return array
422
     */
423 2
    private function getFilesToParse()
424
    {
425 2
        $filesToParse = [];
426
427 2
        if ($this->file instanceof File) {
0 ignored issues
show
introduced by
$this->file is always a sub-type of Phing\Io\File.
Loading history...
428
            $filesToParse[] = $this->file->__toString();
429
            return $filesToParse;
430
        }
431
432
        // append any files in filesets
433 2
        foreach ($this->filesets as $fs) {
434 2
            $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
435
436 2
            foreach ($files as $filename) {
437 2
                $f = new File($fs->getDir($this->project), $filename);
438 2
                $filesToParse[] = $f->getAbsolutePath();
439
            }
440
        }
441 2
        return $filesToParse;
442
    }
443
444
    /**
445
     * @return object
446
     */
447 2
    private function createRunner()
448
    {
449 2
        $application = new Application();
450
451 2
        if (!empty($this->configFile)) {
452
            if (file_exists($this->configFile->__toString()) === false) {
453
                throw new BuildException(
454
                    'The configuration file "' . $this->configFile->__toString() . '" doesn\'t exist.'
455
                );
456
            }
457
458
            $application->setConfigurationFile($this->configFile);
459
        }
460
461 2
        $runner = $application->getRunner();
462
463 2
        if ($this->debug) {
464
            // Enable debug logging
465
            Log::setSeverity(1);
466
        }
467
468 2
        return $runner;
469
    }
470
}
471