PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/squizlabs/php_codesniffer/src/Standards/Zend/Sniffs/NamingConventions/ValidVariableNameSniff.php

https://bitbucket.org/ClintSosa/wp-bedrock
PHP | 232 lines | 150 code | 30 blank | 52 comment | 28 complexity | 9cb41fa4ec838b9581a1467d845f88f1 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /**
  3. * Checks the naming of variables and member variables.
  4. *
  5. * @author Greg Sherwood <gsherwood@squiz.net>
  6. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8. */
  9. namespace PHP_CodeSniffer\Standards\Zend\Sniffs\NamingConventions;
  10. use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
  11. use PHP_CodeSniffer\Util\Common;
  12. use PHP_CodeSniffer\Files\File;
  13. class ValidVariableNameSniff extends AbstractVariableSniff
  14. {
  15. /**
  16. * Tokens to ignore so that we can find a DOUBLE_COLON.
  17. *
  18. * @var array
  19. */
  20. private $ignore = [
  21. T_WHITESPACE,
  22. T_COMMENT,
  23. ];
  24. /**
  25. * Processes this test, when one of its tokens is encountered.
  26. *
  27. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  28. * @param int $stackPtr The position of the current token in the
  29. * stack passed in $tokens.
  30. *
  31. * @return void
  32. */
  33. protected function processVariable(File $phpcsFile, $stackPtr)
  34. {
  35. $tokens = $phpcsFile->getTokens();
  36. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  37. $phpReservedVars = [
  38. '_SERVER' => true,
  39. '_GET' => true,
  40. '_POST' => true,
  41. '_REQUEST' => true,
  42. '_SESSION' => true,
  43. '_ENV' => true,
  44. '_COOKIE' => true,
  45. '_FILES' => true,
  46. 'GLOBALS' => true,
  47. 'http_response_header' => true,
  48. 'HTTP_RAW_POST_DATA' => true,
  49. 'php_errormsg' => true,
  50. ];
  51. // If it's a php reserved var, then its ok.
  52. if (isset($phpReservedVars[$varName]) === true) {
  53. return;
  54. }
  55. $objOperator = $phpcsFile->findNext([T_WHITESPACE], ($stackPtr + 1), null, true);
  56. if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
  57. // Check to see if we are using a variable from an object.
  58. $var = $phpcsFile->findNext([T_WHITESPACE], ($objOperator + 1), null, true);
  59. if ($tokens[$var]['code'] === T_STRING) {
  60. // Either a var name or a function call, so check for bracket.
  61. $bracket = $phpcsFile->findNext([T_WHITESPACE], ($var + 1), null, true);
  62. if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
  63. $objVarName = $tokens[$var]['content'];
  64. // There is no way for us to know if the var is public or private,
  65. // so we have to ignore a leading underscore if there is one and just
  66. // check the main part of the variable name.
  67. $originalVarName = $objVarName;
  68. if (substr($objVarName, 0, 1) === '_') {
  69. $objVarName = substr($objVarName, 1);
  70. }
  71. if (Common::isCamelCaps($objVarName, false, true, false) === false) {
  72. $error = 'Variable "%s" is not in valid camel caps format';
  73. $data = [$originalVarName];
  74. $phpcsFile->addError($error, $var, 'NotCamelCaps', $data);
  75. } else if (preg_match('|\d|', $objVarName) === 1) {
  76. $warning = 'Variable "%s" contains numbers but this is discouraged';
  77. $data = [$originalVarName];
  78. $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
  79. }
  80. }//end if
  81. }//end if
  82. }//end if
  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 = $varName;
  87. if (substr($varName, 0, 1) === '_') {
  88. $objOperator = $phpcsFile->findPrevious([T_WHITESPACE], ($stackPtr - 1), null, true);
  89. if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
  90. // The variable lives within a class, and is referenced like
  91. // this: MyClass::$_variable, so we don't know its scope.
  92. $inClass = true;
  93. } else {
  94. $inClass = $phpcsFile->hasCondition($stackPtr, [T_CLASS, T_INTERFACE, T_TRAIT]);
  95. }
  96. if ($inClass === true) {
  97. $varName = substr($varName, 1);
  98. }
  99. }
  100. if (Common::isCamelCaps($varName, false, true, false) === false) {
  101. $error = 'Variable "%s" is not in valid camel caps format';
  102. $data = [$originalVarName];
  103. $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
  104. } else if (preg_match('|\d|', $varName) === 1) {
  105. $warning = 'Variable "%s" contains numbers but this is discouraged';
  106. $data = [$originalVarName];
  107. $phpcsFile->addWarning($warning, $stackPtr, 'ContainsNumbers', $data);
  108. }
  109. }//end processVariable()
  110. /**
  111. * Processes class member variables.
  112. *
  113. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  114. * @param int $stackPtr The position of the current token in the
  115. * stack passed in $tokens.
  116. *
  117. * @return void
  118. */
  119. protected function processMemberVar(File $phpcsFile, $stackPtr)
  120. {
  121. $tokens = $phpcsFile->getTokens();
  122. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  123. $memberProps = $phpcsFile->getMemberProperties($stackPtr);
  124. if (empty($memberProps) === true) {
  125. // Exception encountered.
  126. return;
  127. }
  128. $public = ($memberProps['scope'] === 'public');
  129. if ($public === true) {
  130. if (substr($varName, 0, 1) === '_') {
  131. $error = 'Public member variable "%s" must not contain a leading underscore';
  132. $data = [$varName];
  133. $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
  134. return;
  135. }
  136. } else {
  137. if (substr($varName, 0, 1) !== '_') {
  138. $scope = ucfirst($memberProps['scope']);
  139. $error = '%s member variable "%s" must contain a leading underscore';
  140. $data = [
  141. $scope,
  142. $varName,
  143. ];
  144. $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
  145. return;
  146. }
  147. }
  148. if (Common::isCamelCaps($varName, false, $public, false) === false) {
  149. $error = 'Member variable "%s" is not in valid camel caps format';
  150. $data = [$varName];
  151. $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
  152. } else if (preg_match('|\d|', $varName) === 1) {
  153. $warning = 'Member variable "%s" contains numbers but this is discouraged';
  154. $data = [$varName];
  155. $phpcsFile->addWarning($warning, $stackPtr, 'MemberVarContainsNumbers', $data);
  156. }
  157. }//end processMemberVar()
  158. /**
  159. * Processes the variable found within a double quoted string.
  160. *
  161. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  162. * @param int $stackPtr The position of the double quoted
  163. * string.
  164. *
  165. * @return void
  166. */
  167. protected function processVariableInString(File $phpcsFile, $stackPtr)
  168. {
  169. $tokens = $phpcsFile->getTokens();
  170. $phpReservedVars = [
  171. '_SERVER',
  172. '_GET',
  173. '_POST',
  174. '_REQUEST',
  175. '_SESSION',
  176. '_ENV',
  177. '_COOKIE',
  178. '_FILES',
  179. 'GLOBALS',
  180. 'http_response_header',
  181. 'HTTP_RAW_POST_DATA',
  182. 'php_errormsg',
  183. ];
  184. if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
  185. foreach ($matches[1] as $varName) {
  186. // If it's a php reserved var, then its ok.
  187. if (in_array($varName, $phpReservedVars) === true) {
  188. continue;
  189. }
  190. if (Common::isCamelCaps($varName, false, true, false) === false) {
  191. $error = 'Variable "%s" is not in valid camel caps format';
  192. $data = [$varName];
  193. $phpcsFile->addError($error, $stackPtr, 'StringVarNotCamelCaps', $data);
  194. } else if (preg_match('|\d|', $varName) === 1) {
  195. $warning = 'Variable "%s" contains numbers but this is discouraged';
  196. $data = [$varName];
  197. $phpcsFile->addWarning($warning, $stackPtr, 'StringVarContainsNumbers', $data);
  198. }
  199. }//end foreach
  200. }//end if
  201. }//end processVariableInString()
  202. }//end class