PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP/CodeSniffer/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 259 lines | 151 code | 33 blank | 75 comment | 28 complexity | c4b6ec7b3c476782309ae4ba7e18c0c7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @author Marc McIntyre <mmcintyre@squiz.net>
  11. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: ValidVariableNameSniff.php 261899 2008-07-02 05:08:16Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false)
  17. {
  18. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
  19. }
  20. /**
  21. * Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
  22. *
  23. * Checks the naming of variables and member variables.
  24. *
  25. * @category PHP
  26. * @package PHP_CodeSniffer
  27. * @author Greg Sherwood <gsherwood@squiz.net>
  28. * @author Marc McIntyre <mmcintyre@squiz.net>
  29. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  30. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  31. * @version Release: 1.2.2
  32. * @link http://pear.php.net/package/PHP_CodeSniffer
  33. */
  34. class Zend_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
  35. {
  36. /**
  37. * Tokens to ignore so that we can find a DOUBLE_COLON.
  38. *
  39. * @var array
  40. */
  41. private $_ignore = array(T_WHITESPACE, T_COMMENT);
  42. /**
  43. * Processes this test, when one of its tokens is encountered.
  44. *
  45. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  46. * @param int $stackPtr The position of the current token in the
  47. * stack passed in $tokens.
  48. *
  49. * @return void
  50. */
  51. protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  52. {
  53. $tokens = $phpcsFile->getTokens();
  54. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  55. $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES',
  56. 'GLOBALS');
  57. // If it's a php reserved var, then its ok.
  58. if (in_array($varName, $phpReservedVars) === true)
  59. {
  60. return;
  61. }
  62. $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
  63. if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR)
  64. {
  65. // Check to see if we are using a variable from an object.
  66. $var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
  67. if ($tokens[$var]['code'] === T_STRING)
  68. {
  69. // Either a var name or a function call, so check for bracket.
  70. $bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
  71. if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS)
  72. {
  73. $objVarName = $tokens[$var]['content'];
  74. // There is no way for us to know if the var is public or private,
  75. // so we have to ignore a leading underscore if there is one and just
  76. // check the main part of the variable name.
  77. $originalVarName = $objVarName;
  78. if (substr($objVarName, 0, 1) === '_')
  79. {
  80. $objVarName = substr($objVarName, 1);
  81. }
  82. if (PHP_CodeSniffer :: isCamelCaps($objVarName, false, true, false) === false)
  83. {
  84. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  85. $phpcsFile->addError($error, $var);
  86. }
  87. else
  88. if (preg_match('|\d|', $objVarName))
  89. {
  90. $warning = "Variable \"$originalVarName\" contains numbers but this is discouraged";
  91. $phpcsFile->addWarning($warning, $stackPtr);
  92. }
  93. } //end if
  94. } //end if
  95. } //end if
  96. // There is no way for us to know if the var is public or private,
  97. // so we have to ignore a leading underscore if there is one and just
  98. // check the main part of the variable name.
  99. $originalVarName = $varName;
  100. if (substr($varName, 0, 1) === '_')
  101. {
  102. $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
  103. if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON)
  104. {
  105. // The variable lives within a class, and is referenced like
  106. // this: MyClass::$_variable, so we don't know its scope.
  107. $inClass = true;
  108. }
  109. else
  110. {
  111. $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
  112. }
  113. if ($inClass === true)
  114. {
  115. $varName = substr($varName, 1);
  116. }
  117. }
  118. if (PHP_CodeSniffer :: isCamelCaps($varName, false, true, false) === false)
  119. {
  120. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  121. $phpcsFile->addError($error, $stackPtr);
  122. }
  123. else
  124. if (preg_match('|\d|', $varName))
  125. {
  126. $warning = "Variable \"$originalVarName\" contains numbers but this is discouraged";
  127. $phpcsFile->addWarning($warning, $stackPtr);
  128. }
  129. } //end processVariable()
  130. /**
  131. * Processes class member variables.
  132. *
  133. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  134. * @param int $stackPtr The position of the current token in the
  135. * stack passed in $tokens.
  136. *
  137. * @return void
  138. */
  139. protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  140. {
  141. $tokens = $phpcsFile->getTokens();
  142. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  143. $memberProps = $phpcsFile->getMemberProperties($stackPtr);
  144. $public = ($memberProps['scope'] === 'public');
  145. if ($public === true)
  146. {
  147. if (substr($varName, 0, 1) === '_')
  148. {
  149. $error = "Public member variable \"$varName\" must not contain a leading underscore";
  150. $phpcsFile->addError($error, $stackPtr);
  151. return;
  152. }
  153. }
  154. else
  155. {
  156. if (substr($varName, 0, 1) !== '_')
  157. {
  158. $scope = ucfirst($memberProps['scope']);
  159. $error = "$scope member variable \"$varName\" must contain a leading underscore";
  160. $phpcsFile->addError($error, $stackPtr);
  161. return;
  162. }
  163. }
  164. if (PHP_CodeSniffer :: isCamelCaps($varName, false, $public, false) === false)
  165. {
  166. $error = "Variable \"$varName\" is not in valid camel caps format";
  167. $phpcsFile->addError($error, $stackPtr);
  168. }
  169. else
  170. if (preg_match('|\d|', $varName))
  171. {
  172. $warning = "Variable \"$varName\" contains numbers but this is discouraged";
  173. $phpcsFile->addWarning($warning, $stackPtr);
  174. }
  175. } //end processMemberVar()
  176. /**
  177. * Processes the variable found within a double quoted string.
  178. *
  179. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  180. * @param int $stackPtr The position of the double quoted
  181. * string.
  182. *
  183. * @return void
  184. */
  185. protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  186. {
  187. $tokens = $phpcsFile->getTokens();
  188. $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES',
  189. 'GLOBALS');
  190. if (preg_match_all('|[^\\\]\$([a-zA-Z0-9_]+)|', $tokens[$stackPtr]['content'], $matches) !== 0)
  191. {
  192. foreach ($matches[1] as $varName)
  193. {
  194. // If it's a php reserved var, then its ok.
  195. if (in_array($varName, $phpReservedVars) === true)
  196. {
  197. continue;
  198. }
  199. // There is no way for us to know if the var is public or private,
  200. // so we have to ignore a leading underscore if there is one and just
  201. // check the main part of the variable name.
  202. $originalVarName = $varName;
  203. if (substr($varName, 0, 1) === '_')
  204. {
  205. if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true)
  206. {
  207. $varName = substr($varName, 1);
  208. }
  209. }
  210. if (PHP_CodeSniffer :: isCamelCaps($varName, false, true, false) === false)
  211. {
  212. $varName = $matches[0];
  213. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  214. $phpcsFile->addError($error, $stackPtr);
  215. }
  216. else
  217. if (preg_match('|\d|', $varName))
  218. {
  219. $warning = "Variable \"$originalVarName\" contains numbers but this is discouraged";
  220. $phpcsFile->addWarning($warning, $stackPtr);
  221. }
  222. }
  223. } //end if
  224. } //end processVariableInString()
  225. } //end class
  226. ?>