Completed
Push — master ( 525b08...1c0221 )
by Matthias
02:15
created

Analyzer::warningHandler()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 2
nop 5
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 Config
28
     */
29
    protected $config;
30
31
    /**
32
     * @param Config $config
33
     */
34
    public function __construct(Config $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function execute()
43
    {
44
        // all paths in build_path are relative to project root, which may not
45
        // be where this code is run from, so prepend the project root!
46
        $buildPath = $this->config['path'].DIRECTORY_SEPARATOR.$this->config['build_path'];
47
48
        exec('mkdir -p '.$buildPath, $output, $result);
49
        if ($result !== 0) {
50
            throw new Exception('Unable to create build directory.');
51
        }
52
53
        // let pdepend generate all metrics we'll need
54
        set_error_handler(array($this, 'warningHandler'), E_WARNING);
55
        $path = $buildPath.DIRECTORY_SEPARATOR.$this->json;
56
        $this->pdepend($path);
57
        restore_error_handler();
58
59
        // if we expect these json files to be loaded client-side to render
60
        // the charts, might as well assume it'll fit in this machine's
61
        // memory to submit it to our API ;)
62
        $json = file_get_contents($path);
63
64
        return json_decode($json, true);
65
    }
66
67
    /**
68
     * Runs pdepend to generate the metrics.
69
     *
70
     * @param string $path
71
     *
72
     * @throws Exception
73
     */
74
    protected function pdepend($path)
75
    {
76
        $jsonGenerator = new JsonGenerator();
77
        $jsonGenerator->setLogFile($path);
78
79
        $application = new Application();
80
        $engine = $application->getEngine();
81
        $engine->addReportGenerator($jsonGenerator);
82
83
        $engine->addDirectory($this->config['path']);
84
85
        // exclude directories are evaluated relative to where pdepend is being
86
        // run from, not what it is running on
87
        $converter = new PathConverter($this->config['path'], getcwd());
88
        $exclude = array_map(array($converter, 'convert'), $this->config['exclude_folders']);
89
        $filter = new ExcludePathFilter($exclude);
90
        $engine->addFileFilter($filter);
91
92
        try {
93
            $engine->analyze();
94
        } catch (\Exception $e) {
95
            throw new Exception('Unable to generate pdepend metrics.');
96
        }
97
    }
98
99
    /**
100
     * @param int $errno
101
     * @param string $errstr
102
     * @param string $errfile
103
     * @param string $errline
104
     * @param array $errcontext
105
     * @throws Exception
106
     */
107
    protected function warningHandler($errno, $errstr, $errfile, $errline, array $errcontext) {
0 ignored issues
show
Unused Code introduced by
The parameter $errline is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errcontext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
        if (
109
            strpos($errstr, 'filesize(): stat failed') !== false &&
110
            strpos($errfile, 'FileCacheDriver.php') !== false
111
        ) {
112
            throw new Exception('Failed to load from cache; risk potentially incomplete metrics.');
113
        }
114
115
        return;
116
    }
117
}
118