PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 233 lines | 127 code | 32 blank | 74 comment | 20 complexity | 1afefa85b2f9011cfdf62c9dcecb8e3c 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 253706 2008-02-25 00:25:02Z 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 Squiz_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. $bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
  70. if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS)
  71. {
  72. $objVarName = $tokens[$var]['content'];
  73. // There is no way for us to know if the var is public or private,
  74. // so we have to ignore a leading underscore if there is one and just
  75. // check the main part of the variable name.
  76. $originalVarName = $objVarName;
  77. if (substr($objVarName, 0, 1) === '_')
  78. {
  79. $objVarName = substr($objVarName, 1);
  80. }
  81. if (PHP_CodeSniffer :: isCamelCaps($objVarName, false, true, false) === false)
  82. {
  83. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  84. $phpcsFile->addError($error, $var);
  85. }
  86. } //end if
  87. } //end if
  88. } //end if
  89. // There is no way for us to know if the var is public or private,
  90. // so we have to ignore a leading underscore if there is one and just
  91. // check the main part of the variable name.
  92. $originalVarName = $varName;
  93. if (substr($varName, 0, 1) === '_')
  94. {
  95. $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
  96. if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON)
  97. {
  98. // The variable lives within a class, and is referenced like
  99. // this: MyClass::$_variable, so we don't know its scope.
  100. $inClass = true;
  101. }
  102. else
  103. {
  104. $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE));
  105. }
  106. if ($inClass === true)
  107. {
  108. $varName = substr($varName, 1);
  109. }
  110. }
  111. if (PHP_CodeSniffer :: isCamelCaps($varName, false, true, false) === false)
  112. {
  113. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  114. $phpcsFile->addError($error, $stackPtr);
  115. }
  116. } //end processVariable()
  117. /**
  118. * Processes class member variables.
  119. *
  120. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  121. * @param int $stackPtr The position of the current token in the
  122. * stack passed in $tokens.
  123. *
  124. * @return void
  125. */
  126. protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  127. {
  128. $tokens = $phpcsFile->getTokens();
  129. $varName = ltrim($tokens[$stackPtr]['content'], '$');
  130. $memberProps = $phpcsFile->getMemberProperties($stackPtr);
  131. $public = ($memberProps['scope'] !== 'private');
  132. if ($public === true)
  133. {
  134. if (substr($varName, 0, 1) === '_')
  135. {
  136. $scope = ucfirst($memberProps['scope']);
  137. $error = "$scope member variable \"$varName\" must not contain a leading underscore";
  138. $phpcsFile->addError($error, $stackPtr);
  139. return;
  140. }
  141. }
  142. else
  143. {
  144. if (substr($varName, 0, 1) !== '_')
  145. {
  146. $error = "Private member variable \"$varName\" must contain a leading underscore";
  147. $phpcsFile->addError($error, $stackPtr);
  148. return;
  149. }
  150. }
  151. if (PHP_CodeSniffer :: isCamelCaps($varName, false, $public, false) === false)
  152. {
  153. $error = "Variable \"$varName\" is not in valid camel caps format";
  154. $phpcsFile->addError($error, $stackPtr);
  155. }
  156. } //end processMemberVar()
  157. /**
  158. * Processes the variable found within a double quoted string.
  159. *
  160. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  161. * @param int $stackPtr The position of the double quoted
  162. * string.
  163. *
  164. * @return void
  165. */
  166. protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  167. {
  168. $tokens = $phpcsFile->getTokens();
  169. $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES',
  170. 'GLOBALS');
  171. if (preg_match_all('|[^\\\]\$([a-zA-Z0-9_]+)|', $tokens[$stackPtr]['content'], $matches) !== 0)
  172. {
  173. foreach ($matches[1] as $varName)
  174. {
  175. // If it's a php reserved var, then its ok.
  176. if (in_array($varName, $phpReservedVars) === true)
  177. {
  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. {
  186. if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true)
  187. {
  188. $varName = substr($varName, 1);
  189. }
  190. }
  191. if (PHP_CodeSniffer :: isCamelCaps($varName, false, true, false) === false)
  192. {
  193. $varName = $matches[0];
  194. $error = "Variable \"$originalVarName\" is not in valid camel caps format";
  195. $phpcsFile->addError($error, $stackPtr);
  196. }
  197. }
  198. } //end if
  199. } //end processVariableInString()
  200. } //end class
  201. ?>