PageRenderTime 24ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/sfAuditPlugin/lib/vendor/PHP/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php

https://github.com/mikesname/ehri-ica-atom
PHP | 263 lines | 145 code | 39 blank | 79 comment | 29 complexity | 6e4ba5c1c287b280b8ce703cba0f9879 MD5 | raw file
  1. <?php
  2. /**
  3. * PEAR_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 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: ValidFunctionNameSniff.php,v 1.18 2008/01/22 23:50:23 squiz Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  17. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
  18. }
  19. /**
  20. * PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
  21. *
  22. * Ensures method names are correct depending on whether they are public
  23. * or private, and that functions are named correctly.
  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: @package_version@
  32. * @link http://pear.php.net/package/PHP_CodeSniffer
  33. */
  34. class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  35. {
  36. /**
  37. * A list of all PHP magic methods.
  38. *
  39. * @var array
  40. */
  41. private $_magicMethods = array(
  42. 'construct',
  43. 'destruct',
  44. 'call',
  45. 'callStatic',
  46. 'get',
  47. 'set',
  48. 'isset',
  49. 'unset',
  50. 'sleep',
  51. 'wakeup',
  52. 'toString',
  53. 'set_state',
  54. 'clone',
  55. );
  56. /**
  57. * A list of all PHP magic functions.
  58. *
  59. * @var array
  60. */
  61. private $_magicFunctions = array(
  62. 'autoload',
  63. );
  64. /**
  65. * Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
  66. */
  67. public function __construct()
  68. {
  69. parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
  70. }//end __construct()
  71. /**
  72. * Processes the tokens within the scope.
  73. *
  74. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  75. * @param int $stackPtr The position where this token was
  76. * found.
  77. * @param int $currScope The position of the current scope.
  78. *
  79. * @return void
  80. */
  81. protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
  82. {
  83. $className = $phpcsFile->getDeclarationName($currScope);
  84. $methodName = $phpcsFile->getDeclarationName($stackPtr);
  85. // Is this a magic method. IE. is prefixed with "__".
  86. if (preg_match('|^__|', $methodName) !== 0) {
  87. $magicPart = substr($methodName, 2);
  88. if (in_array($magicPart, $this->_magicMethods) === false) {
  89. $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
  90. $phpcsFile->addError($error, $stackPtr);
  91. }
  92. return;
  93. }
  94. // PHP4 constructors are allowed to break our rules.
  95. if ($methodName === $className) {
  96. return;
  97. }
  98. // PHP4 destructors are allowed to break our rules.
  99. if ($methodName === '_'.$className) {
  100. return;
  101. }
  102. $methodProps = $phpcsFile->getMethodProperties($stackPtr);
  103. $isPublic = ($methodProps['scope'] === 'private') ? false : true;
  104. $scope = $methodProps['scope'];
  105. $scopeSpecified = $methodProps['scope_specified'];
  106. // If it's a private method, it must have an underscore on the front.
  107. if ($isPublic === false && $methodName{0} !== '_') {
  108. $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore";
  109. $phpcsFile->addError($error, $stackPtr);
  110. return;
  111. }
  112. // If it's not a private method, it must not have an underscore on the front.
  113. if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
  114. $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore";
  115. $phpcsFile->addError($error, $stackPtr);
  116. return;
  117. }
  118. // If the scope was specified on the method, then the method must be
  119. // camel caps and an underscore should be checked for. If it wasn't
  120. // specified, treat it like a public method and remove the underscore
  121. // prefix if there is one because we cant determine if it is private or
  122. // public.
  123. $testMethodName = $methodName;
  124. if ($scopeSpecified === false && $methodName{0} === '_') {
  125. $testMethodName = substr($methodName, 1);
  126. }
  127. if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
  128. if ($scopeSpecified === true) {
  129. $error = ucfirst($scope)." method name \"$className::$methodName\" is not in camel caps format";
  130. } else {
  131. $error = "Method name \"$className::$methodName\" is not in camel caps format";
  132. }
  133. $phpcsFile->addError($error, $stackPtr);
  134. return;
  135. }
  136. }//end processTokenWithinScope()
  137. /**
  138. * Processes the tokens outside the scope.
  139. *
  140. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  141. * @param int $stackPtr The position where this token was
  142. * found.
  143. *
  144. * @return void
  145. */
  146. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  147. {
  148. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  149. // Is this a magic function. IE. is prefixed with "__".
  150. if (preg_match('|^__|', $functionName) !== 0) {
  151. $magicPart = substr($functionName, 2);
  152. if (in_array($magicPart, $this->_magicFunctions) === false) {
  153. $error = "Function name \"$functionName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
  154. $phpcsFile->addError($error, $stackPtr);
  155. }
  156. return;
  157. }
  158. // Function names can be in two parts; the package name and
  159. // the function name.
  160. $packagePart = '';
  161. $camelCapsPart = '';
  162. $underscorePos = strrpos($functionName, '_');
  163. if ($underscorePos === false) {
  164. $camelCapsPart = $functionName;
  165. } else {
  166. $packagePart = substr($functionName, 0, $underscorePos);
  167. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  168. // We don't care about _'s on the front.
  169. $packagePart = ltrim($packagePart, '_');
  170. }
  171. // If it has a package part, make sure the first letter is a capital.
  172. if ($packagePart !== '') {
  173. if ($functionName{0} === '_') {
  174. $error = "Function name \"$functionName\" is invalid; only private methods should be prefixed with an underscore";
  175. $phpcsFile->addError($error, $stackPtr);
  176. return;
  177. }
  178. if ($functionName{0} !== strtoupper($functionName{0})) {
  179. $error = "Function name \"$functionName\" is prefixed with a package name but does not begin with a capital letter";
  180. $phpcsFile->addError($error, $stackPtr);
  181. return;
  182. }
  183. }
  184. // If it doesn't have a camel caps part, it's not valid.
  185. if (trim($camelCapsPart) === '') {
  186. $error = "Function name \"$functionName\" is not valid; name appears incomplete";
  187. $phpcsFile->addError($error, $stackPtr);
  188. return;
  189. }
  190. $validName = true;
  191. $newPackagePart = $packagePart;
  192. $newCamelCapsPart = $camelCapsPart;
  193. // Every function must have a camel caps part, so check that first.
  194. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  195. $validName = false;
  196. $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
  197. }
  198. if ($packagePart !== '') {
  199. // Check that each new word starts with a capital.
  200. $nameBits = explode('_', $packagePart);
  201. foreach ($nameBits as $bit) {
  202. if ($bit{0} !== strtoupper($bit{0})) {
  203. $newPackagePart = '';
  204. foreach ($nameBits as $bit) {
  205. $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
  206. }
  207. $validName = false;
  208. break;
  209. }
  210. }
  211. }
  212. if ($validName === false) {
  213. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  214. if ($newPackagePart === '') {
  215. $newName = $newCamelCapsPart;
  216. } else {
  217. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  218. }
  219. $error = "Function name \"$functionName\" is invalid; consider \"$newName\" instead";
  220. $phpcsFile->addError($error, $stackPtr);
  221. }
  222. }//end processTokenOutsideScope()
  223. }//end class
  224. ?>