Arbit - project tracking

PHPMD

Browse source code

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

Type
text/plain text/plain
Last Author
mapi
Version
200
Line Rev. Author Source
1 22 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 22 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 22 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 22 mapi */
47 mapi
48 128 mapi require_once 'PHP/PMD/Node/ASTNode.php';
49 200 mapi require_once 'PHP/PMD/Node/Annotations.php';
50 128 mapi
51 22 mapi /**
52 mapi * This is an abstract base class for PHP_PMD code nodes, it is just a wrapper
53 mapi * around PHP_Depend's object model.
54 mapi *
55 mapi * @category PHP
56 mapi * @package PHP_PMD
57 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
58 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
59 22 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
60 mapi * @version Release: @package_version@
61 174 mapi * @link http://phpmd.org
62 22 mapi */
63 mapi abstract class PHP_PMD_AbstractNode pdepend-warning pdepend-warning pdepend-warning pdepend-warning
64 mapi {
65 mapi /**
66 mapi *
67 128 mapi * @var PHP_Depend_Code_AbstractItem|PHP_Depend_Code_ASTNode $node
68 22 mapi */
69 mapi private $_node = null;
70 mapi
71 mapi /**
72 mapi * The collected metrics for this node.
73 mapi *
74 mapi * @var array(string=>mixed) $_metrics
75 mapi */
76 mapi private $_metrics = null;
77 mapi
78 36 mapi /**
79 mapi * Constructs a new PHP_PMD node.
80 mapi *
81 128 mapi * @param PHP_Depend_Code_AbstractItem|PHP_Depend_Code_ASTNode $node The wrapped
82 mapi * PHP_Depend ast node instance or item object.
83 36 mapi */
84 128 mapi public function __construct($node)
85 22 mapi {
86 mapi $this->_node = $node;
87 mapi }
88 mapi
89 36 mapi /**
90 129 mapi * The magic call method is used to pipe requests from rules direct
91 mapi * to the underlying PHP_Depend ast node.
92 mapi *
93 mapi * @param string $name Name of the invoked method.
94 mapi * @param array $args Optional method arguments.
95 mapi *
96 mapi * @return mixed
97 mapi * @throws BadMethodCallException When the underlying PHP_Depend node
98 mapi * does not contain a method named <b>$name</b>.
99 mapi */
100 mapi public function __call($name, array $args)
101 mapi {
102 mapi if (method_exists($this->getNode(), $name)) {
103 mapi return call_user_func_array(array($this->getNode(), $name), $args);
104 mapi }
105 mapi throw new BadMethodCallException(
106 mapi sprintf('Invalid method %s() called.', $name)
107 mapi );
108 mapi }
109 mapi
110 mapi /**
111 128 mapi * Returns the parent of this node or <b>null</b> when no parent node
112 mapi * exists.
113 mapi *
114 mapi * @return PHP_PMD_AbstractNode
115 mapi */
116 mapi public function getParent()
117 mapi {
118 mapi if (($node = $this->_node->getParent()) === null) {
119 mapi return null;
120 mapi }
121 mapi return new PHP_PMD_Node_ASTNode($node, $this->getFileName());
122 mapi }
123 mapi
124 mapi /**
125 129 mapi * Returns a child node at the given index.
126 mapi *
127 mapi * @param integer $index The child offset.
128 mapi *
129 mapi * @return PHP_PMD_Node_ASTNode
130 mapi */
131 mapi public function getChild($index)
132 mapi {
133 mapi return new PHP_PMD_Node_ASTNode(
134 mapi $this->_node->getChild($index),
135 mapi $this->getFileName()
136 mapi );
137 mapi }
138 mapi
139 mapi /**
140 128 mapi * Returns the first child of the given type or <b>null</b> when this node
141 mapi * has no child of the given type.
142 mapi *
143 mapi * @param string $type The searched child type.
144 mapi *
145 mapi * @return PHP_PMD_AbstractNode
146 mapi */
147 mapi public function getFirstChildOfType($type)
148 mapi {
149 mapi $node = $this->_node->getFirstChildOfType('PHP_Depend_Code_AST' . $type);
150 mapi if ($node === null) {
151 mapi return null;
152 mapi }
153 mapi return new PHP_PMD_Node_ASTNode($node, $this->getFileName());
154 mapi }
155 mapi
156 mapi /**
157 mapi * Searches recursive for all children of this node that are of the given
158 mapi * type.
159 mapi *
160 mapi * @param string $type The searched child type.
161 mapi *
162 mapi * @return array(PHP_PMD_AbstractNode)
163 mapi */
164 mapi public function findChildrenOfType($type)
165 mapi {
166 mapi $children = $this->_node->findChildrenOfType('PHP_Depend_Code_AST' . $type);
167 mapi
168 mapi $nodes = array();
169 mapi foreach ($children as $child) {
170 mapi $nodes[] = new PHP_PMD_Node_ASTNode($child, $this->getFileName());
171 mapi }
172 mapi return $nodes;
173 mapi }
174 mapi
175 mapi /**
176 mapi * Tests if this node represents the the given type.
177 mapi *
178 mapi * @param string $type The expected node type.
179 mapi *
180 mapi * @return boolean
181 mapi */
182 mapi public function isInstanceOf($type)
183 mapi {
184 mapi $class = 'PHP_Depend_Code_AST' . $type;
185 mapi return ($this->_node instanceof $class);
186 mapi }
187 mapi
188 mapi /**
189 mapi * Returns the image of the underlying node.
190 mapi *
191 mapi * @return string
192 mapi */
193 mapi public function getImage()
194 mapi {
195 129 mapi return $this->_node->getName();
196 128 mapi }
197 mapi
198 mapi /**
199 36 mapi * Returns the source name for this node, maybe a class or interface name,
200 mapi * or a package, method, function name.
201 mapi *
202 mapi * @return string
203 mapi */
204 22 mapi public function getName()
205 mapi {
206 mapi return $this->_node->getName();
207 mapi }
208 mapi
209 36 mapi /**
210 mapi * Returns the begin line for this node in the php source code file.
211 mapi *
212 mapi * @return integer
213 mapi */
214 22 mapi public function getBeginLine()
215 mapi {
216 mapi return $this->_node->getStartLine();
217 mapi }
218 mapi
219 36 mapi /**
220 mapi * Returns the end line for this node in the php source code file.
221 mapi *
222 mapi * @return integer
223 mapi */
224 22 mapi public function getEndLine()
225 mapi {
226 mapi return $this->_node->getEndLine();
227 mapi }
228 mapi
229 mapi /**
230 mapi * Returns the name of the declaring source file.
231 mapi *
232 mapi * @return string
233 mapi */
234 mapi public function getFileName()
235 mapi {
236 mapi return (string) $this->_node->getSourceFile();
237 mapi }
238 mapi
239 36 mapi /**
240 22 mapi * Returns the wrapped PHP_Depend node instance.
241 mapi *
242 mapi * @return PHP_Depend_Code_AbstractItem
243 mapi */
244 mapi public function getNode()
245 mapi {
246 mapi return $this->_node;
247 mapi }
248 mapi
249 mapi /**
250 mapi * Returns a textual representation/name for the concrete node type.
251 mapi *
252 mapi * @return string
253 mapi */
254 mapi public function getType()
255 mapi {
256 mapi $type = explode('_', get_class($this));
257 mapi return strtolower(array_pop($type));
258 mapi }
259 mapi
260 mapi /**
261 mapi * This method will return the metric value for the given identifier or
262 mapi * <b>null</b> when no such metric exists.
263 mapi *
264 mapi * @param string $name The metric name or abbreviation.
265 mapi *
266 mapi * @return mixed
267 mapi */
268 mapi public function getMetric($name)
269 mapi {
270 mapi if (isset($this->_metrics[$name])) {
271 mapi return $this->_metrics[$name];
272 mapi }
273 mapi return null;
274 mapi }
275 mapi
276 mapi /**
277 mapi * This method will set the metrics for this node.
278 mapi *
279 mapi * @param array(string=>mixed) $metrics The collected node metrics.
280 mapi *
281 mapi * @return void
282 mapi */
283 mapi public function setMetrics(array $metrics)
284 mapi {
285 mapi if ($this->_metrics === null) {
286 mapi $this->_metrics = $metrics;
287 mapi }
288 mapi }
289 mapi
290 mapi /**
291 194 mapi * Checks if this node has a suppressed annotation for the given rule
292 mapi * instance.
293 mapi *
294 mapi * @param PHP_PMD_AbstractRule $rule The context rule instance.
295 mapi *
296 mapi * @return boolean
297 mapi */
298 mapi public abstract function hasSuppressWarningsAnnotationFor(
299 mapi PHP_PMD_AbstractRule $rule
300 mapi );
301 mapi
302 mapi /**
303 22 mapi * Returns the name of the parent type or <b>null</b> when this node has no
304 mapi * parent type.
305 mapi *
306 mapi * @return string
307 mapi */
308 mapi public abstract function getParentName();
309 36 mapi
310 mapi /**
311 mapi * Returns the name of the parent package.
312 mapi *
313 mapi * @return string
314 mapi */
315 22 mapi public abstract function getPackageName();
316 73 mapi }