PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php

https://gitlab.com/link233/bootmw
PHP | 121 lines | 45 code | 18 blank | 58 comment | 9 complexity | a6b8461b87fbec90686e86cf3c28047c MD5 | raw file
  1. <?php
  2. /**
  3. * PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
  15. $error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
  16. throw new PHP_CodeSniffer_Exception($error);
  17. }
  18. /**
  19. * PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
  20. *
  21. * Checks the naming of member variables.
  22. *
  23. * @category PHP
  24. * @package PHP_CodeSniffer
  25. * @author Greg Sherwood <gsherwood@squiz.net>
  26. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  27. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  28. * @version Release: @package_version@
  29. * @link http://pear.php.net/package/PHP_CodeSniffer
  30. */
  31. class PEAR_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
  32. {
  33. /**
  34. * Processes class member variables.
  35. *
  36. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  37. * @param int $stackPtr The position of the current token
  38. * in the stack passed in $tokens.
  39. *
  40. * @return void
  41. */
  42. protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  43. {
  44. $tokens = $phpcsFile->getTokens();
  45. $memberProps = $phpcsFile->getMemberProperties($stackPtr);
  46. if (empty($memberProps) === true) {
  47. return;
  48. }
  49. $memberName = ltrim($tokens[$stackPtr]['content'], '$');
  50. $scope = $memberProps['scope'];
  51. $scopeSpecified = $memberProps['scope_specified'];
  52. if ($memberProps['scope'] === 'private') {
  53. $isPublic = false;
  54. } else {
  55. $isPublic = true;
  56. }
  57. // If it's a private member, it must have an underscore on the front.
  58. if ($isPublic === false && $memberName{0} !== '_') {
  59. $error = 'Private member variable "%s" must be prefixed with an underscore';
  60. $data = array($memberName);
  61. $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
  62. return;
  63. }
  64. // If it's not a private member, it must not have an underscore on the front.
  65. if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
  66. $error = '%s member variable "%s" must not be prefixed with an underscore';
  67. $data = array(
  68. ucfirst($scope),
  69. $memberName,
  70. );
  71. $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
  72. return;
  73. }
  74. }//end processMemberVar()
  75. /**
  76. * Processes normal variables.
  77. *
  78. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  79. * @param int $stackPtr The position where the token was found.
  80. *
  81. * @return void
  82. */
  83. protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  84. {
  85. /*
  86. We don't care about normal variables.
  87. */
  88. }//end processVariable()
  89. /**
  90. * Processes variables in double quoted strings.
  91. *
  92. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  93. * @param int $stackPtr The position where the token was found.
  94. *
  95. * @return void
  96. */
  97. protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  98. {
  99. /*
  100. We don't care about normal variables.
  101. */
  102. }//end processVariableInString()
  103. }//end class