Parser::getAcceptedAnalyzers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD;
19
20
use PDepend\Engine;
21
use PDepend\Report\CodeAwareGenerator;
22
use PDepend\Source\ASTVisitor\AbstractASTVisitor;
23
use PDepend\Metrics\Analyzer;
24
use PDepend\Source\AST\ASTClass;
25
use PDepend\Source\AST\ASTMethod;
26
use PDepend\Source\AST\ASTInterface;
27
use PDepend\Source\AST\ASTFunction;
28
use PDepend\Source\AST\ASTArtifactList;
29
use PHPMD\Node\ClassNode;
30
use PHPMD\Node\FunctionNode;
31
use PHPMD\Node\InterfaceNode;
32
use PHPMD\Node\MethodNode;
33
34
/**
35
 * Simple wrapper around the php depend engine.
36
 */
37
class Parser extends AbstractASTVisitor implements CodeAwareGenerator
38
{
39
    /**
40
     * The analysing rule-set instance.
41
     *
42
     * @var \PHPMD\RuleSet[]
43
     */
44
    private $ruleSets = array();
45
46
    /**
47
     * The metric containing analyzer instances.
48
     *
49
     * @var \PDepend\Metrics\AnalyzerNodeAware[]
50
     */
51
    private $analyzers = array();
52
53
    /**
54
     * The raw PDepend code nodes.
55
     *
56
     * @var \PDepend\Source\AST\ASTArtifactList
57
     */
58
    private $artifacts = null;
59
60
    /**
61
     * The violation report used by this PDepend adapter.
62
     *
63
     * @var \PHPMD\Report
64
     */
65
    private $report = null;
66
67
    /**
68
     * The wrapped PDepend Engine instance.
69
     *
70
     * @var \PDepend\Engine
71
     */
72
    private $pdepend = null;
73
74
    /**
75
     * Constructs a new parser adapter instance.
76
     *
77
     * @param \PDepend\Engine $pdepend The context php depend instance.
78
     */
79 13
    public function __construct(Engine $pdepend)
80
    {
81 13
        $this->pdepend = $pdepend;
82 13
    }
83
84
    /**
85
     * Parses the projects source and reports all detected errors and violations.
86
     *
87
     * @param \PHPMD\Report $report
88
     * @return void
89
     */
90 7
    public function parse(Report $report)
91
    {
92 7
        $this->setReport($report);
93
94 7
        $this->pdepend->addReportGenerator($this);
95 7
        $this->pdepend->analyze();
96
97 7
        foreach ($this->pdepend->getExceptions() as $exception) {
98 1
            $report->addError(new ProcessingError($exception->getMessage()));
99
        }
100 7
    }
101
102
    /**
103
     * Adds a new analysis rule-set to this adapter.
104
     *
105
     * @param \PHPMD\RuleSet $ruleSet
106
     * @return void
107
     */
108 12
    public function addRuleSet(RuleSet $ruleSet)
109
    {
110 12
        $this->ruleSets[] = $ruleSet;
111 12
    }
112
113
    /**
114
     * Sets the violation report used by the rule-set.
115
     *
116
     * @param \PHPMD\Report $report
117
     * @return void
118
     */
119 13
    public function setReport(Report $report)
120
    {
121 13
        $this->report = $report;
122 13
    }
123
124
    /**
125
     * Adds an analyzer to log. If this logger accepts the given analyzer it
126
     * with return <b>true</b>, otherwise the return value is <b>false</b>.
127
     *
128
     * @param \PDepend\Metrics\Analyzer $analyzer The analyzer to log.
129
     * @return void
130
     */
131 6
    public function log(Analyzer $analyzer)
132
    {
133 6
        $this->analyzers[] = $analyzer;
134 6
    }
135
136
    /**
137
     * Closes the logger process and writes the output file.
138
     *
139
     * @return void
140
     * @throws \PDepend\Report\NoLogOutputException If the no log target exists.
141
     */
142 6
    public function close()
143
    {
144
        // Set max nesting level, because we may get really deep data structures
145 6
        ini_set('xdebug.max_nesting_level', 8192);
146
147 6
        foreach ($this->artifacts as $node) {
148 6
            $node->accept($this);
149
        }
150 6
    }
151
152
    /**
153
     * Returns an <b>array</b> with accepted analyzer types. These types can be
154
     * concrete analyzer classes or one of the descriptive analyzer interfaces.
155
     *
156
     * @return string[]
157
     */
158 6
    public function getAcceptedAnalyzers()
159
    {
160
        return array(
161 6
            'pdepend.analyzer.cyclomatic_complexity',
162
            'pdepend.analyzer.node_loc',
163
            'pdepend.analyzer.npath_complexity',
164
            'pdepend.analyzer.inheritance',
165
            'pdepend.analyzer.node_count',
166
            'pdepend.analyzer.hierarchy',
167
            'pdepend.analyzer.crap_index',
168
            'pdepend.analyzer.code_rank',
169
            'pdepend.analyzer.coupling',
170
            'pdepend.analyzer.class_level',
171
            'pdepend.analyzer.cohesion',
172
        );
173
    }
174
175
    /**
176
     * Visits a class node.
177
     *
178
     * @param \PDepend\Source\AST\ASTClass $node
179
     * @return void
180
     */
181 7
    public function visitClass(ASTClass $node)
182
    {
183 7
        if (!$node->isUserDefined()) {
184 1
            return;
185
        }
186
187 6
        $this->apply(new ClassNode($node));
188 6
        parent::visitClass($node);
189 6
    }
190
191
    /**
192
     * Visits a function node.
193
     *
194
     * @param \PDepend\Source\AST\ASTFunction $node
195
     * @return void
196
     */
197 3
    public function visitFunction(ASTFunction $node)
198
    {
199 3
        if ($node->getCompilationUnit()->getFileName() === null) {
200 1
            return;
201
        }
202
203 2
        $this->apply(new FunctionNode($node));
204 2
    }
205
206
    /**
207
     * Visits an interface node.
208
     *
209
     * @param \PDepend\Source\AST\ASTInterface $node
210
     * @return void
211
     */
212
    public function visitInterface(ASTInterface $node)
213
    {
214
        if (!$node->isUserDefined()) {
215
            return;
216
        }
217
218
        $this->apply(new InterfaceNode($node));
219
        parent::visitInterface($node);
220
    }
221
222
    /**
223
     * Visits a method node.
224
     *
225
     * @param \PDepend\Source\AST\ASTMethod $node
226
     * @return void
227
     */
228 7
    public function visitMethod(ASTMethod $node)
229
    {
230 7
        if ($node->getCompilationUnit()->getFileName() === null) {
231 1
            return;
232
        }
233
234 6
        $this->apply(new MethodNode($node));
235 6
    }
236
237
    /**
238
     * Sets the context code nodes.
239
     *
240
     * @param \PDepend\Source\AST\ASTArtifactList $artifacts
241
     * @return void
242
     */
243 6
    public function setArtifacts(ASTArtifactList $artifacts)
244
    {
245 6
        $this->artifacts = $artifacts;
246 6
    }
247
248
    /**
249
     * Applies all rule-sets to the given <b>$node</b> instance.
250
     *
251
     * @param \PHPMD\AbstractNode $node
252
     * @return void
253
     */
254 9
    private function apply(AbstractNode $node)
255
    {
256 9
        $this->collectMetrics($node);
257 9
        foreach ($this->ruleSets as $ruleSet) {
258 9
            $ruleSet->setReport($this->report);
259 9
            $ruleSet->apply($node);
260
        }
261 9
    }
262
263
    /**
264
     * Collects the collected metrics for the given node and adds them to the
265
     * <b>$node</b>.
266
     *
267
     * @param \PHPMD\AbstractNode $node
268
     * @return void
269
     */
270 9
    private function collectMetrics(AbstractNode $node)
271
    {
272 9
        $metrics = array();
273
274 9
        $pdepend = $node->getNode();
275 9
        foreach ($this->analyzers as $analyzer) {
276 6
            $metrics = array_merge($metrics, $analyzer->getNodeMetrics($pdepend));
277
        }
278 9
        $node->setMetrics($metrics);
279 9
    }
280
}
281