/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php

https://gitlab.com/trang1104/portable_project · PHP · 74 lines · 25 code · 11 blank · 38 comment · 4 complexity · 71bd982fe788449441619c0753b55a69 MD5 · raw file

  1. <?php
  2. /**
  3. * Squiz_Sniffs_NamingConventions_ValidFunctionNameSniff.
  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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff not found');
  17. }
  18. /**
  19. * Squiz_Sniffs_NamingConventions_ValidFunctionNameSniff.
  20. *
  21. * Ensures method names are correct depending on whether they are public
  22. * or private, and that functions are named correctly.
  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-2011 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.3.3
  31. * @link http://pear.php.net/package/PHP_CodeSniffer
  32. */
  33. class Squiz_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff
  34. {
  35. /**
  36. * Processes the tokens outside the scope.
  37. *
  38. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  39. * @param int $stackPtr The position where this token was
  40. * found.
  41. *
  42. * @return void
  43. */
  44. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  45. {
  46. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  47. if ($functionName === null) {
  48. return;
  49. }
  50. $errorData = array($functionName);
  51. // Does this function claim to be magical?
  52. if (preg_match('|^__|', $functionName) !== 0) {
  53. $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  54. $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData);
  55. return;
  56. }
  57. if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) {
  58. $error = 'Function name "%s" is not in camel caps format';
  59. $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
  60. }
  61. }//end processTokenOutsideScope()
  62. }//end class
  63. ?>