PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/0072016/PHP_CodeSniffer
PHP | 245 lines | 146 code | 28 blank | 71 comment | 28 complexity | ed723bff912fbafea8601e076ea92952 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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt 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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  28. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  29. * @version Release: @package_version@
  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. 'http_response_header',
  67. 'HTTP_RAW_POST_DATA',
  68. 'php_errormsg',
  69. );
  70. // If it's a php reserved var, then its ok.
  71. if (in_array($varName, $phpReservedVars) === true) {
  72. return;
  73. }
  74. $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
  75. if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
  76. // Check to see if we are using a variable from an object.
  77. $var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
  78. if ($tokens[$var]['code'] === T_STRING) {
  79. // Either a var name or a function call, so check for bracket.
  80. $bracket = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
  81. if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
  82. $objVarName = $tokens[$var]['content'];
  83. // There is no way for us to know if the var is public or private,
  84. // so we have to ignore a leading underscore if there is one and just
  85. // check the main part of the variable name.
  86. $originalVarName = $objVarName;
  87. if (substr($objVarName, 0, 1) === '_') {
  88. $objVarName = substr($objVarName, 1);
  89. }
  90. if (PHP_CodeSniffer::isCamelCaps($objVarName, false, true, false) === false) {
  91. $error = 'Variable "%s" is not in valid camel caps format';
  92. $data = array($originalVarName);
  93. $phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
  94. } else if (preg_match('|\d|', $objVarName) === 1) {
  95. $warning = 'Variable "%s" contains numbers but this is discouraged';
  96. $data = array($originalVarName);
  97. $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
  98. }
  99. }//end if
  100. }//end if
  101. }//end if
  102. // There is no way for us to know if the var is public or private,
  103. // so we have to ignore a leading underscore if there is one and just
  104. // check the main part of the variable name.
  105. $originalVarName = $varName;
  106. if (substr($varName, 0, 1) === '_') {
  107. $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
  108. if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
  109. // The variable lives within a class, and is referenced like
  110. // this: MyClass::$_variable, so we don't know its scope.
  111. $inClass = true;
  112. } else {
  113. $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE, T_TRAIT));
  114. }
  115. if ($inClass === true) {
  116. $varName = substr($varName, 1);
  117. }
  118. }
  119. if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
  120. $error = 'Variable "%s" is not in valid camel caps format';
  121. $data = array($originalVarName);
  122. $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
  123. } else if (preg_match('|\d|', $varName) === 1) {
  124. $warning = 'Variable "%s" contains numbers but this is discouraged';
  125. $data = array($originalVarName);
  126. $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
  127. }
  128. }//end processVariable()
  129. /**
  130. * Processes class member variables.
  131. *
  132. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  133. * @param int $stackPtr The position of the current token in the
  134. * stack passed in $tokens.
  135. *
  136. * @return void
  137. */
  138. protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  139. {
  140. $tokens = $phpcsFile->getTokens();
  141. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  142. $memberProps = $phpcsFile->getMemberProperties($stackPtr);
  143. $public = ($memberProps['scope'] === 'public');
  144. if ($public === true) {
  145. if (substr($varName, 0, 1) === '_') {
  146. $error = 'Public member variable "%s" must not contain a leading underscore';
  147. $data = array($varName);
  148. $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
  149. return;
  150. }
  151. } else {
  152. if (substr($varName, 0, 1) !== '_') {
  153. $scope = ucfirst($memberProps['scope']);
  154. $error = '%s member variable "%s" must contain a leading underscore';
  155. $data = array(
  156. $scope,
  157. $varName,
  158. );
  159. $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
  160. return;
  161. }
  162. }
  163. if (PHP_CodeSniffer::isCamelCaps($varName, false, $public, false) === false) {
  164. $error = 'Member variable "%s" is not in valid camel caps format';
  165. $data = array($varName);
  166. $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
  167. } else if (preg_match('|\d|', $varName) === 1) {
  168. $warning = 'Member variable "%s" contains numbers but this is discouraged';
  169. $data = array($varName);
  170. $phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
  171. }
  172. }//end processMemberVar()
  173. /**
  174. * Processes the variable found within a double quoted string.
  175. *
  176. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  177. * @param int $stackPtr The position of the double quoted
  178. * string.
  179. *
  180. * @return void
  181. */
  182. protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  183. {
  184. $tokens = $phpcsFile->getTokens();
  185. $phpReservedVars = array(
  186. '_SERVER',
  187. '_GET',
  188. '_POST',
  189. '_REQUEST',
  190. '_SESSION',
  191. '_ENV',
  192. '_COOKIE',
  193. '_FILES',
  194. 'GLOBALS',
  195. 'http_response_header',
  196. 'HTTP_RAW_POST_DATA',
  197. 'php_errormsg',
  198. );
  199. if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
  200. foreach ($matches[1] as $varName) {
  201. // If it's a php reserved var, then its ok.
  202. if (in_array($varName, $phpReservedVars) === true) {
  203. continue;
  204. }
  205. if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
  206. $error = 'Variable "%s" is not in valid camel caps format';
  207. $data = array($varName);
  208. $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
  209. } else if (preg_match('|\d|', $varName) === 1) {
  210. $warning = 'Variable "%s" contains numbers but this is discouraged';
  211. $data = array($varName);
  212. $phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
  213. }
  214. }//end foreach
  215. }//end if
  216. }//end processVariableInString()
  217. }//end class