PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/php-phpmd-PHP-PMD-1.3.3/PHP_PMD-1.3.3/PHP/PMD/Rule/Naming/LongVariable.php

#
PHP | 223 lines | 84 code | 14 blank | 125 comment | 8 complexity | e6ef94ea2203c9c653dc29c37fc6e953 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of PHP_PMD.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2012, Manuel Pichler <mapi@phpmd.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category PHP
  40. * @package PHP_PMD
  41. * @subpackage Rule_Naming
  42. * @author Manuel Pichler <mapi@phpmd.org>
  43. * @copyright 2008-2012 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://phpmd.org
  47. */
  48. require_once 'PHP/PMD/AbstractRule.php';
  49. require_once 'PHP/PMD/Rule/IClassAware.php';
  50. require_once 'PHP/PMD/Rule/IMethodAware.php';
  51. require_once 'PHP/PMD/Rule/IFunctionAware.php';
  52. /**
  53. * This rule class will detect variables, parameters and properties with really
  54. * long names.
  55. *
  56. * @category PHP
  57. * @package PHP_PMD
  58. * @subpackage Rule_Naming
  59. * @author Manuel Pichler <mapi@phpmd.org>
  60. * @copyright 2008-2012 Manuel Pichler. All rights reserved.
  61. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  62. * @version Release: @package_version@
  63. * @link http://phpmd.org
  64. */
  65. class PHP_PMD_Rule_Naming_LongVariable
  66. extends PHP_PMD_AbstractRule
  67. implements PHP_PMD_Rule_IClassAware,
  68. PHP_PMD_Rule_IMethodAware,
  69. PHP_PMD_Rule_IFunctionAware
  70. {
  71. /**
  72. * Temporary map holding variables that were already processed in the
  73. * current context.
  74. *
  75. * @var array(string=>boolean)
  76. */
  77. private $_processedVariables = array();
  78. /**
  79. * Extracts all variable and variable declarator nodes from the given node
  80. * and checks the variable name length against the configured maximum
  81. * length.
  82. *
  83. * @param PHP_PMD_AbstractNode $node The context source code node.
  84. *
  85. * @return void
  86. */
  87. public function apply(PHP_PMD_AbstractNode $node)
  88. {
  89. $this->resetProcessed();
  90. if ($node->getType() === 'class') {
  91. $fields = $node->findChildrenOfType('FieldDeclaration');
  92. foreach ($fields as $field) {
  93. if ($field->isPrivate()) {
  94. continue;
  95. }
  96. $declarators = $field->findChildrenOfType('VariableDeclarator');
  97. foreach ($declarators as $declarator) {
  98. $this->checkNodeImage($declarator);
  99. }
  100. }
  101. } else {
  102. $declarators = $node->findChildrenOfType('VariableDeclarator');
  103. foreach ($declarators as $declarator) {
  104. $this->checkNodeImage($declarator);
  105. }
  106. $variables = $node->findChildrenOfType('Variable');
  107. foreach ($variables as $variable) {
  108. $this->checkNodeImage($variable);
  109. }
  110. }
  111. $this->resetProcessed();
  112. }
  113. /**
  114. * Checks if the variable name of the given node is smaller/equal to the
  115. * configured threshold.
  116. *
  117. * @param PHP_PMD_AbstractNode $node The context source code node.
  118. *
  119. * @return void
  120. */
  121. protected function checkNodeImage(PHP_PMD_AbstractNode $node)
  122. {
  123. if ($this->isNotProcessed($node)) {
  124. $this->addProcessed($node);
  125. $this->doCheckNodeImage($node);
  126. }
  127. }
  128. /**
  129. * Template method that performs the real node image check.
  130. *
  131. * @param PHP_PMD_AbstractNode $node The context source code node.
  132. *
  133. * @return void
  134. */
  135. protected function doCheckNodeImage(PHP_PMD_AbstractNode $node)
  136. {
  137. $threshold = $this->getIntProperty('maximum');
  138. if ($threshold >= strlen($node->getImage()) - 1) {
  139. return;
  140. }
  141. if ($this->_isNameAllowedInContext($node)) {
  142. return;
  143. }
  144. $this->addViolation($node, array($node->getImage(), $threshold));
  145. }
  146. /**
  147. * Checks if a short name is acceptable in the current context. For the
  148. * moment the only context is a static member.
  149. *
  150. * @param PHP_PMD_AbstractNode $node The context source code node.
  151. *
  152. * @return boolean
  153. */
  154. private function _isNameAllowedInContext(PHP_PMD_AbstractNode $node)
  155. {
  156. return $this->_isChildOf($node, 'MemberPrimaryPrefix');
  157. }
  158. /**
  159. * Checks if the given node is a direct or indirect child of a node with
  160. * the given type.
  161. *
  162. * @param PHP_PMD_AbstractNode $node The context source code node.
  163. * @param string $type Possible parent type.
  164. *
  165. * @return boolean
  166. */
  167. private function _isChildOf(PHP_PMD_AbstractNode $node, $type)
  168. {
  169. $parent = $node->getParent();
  170. while (is_object($parent)) {
  171. if ($parent->isInstanceOf($type)) {
  172. return true;
  173. }
  174. $parent = $parent->getParent();
  175. }
  176. return false;
  177. }
  178. /**
  179. * Resets the already processed nodes.
  180. *
  181. * @return void
  182. */
  183. protected function resetProcessed()
  184. {
  185. $this->_processedVariables = array();
  186. }
  187. /**
  188. * Flags the given node as already processed.
  189. *
  190. * @param PHP_PMD_AbstractNode $node The node to add.
  191. *
  192. * @return void
  193. */
  194. protected function addProcessed(PHP_PMD_AbstractNode $node)
  195. {
  196. $this->_processedVariables[$node->getImage()] = true;
  197. }
  198. /**
  199. * Checks if the given node was already processed.
  200. *
  201. * @param PHP_PMD_AbstractNode $node The node to check.
  202. *
  203. * @return boolean
  204. */
  205. protected function isNotProcessed(PHP_PMD_AbstractNode $node)
  206. {
  207. return !isset($this->_processedVariables[$node->getImage()]);
  208. }
  209. }