Arbit - project tracking

PHPMD

Browse source code

File: / source/ PHP/ PMD/ Parser.php

Type
text/plain text/plain
Last Author
mapi
Version
205
Line Rev. Author Source
1 143 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_PMD.
4 mapi *
5 mapi * PHP Version 5
6 mapi *
7 174 mapi * Copyright (c) 2009-2010, Manuel Pichler <mapi@phpmd.org>.
8 143 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 mapi * @category PHP
40 mapi * @package PHP_PMD
41 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
42 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
43 143 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 mapi * @version SVN: $Id$
45 174 mapi * @link http://phpmd.org
46 143 mapi */
47 mapi
48 mapi require_once 'PHP/Depend/Log/LoggerI.php';
49 mapi require_once 'PHP/Depend/Log/CodeAwareI.php';
50 mapi require_once 'PHP/Depend/Visitor/AbstractVisitor.php';
51 mapi
52 200 mapi require_once 'PHP/PMD/Node/Class.php';
53 mapi require_once 'PHP/PMD/Node/Function.php';
54 205 mapi require_once 'PHP/PMD/Node/Interface.php';
55 200 mapi require_once 'PHP/PMD/Node/Method.php';
56 143 mapi
57 mapi /**
58 mapi * Simple wrapper around the php depend engine.
59 mapi *
60 mapi * @category PHP
61 mapi * @package PHP_PMD
62 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
63 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
64 143 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
65 mapi * @version Release: @package_version@
66 174 mapi * @link http://phpmd.org
67 143 mapi */
68 mapi class PHP_PMD_Parser pdepend-warning pdepend-warning pdepend-warning pdepend-warning
69 mapi extends PHP_Depend_Visitor_AbstractVisitor
70 mapi implements PHP_Depend_Log_LoggerI,
71 mapi PHP_Depend_Log_CodeAwareI
72 mapi {
73 mapi /**
74 mapi * The analysing rule-set instance.
75 mapi *
76 mapi * @var array(PHP_PMD_RuleSet) $_ruleSets
77 mapi */
78 mapi private $_ruleSets = array();
79 mapi
80 mapi /**
81 mapi * The metric containing analyzer instances.
82 mapi *
83 mapi * @var array(PHP_Depend_Metrics_AnalyzerI) $_analyzers
84 mapi */
85 mapi private $_analyzers = array();
86 mapi
87 mapi /**
88 mapi * The raw PHP_Depend code nodes.
89 mapi *
90 mapi * @var PHP_Depend_Code_NodeIterator
91 mapi */
92 mapi private $_code = null;
93 mapi
94 mapi /**
95 mapi * The violation report used by this PHP_Depend adapter.
96 mapi *
97 mapi * @var PHP_PMD_Report $_report
98 mapi */
99 mapi private $_report = null;
100 mapi
101 mapi /**
102 mapi * The wrapped PHP_Depend instance.
103 mapi *
104 mapi * @var PHP_Depend
105 mapi */
106 mapi private $_pdepend = null;
107 mapi
108 mapi /**
109 mapi * Constructs a new parser adapter instance.
110 mapi *
111 mapi * @param PHP_Depend $pdepend The context php depend instance.
112 mapi */
113 mapi public function __construct(PHP_Depend $pdepend)
114 mapi {
115 mapi $this->_pdepend = $pdepend;
116 mapi }
117 mapi
118 mapi /**
119 mapi * Parses the projects source and reports all detected errors and violations.
120 mapi *
121 mapi * @param PHP_PMD_Report $report The phpmd error and violation report.
122 mapi *
123 mapi * @return void
124 mapi */
125 mapi public function parse(PHP_PMD_Report $report)
126 mapi {
127 mapi $this->setReport($report);
128 mapi
129 mapi $this->_pdepend->addLogger($this);
130 mapi $this->_pdepend->analyze();
131 mapi }
132 mapi
133 mapi /**
134 mapi * Adds a new analysis rule-set to this adapter.
135 mapi *
136 mapi * @param PHP_PMD_RuleSet $ruleSet The new rule-set instance.
137 mapi *
138 mapi * @return void
139 mapi */
140 mapi public function addRuleSet(PHP_PMD_RuleSet $ruleSet)
141 mapi {
142 mapi $this->_ruleSets[] = $ruleSet;
143 mapi }
144 mapi
145 mapi /**
146 mapi * Sets the violation report used by the rule-set.
147 mapi *
148 mapi * @param PHP_PMD_Report $report The violation report to use.
149 mapi *
150 mapi * @return void
151 mapi */
152 mapi public function setReport(PHP_PMD_Report $report)
153 mapi {
154 mapi $this->_report = $report;
155 mapi }
156 mapi
157 mapi /**
158 mapi * Adds an analyzer to log. If this logger accepts the given analyzer it
159 mapi * with return <b>true</b>, otherwise the return value is <b>false</b>.
160 mapi *
161 mapi * @param PHP_Depend_Metrics_AnalyzerI $analyzer The analyzer to log.
162 mapi *
163 mapi * @return boolean
164 mapi */
165 mapi public function log(PHP_Depend_Metrics_AnalyzerI $analyzer)
166 mapi {
167 mapi $this->_analyzers[] = $analyzer;
168 mapi }
169 mapi
170 mapi /**
171 mapi * Closes the logger process and writes the output file.
172 mapi *
173 mapi * @return void
174 mapi * @throws PHP_Depend_Log_NoLogOutputException If the no log target exists.
175 mapi */
176 mapi public function close()
177 mapi {
178 mapi foreach ($this->_code as $node) {
179 mapi $node->accept($this);
180 mapi }
181 mapi }
182 mapi
183 mapi /**
184 mapi * Returns an <b>array</b> with accepted analyzer types. These types can be
185 mapi * concrete analyzer classes or one of the descriptive analyzer interfaces.
186 mapi *
187 mapi * @return array(string)
188 mapi */
189 mapi public function getAcceptedAnalyzers()
190 mapi {
191 mapi return array('PHP_Depend_Metrics_NodeAwareI');
192 mapi }
193 mapi
194 mapi /**
195 mapi * Visits a class node.
196 mapi *
197 mapi * @param PHP_Depend_Code_Class $node The current class node.
198 mapi *
199 mapi * @return void
200 mapi * @see PHP_Depend_VisitorI::visitClass()
201 mapi */
202 mapi public function visitClass(PHP_Depend_Code_Class $node)
203 mapi {
204 205 mapi if (!$node->isUserDefined()) {
205 143 mapi return;
206 mapi }
207 mapi
208 200 mapi $this->_apply(new PHP_PMD_Node_Class($node));
209 143 mapi parent::visitClass($node);
210 mapi }
211 mapi
212 mapi /**
213 mapi * Visits a function node.
214 mapi *
215 mapi * @param PHP_Depend_Code_Function $node The current function node.
216 mapi *
217 mapi * @return void
218 mapi * @see PHP_Depend_VisitorI::visitFunction()
219 mapi */
220 mapi public function visitFunction(PHP_Depend_Code_Function $node)
221 mapi {
222 mapi if ($node->getSourceFile()->getFileName() === null) {
223 mapi return;
224 mapi }
225 mapi
226 200 mapi $this->_apply(new PHP_PMD_Node_Function($node));
227 143 mapi }
228 mapi
229 mapi /**
230 205 mapi * Visits an interface node.
231 mapi *
232 mapi * @param PHP_Depend_Code_Interface $node The current interface node.
233 mapi *
234 mapi * @return void
235 mapi * @see PHP_Depend_VisitorI::visitInterface()
236 mapi */
237 mapi public function visitInterface(PHP_Depend_Code_Interface $node)
238 mapi {
239 mapi if (!$node->isUserDefined()) {
240 mapi return;
241 mapi }
242 mapi
243 mapi $this->_apply(new PHP_PMD_Node_Interface($node));
244 mapi parent::visitInterface($node);
245 mapi }
246 mapi
247 mapi /**
248 143 mapi * Visits a method node.
249 mapi *
250 196 mapi * @param PHP_Depend_Code_Method $node The method class node.
251 143 mapi *
252 mapi * @return void
253 mapi * @see PHP_Depend_VisitorI::visitMethod()
254 mapi */
255 mapi public function visitMethod(PHP_Depend_Code_Method $node)
256 mapi {
257 mapi if ($node->getSourceFile()->getFileName() === null) {
258 mapi return;
259 mapi }
260 mapi
261 200 mapi $this->_apply(new PHP_PMD_Node_Method($node));
262 143 mapi }
263 mapi
264 mapi /**
265 mapi * Sets the context code nodes.
266 mapi *
267 mapi * @param PHP_Depend_Code_NodeIterator $code The code nodes.
268 mapi *
269 mapi * @return void
270 mapi */
271 mapi public function setCode(PHP_Depend_Code_NodeIterator $code)
272 mapi {
273 mapi $this->_code = $code;
274 mapi }
275 mapi
276 mapi /**
277 mapi * Applies all rule-sets to the given <b>$node</b> instance.
278 mapi *
279 mapi * @param PHP_PMD_AbstractNode $node The context source node.
280 mapi *
281 mapi * @return void
282 mapi */
283 mapi private function _apply(PHP_PMD_AbstractNode $node)
284 mapi {
285 mapi $this->_collectMetrics($node);
286 mapi foreach ($this->_ruleSets as $ruleSet) {
287 mapi $ruleSet->setReport($this->_report);
288 mapi $ruleSet->apply($node);
289 mapi }
290 mapi }
291 mapi
292 mapi /**
293 mapi * Collects the collected metrics for the given node and adds them to the
294 mapi * <b>$node</b>.
295 mapi *
296 mapi * @param PHP_PMD_AbstractNode $node The context source node.
297 mapi *
298 mapi * @return void
299 mapi */
300 mapi private function _collectMetrics(PHP_PMD_AbstractNode $node)
301 mapi {
302 mapi $metrics = array();
303 mapi
304 mapi $pdepend = $node->getNode();
305 mapi foreach ($this->_analyzers as $analyzer) {
306 mapi $metrics = array_merge($metrics, $analyzer->getNodeMetrics($pdepend));
307 mapi }
308 mapi $node->setMetrics($metrics);
309 mapi }
310 194 mapi }