AbstractTool   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

10 Methods

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