| 1 |
116
|
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 |
116
|
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 |
↓
|
mapi |
* @subpackage Rule |
|
| 42 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 43 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 44 |
116
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 45 |
↓
|
mapi |
* @version SVN: $Id$ |
|
| 46 |
174
|
mapi |
* @link http://phpmd.org |
|
| 47 |
116
|
mapi |
*/ |
|
| 48 |
↓
|
mapi |
|
|
| 49 |
245
|
mapi |
require_once 'PHP/PMD/Rule/AbstractLocalVariable.php'; |
|
| 50 |
116
|
mapi |
require_once 'PHP/PMD/Rule/IFunctionAware.php'; |
|
| 51 |
↓
|
mapi |
require_once 'PHP/PMD/Rule/IMethodAware.php'; |
|
| 52 |
↓
|
mapi |
|
|
| 53 |
↓
|
mapi |
/** |
|
| 54 |
↓
|
mapi |
* This rule collects all local variables within a given function or method |
|
| 55 |
↓
|
mapi |
* that are not used by any code in the analyzed source artifact. |
|
| 56 |
↓
|
mapi |
* |
|
| 57 |
↓
|
mapi |
* @category PHP |
|
| 58 |
↓
|
mapi |
* @package PHP_PMD |
|
| 59 |
↓
|
mapi |
* @subpackage Rule |
|
| 60 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 61 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 62 |
116
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 63 |
↓
|
mapi |
* @version Release: @package_version@ |
|
| 64 |
174
|
mapi |
* @link http://phpmd.org |
|
| 65 |
116
|
mapi |
*/ |
|
| 66 |
↓
|
mapi |
class PHP_PMD_Rule_UnusedLocalVariable |
|
| 67 |
245
|
mapi |
extends PHP_PMD_Rule_AbstractLocalVariable |
|
| 68 |
116
|
mapi |
implements PHP_PMD_Rule_IFunctionAware, |
|
| 69 |
↓
|
mapi |
PHP_PMD_Rule_IMethodAware |
|
| 70 |
↓
|
mapi |
{ |
|
| 71 |
↓
|
mapi |
/** |
|
| 72 |
↓
|
mapi |
* Found variable images within a single method or function. |
|
| 73 |
↓
|
mapi |
* |
|
| 74 |
↓
|
mapi |
* @var array(string) |
|
| 75 |
↓
|
mapi |
*/ |
|
| 76 |
↓
|
mapi |
private $_images = array(); |
|
| 77 |
↓
|
mapi |
|
|
| 78 |
↓
|
mapi |
/** |
|
| 79 |
117
|
mapi |
* This method checks that all local variables within the given function or |
|
| 80 |
↓
|
mapi |
* method are used at least one time. |
|
| 81 |
116
|
mapi |
* |
|
| 82 |
↓
|
mapi |
* @param PHP_PMD_AbstractNode $node The context source code node. |
|
| 83 |
↓
|
mapi |
* |
|
| 84 |
↓
|
mapi |
* @return void |
|
| 85 |
↓
|
mapi |
*/ |
|
| 86 |
↓
|
mapi |
public function apply(PHP_PMD_AbstractNode $node) |
|
| 87 |
↓
|
mapi |
{ |
|
| 88 |
↓
|
mapi |
$this->_images = array(); |
|
| 89 |
↓
|
mapi |
|
|
| 90 |
↓
|
mapi |
$this->_collectVariables($node); |
|
| 91 |
128
|
mapi |
$this->_removeParameters($node); |
|
| 92 |
235
|
mapi |
|
|
| 93 |
128
|
mapi |
foreach ($this->_images as $image => $nodes) { |
|
| 94 |
↓
|
mapi |
if (count($nodes) === 1) { |
|
| 95 |
↓
|
mapi |
$this->addViolation(reset($nodes), array($image)); |
|
| 96 |
116
|
mapi |
} |
|
| 97 |
↓
|
mapi |
} |
|
| 98 |
↓
|
mapi |
} |
|
| 99 |
↓
|
mapi |
|
|
| 100 |
↓
|
mapi |
/** |
|
| 101 |
128
|
mapi |
* This method removes all variables from the <b>$_images</b> property that |
|
| 102 |
↓
|
mapi |
* are also found in the formal parameters of the given method or/and |
|
| 103 |
↓
|
mapi |
* function node. |
|
| 104 |
116
|
mapi |
* |
|
| 105 |
200
|
mapi |
* @param PHP_PMD_Node_AbstractCallable $node The currently |
|
| 106 |
116
|
mapi |
* analyzed method/function node. |
|
| 107 |
↓
|
mapi |
* |
|
| 108 |
↓
|
mapi |
* @return void |
|
| 109 |
↓
|
mapi |
*/ |
|
| 110 |
200
|
mapi |
private function _removeParameters(PHP_PMD_Node_AbstractCallable $node) |
|
| 111 |
116
|
mapi |
{ |
|
| 112 |
119
|
mapi |
// Get formal parameter container |
|
| 113 |
128
|
mapi |
$parameters = $node->getFirstChildOfType('FormalParameters'); |
|
| 114 |
119
|
mapi |
|
|
| 115 |
128
|
mapi |
// Now get all declarators in the formal parameters container |
|
| 116 |
↓
|
mapi |
$declarators = $parameters->findChildrenOfType('VariableDeclarator'); |
|
| 117 |
235
|
mapi |
|
|
| 118 |
119
|
mapi |
foreach ($declarators as $declarator) { |
|
| 119 |
128
|
mapi |
unset($this->_images[$declarator->getImage()]); |
|
| 120 |
116
|
mapi |
} |
|
| 121 |
↓
|
mapi |
} |
|
| 122 |
↓
|
mapi |
|
|
| 123 |
↓
|
mapi |
/** |
|
| 124 |
↓
|
mapi |
* This method collects all local variable instances from the given |
|
| 125 |
↓
|
mapi |
* method/function node and stores their image in the <b>$_images</b> |
|
| 126 |
↓
|
mapi |
* property. |
|
| 127 |
↓
|
mapi |
* |
|
| 128 |
200
|
mapi |
* @param PHP_PMD_Node_AbstractCallable $node The currently |
|
| 129 |
116
|
mapi |
* analyzed method/function node. |
|
| 130 |
↓
|
mapi |
* |
|
| 131 |
↓
|
mapi |
* @return void |
|
| 132 |
↓
|
mapi |
*/ |
|
| 133 |
200
|
mapi |
private function _collectVariables(PHP_PMD_Node_AbstractCallable $node) |
|
| 134 |
116
|
mapi |
{ |
|
| 135 |
128
|
mapi |
foreach ($node->findChildrenOfType('Variable') as $variable) { |
|
| 136 |
245
|
mapi |
if ($this->isLocal($variable)) { |
|
| 137 |
128
|
mapi |
$this->_collectVariable($variable); |
|
| 138 |
116
|
mapi |
} |
|
| 139 |
↓
|
mapi |
} |
|
| 140 |
219
|
mapi |
foreach ($node->findChildrenOfType('VariableDeclarator') as $variable) { |
|
| 141 |
↓
|
mapi |
$this->_collectVariable($variable); |
|
| 142 |
↓
|
mapi |
} |
|
| 143 |
116
|
mapi |
} |
|
| 144 |
↓
|
mapi |
|
|
| 145 |
↓
|
mapi |
/** |
|
| 146 |
128
|
mapi |
* Stores the given variable node in an internal list of found variables. |
|
| 147 |
↓
|
mapi |
* |
|
| 148 |
↓
|
mapi |
* @param PHP_PMD_Node_ASTNode $node The context variable node. |
|
| 149 |
↓
|
mapi |
* |
|
| 150 |
↓
|
mapi |
* @return void |
|
| 151 |
↓
|
mapi |
*/ |
|
| 152 |
↓
|
mapi |
private function _collectVariable(PHP_PMD_Node_ASTNode $node) |
|
| 153 |
↓
|
mapi |
{ |
|
| 154 |
↓
|
mapi |
if (!isset($this->_images[$node->getImage()])) { |
|
| 155 |
↓
|
mapi |
$this->_images[$node->getImage()] = array(); |
|
| 156 |
↓
|
mapi |
} |
|
| 157 |
↓
|
mapi |
$this->_images[$node->getImage()][] = $node; |
|
| 158 |
↓
|
mapi |
} |
|
| 159 |
116
|
mapi |
} |
|