Completed
Push — master ( 064bf6...127bad )
by Matthias
02:16
created

Analyzer::pdepend()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 15
nc 2
nop 0
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->setConfig($config);
42
    }
43
44
    /**
45
     * @param Config $config
46
     */
47
    public function setConfig(Config $config)
48
    {
49
        $this->config = $config;
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
        $this->buildPath = $this->config['path'].DIRECTORY_SEPARATOR.$this->config['build_path'];
54
    }
55
56
    /**
57
     * @return array
58
     *
59
     * @throws Exception
60
     */
61
    public function execute()
62
    {
63
        exec('mkdir -p '.$this->buildPath, $output, $result);
64
        if ($result !== 0) {
65
            throw new Exception('Unable to create build directory.');
66
        }
67
68
        // let pdepend generate all metrics we'll need
69
        $this->pdepend();
70
71
        // if we expect these json files to be loaded client-side to render
72
        // the charts, might as well assume it'll fit in this machine's
73
        // memory to submit it to our API ;)
74
        $json = file_get_contents($this->buildPath.DIRECTORY_SEPARATOR.$this->json);
75
76
        return json_decode($json);
77
    }
78
79
    /**
80
     * Runs pdepend to generate the metrics.
81
     *
82
     * @throws Exception
83
     */
84
    protected function pdepend()
85
    {
86
        $jsonGenerator = new JsonGenerator();
87
        $jsonGenerator->setLogFile($this->buildPath.DIRECTORY_SEPARATOR.$this->json);
88
89
        $application = new Application();
90
        $engine = $application->getEngine();
91
        $engine->addReportGenerator($jsonGenerator);
92
93
        $engine->addDirectory($this->config['path']);
94
95
        // exclude directories are evaluated relative to where pdepend is being
96
        // run from, not what it is running on
97
        $converter = new PathConverter($this->config['path'], getcwd());
98
        $exclude = array_map(array($converter, 'convert'), $this->config['exclude_folders']);
99
        $filter = new ExcludePathFilter($exclude);
100
        $engine->addFileFilter($filter);
101
102
        try {
103
            $engine->analyze();
104
        } catch (\Exception $e) {
105
            throw new Exception('Unable to generate pdepend metrics.');
106
        }
107
    }
108
}
109