/php/pear/PHP/CodeSniffer/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php

https://gitlab.com/trang1104/portable_project · PHP · 252 lines · 148 code · 30 blank · 74 comment · 30 complexity · c2e97c5b9bc866ef9983271bfb85ec75 MD5 · raw file

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