PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/share/pear/PHP/CodeSniffer/Standards_Backup/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php

https://github.com/amumu/modev
PHP | 270 lines | 149 code | 40 blank | 81 comment | 31 complexity | 59cd20cbc8624a765571514e59709804 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 292390 2009-12-21 00:32:14Z squiz $
  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: 1.2.2
  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. protected $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. protected $magicFunctions = array('autoload');
  62. /**
  63. * Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
  64. */
  65. public function __construct()
  66. {
  67. parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
  68. }//end __construct()
  69. /**
  70. * Processes the tokens within the scope.
  71. *
  72. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  73. * @param int $stackPtr The position where this token was
  74. * found.
  75. * @param int $currScope The position of the current scope.
  76. *
  77. * @return void
  78. */
  79. protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
  80. {
  81. $methodName = $phpcsFile->getDeclarationName($stackPtr);
  82. if ($methodName === null) {
  83. // Ignore closures.
  84. return;
  85. }
  86. $className = $phpcsFile->getDeclarationName($currScope);
  87. // Is this a magic method. IE. is prefixed with "__".
  88. if (preg_match('|^__|', $methodName) !== 0) {
  89. $magicPart = substr($methodName, 2);
  90. if (in_array($magicPart, $this->magicMethods) === false) {
  91. $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
  92. $phpcsFile->addError($error, $stackPtr);
  93. }
  94. return;
  95. }
  96. // PHP4 constructors are allowed to break our rules.
  97. if ($methodName === $className) {
  98. return;
  99. }
  100. // PHP4 destructors are allowed to break our rules.
  101. if ($methodName === '_'.$className) {
  102. return;
  103. }
  104. $methodProps = $phpcsFile->getMethodProperties($stackPtr);
  105. $isPublic = ($methodProps['scope'] === 'private') ? false : true;
  106. $scope = $methodProps['scope'];
  107. $scopeSpecified = $methodProps['scope_specified'];
  108. // If it's a private method, it must have an underscore on the front.
  109. if ($isPublic === false && $methodName{0} !== '_') {
  110. $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore";
  111. $phpcsFile->addError($error, $stackPtr);
  112. return;
  113. }
  114. // If it's not a private method, it must not have an underscore on the front.
  115. if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
  116. $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore";
  117. $phpcsFile->addError($error, $stackPtr);
  118. return;
  119. }
  120. // If the scope was specified on the method, then the method must be
  121. // camel caps and an underscore should be checked for. If it wasn't
  122. // specified, treat it like a public method and remove the underscore
  123. // prefix if there is one because we cant determine if it is private or
  124. // public.
  125. $testMethodName = $methodName;
  126. if ($scopeSpecified === false && $methodName{0} === '_') {
  127. $testMethodName = substr($methodName, 1);
  128. }
  129. if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
  130. if ($scopeSpecified === true) {
  131. $error = ucfirst($scope)." method name \"$className::$methodName\" is not in camel caps format";
  132. } else {
  133. $error = "Method name \"$className::$methodName\" is not in camel caps format";
  134. }
  135. $phpcsFile->addError($error, $stackPtr);
  136. return;
  137. }
  138. }//end processTokenWithinScope()
  139. /**
  140. * Processes the tokens outside the scope.
  141. *
  142. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  143. * @param int $stackPtr The position where this token was
  144. * found.
  145. *
  146. * @return void
  147. */
  148. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  149. {
  150. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  151. if ($functionName === null) {
  152. // Ignore closures.
  153. return;
  154. }
  155. // Is this a magic function. IE. is prefixed with "__".
  156. if (preg_match('|^__|', $functionName) !== 0) {
  157. $magicPart = substr($functionName, 2);
  158. if (in_array($magicPart, $this->magicFunctions) === false) {
  159. $error = "Function name \"$functionName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
  160. $phpcsFile->addError($error, $stackPtr);
  161. }
  162. return;
  163. }
  164. // Function names can be in two parts; the package name and
  165. // the function name.
  166. $packagePart = '';
  167. $camelCapsPart = '';
  168. $underscorePos = strrpos($functionName, '_');
  169. if ($underscorePos === false) {
  170. $camelCapsPart = $functionName;
  171. } else {
  172. $packagePart = substr($functionName, 0, $underscorePos);
  173. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  174. // We don't care about _'s on the front.
  175. $packagePart = ltrim($packagePart, '_');
  176. }
  177. // If it has a package part, make sure the first letter is a capital.
  178. if ($packagePart !== '') {
  179. if ($functionName{0} === '_') {
  180. $error = "Function name \"$functionName\" is invalid; only private methods should be prefixed with an underscore";
  181. $phpcsFile->addError($error, $stackPtr);
  182. return;
  183. }
  184. if ($functionName{0} !== strtoupper($functionName{0})) {
  185. $error = "Function name \"$functionName\" is prefixed with a package name but does not begin with a capital letter";
  186. $phpcsFile->addError($error, $stackPtr);
  187. return;
  188. }
  189. }
  190. // If it doesn't have a camel caps part, it's not valid.
  191. if (trim($camelCapsPart) === '') {
  192. $error = "Function name \"$functionName\" is not valid; name appears incomplete";
  193. $phpcsFile->addError($error, $stackPtr);
  194. return;
  195. }
  196. $validName = true;
  197. $newPackagePart = $packagePart;
  198. $newCamelCapsPart = $camelCapsPart;
  199. // Every function must have a camel caps part, so check that first.
  200. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  201. $validName = false;
  202. $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
  203. }
  204. if ($packagePart !== '') {
  205. // Check that each new word starts with a capital.
  206. $nameBits = explode('_', $packagePart);
  207. foreach ($nameBits as $bit) {
  208. if ($bit{0} !== strtoupper($bit{0})) {
  209. $newPackagePart = '';
  210. foreach ($nameBits as $bit) {
  211. $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
  212. }
  213. $validName = false;
  214. break;
  215. }
  216. }
  217. }
  218. if ($validName === false) {
  219. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  220. if ($newPackagePart === '') {
  221. $newName = $newCamelCapsPart;
  222. } else {
  223. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  224. }
  225. $error = "Function name \"$functionName\" is invalid; consider \"$newName\" instead";
  226. $phpcsFile->addError($error, $stackPtr);
  227. }
  228. }//end processTokenOutsideScope()
  229. }//end class
  230. ?>