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

/code_standards/lib/CodeSniffer/Standards/Limb/Sniffs/NamingConventions/ValidVariableNameSniff.php

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