PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/build/phpcs/Joomla/Sniffs/NamingConventions/ValidFunctionNameSniff.php

http://github.com/joomla/joomla-platform
PHP | 318 lines | 189 code | 44 blank | 85 comment | 36 complexity | 93fac0c2250d11bfa88186bea8c42fa3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Joomla_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. * Joomla_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.3.0RC2
  32. * @link http://pear.php.net/package/PHP_CodeSniffer
  33. */
  34. class Joomla_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. 'invoke',
  56. );
  57. /**
  58. * A list of all PHP magic functions.
  59. *
  60. * @var array
  61. */
  62. protected $magicFunctions = array('autoload');
  63. /**
  64. * Constructs a Joomla_Sniffs_NamingConventions_ValidFunctionNameSniff.
  65. */
  66. public function __construct()
  67. {
  68. parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
  69. }//end __construct()
  70. /**
  71. * Processes the tokens within the scope.
  72. *
  73. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  74. * @param int $stackPtr The position where this token was
  75. * found.
  76. * @param int $currScope The position of the current scope.
  77. *
  78. * @return void
  79. */
  80. protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
  81. {
  82. $methodName = $phpcsFile->getDeclarationName($stackPtr);
  83. if ($methodName === null) {
  84. // Ignore closures.
  85. return;
  86. }
  87. $className = $phpcsFile->getDeclarationName($currScope);
  88. $errorData = array($className.'::'.$methodName);
  89. // Is this a magic method. IE. is prefixed with "__".
  90. if (preg_match('|^__|', $methodName) !== 0) {
  91. $magicPart = substr($methodName, 2);
  92. if (in_array($magicPart, $this->magicMethods) === false) {
  93. $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  94. $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
  95. }
  96. return;
  97. }
  98. // PHP4 constructors are allowed to break our rules.
  99. if ($methodName === $className) {
  100. return;
  101. }
  102. // PHP4 destructors are allowed to break our rules.
  103. if ($methodName === '_'.$className) {
  104. return;
  105. }
  106. $methodProps = $phpcsFile->getMethodProperties($stackPtr);
  107. $isPublic = ($methodProps['scope'] === 'private') ? false : true;
  108. $scope = $methodProps['scope'];
  109. $scopeSpecified = $methodProps['scope_specified'];
  110. // Detect if it is marked deprecated
  111. $find = array(
  112. T_COMMENT,
  113. T_DOC_COMMENT,
  114. T_CLASS,
  115. T_FUNCTION,
  116. T_OPEN_TAG,
  117. );
  118. $tokens = $phpcsFile->getTokens();
  119. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1));
  120. if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_DOC_COMMENT) {
  121. $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1;
  122. $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  123. try {
  124. $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
  125. $this->commentParser->parse();
  126. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  127. $line = ($e->getLineWithinComment() + $commentStart);
  128. $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
  129. return;
  130. }
  131. $deprecated = $this->commentParser->getDeprecated();
  132. return !is_null($deprecated);
  133. }
  134. else {
  135. return false;
  136. }
  137. // If it's a private method, it must have an underscore on the front.
  138. if ($isPublic === false && $methodName{0} !== '_') {
  139. $error = 'Private method name "%s" must be prefixed with an underscore';
  140. $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
  141. return;
  142. }
  143. // If it's not a private method, it must not have an underscore on the front.
  144. if ($isDeprecated === false && $isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
  145. $error = '%s method name "%s" must not be prefixed with an underscore';
  146. $data = array(
  147. ucfirst($scope),
  148. $errorData[0],
  149. );
  150. // AJE Changed from error to warning.
  151. $phpcsFile->addWarning($error, $stackPtr, 'PublicUnderscore', $data);
  152. return;
  153. }
  154. // If the scope was specified on the method, then the method must be
  155. // camel caps and an underscore should be checked for. If it wasn't
  156. // specified, treat it like a public method and remove the underscore
  157. // prefix if there is one because we cant determine if it is private or
  158. // public.
  159. $testMethodName = $methodName;
  160. if ($scopeSpecified === false && $methodName{0} === '_') {
  161. $testMethodName = substr($methodName, 1);
  162. }
  163. if ($isDeprecated === false && PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
  164. if ($scopeSpecified === true) {
  165. $error = '%s method name "%s" is not in camel caps format';
  166. $data = array(
  167. ucfirst($scope),
  168. $errorData[0],
  169. );
  170. // AJE Change to warning.
  171. $phpcsFile->addWarning($error, $stackPtr, 'ScopeNotCamelCaps', $data);
  172. } else {
  173. $error = 'Method name "%s" is not in camel caps format';
  174. // AJE Change to warning.
  175. $phpcsFile->addWarning($error, $stackPtr, 'NotCamelCaps', $errorData);
  176. }
  177. return;
  178. }
  179. }//end processTokenWithinScope()
  180. /**
  181. * Processes the tokens outside the scope.
  182. *
  183. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  184. * @param int $stackPtr The position where this token was
  185. * found.
  186. *
  187. * @return void
  188. */
  189. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  190. {
  191. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  192. if ($functionName === null) {
  193. // Ignore closures.
  194. return;
  195. }
  196. $errorData = array($functionName);
  197. // Is this a magic function. IE. is prefixed with "__".
  198. if (preg_match('|^__|', $functionName) !== 0) {
  199. $magicPart = substr($functionName, 2);
  200. if (in_array($magicPart, $this->magicFunctions) === false) {
  201. $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  202. $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
  203. }
  204. return;
  205. }
  206. // Function names can be in two parts; the package name and
  207. // the function name.
  208. $packagePart = '';
  209. $camelCapsPart = '';
  210. $underscorePos = strrpos($functionName, '_');
  211. if ($underscorePos === false) {
  212. $camelCapsPart = $functionName;
  213. } else {
  214. $packagePart = substr($functionName, 0, $underscorePos);
  215. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  216. // We don't care about _'s on the front.
  217. $packagePart = ltrim($packagePart, '_');
  218. }
  219. // If it has a package part, make sure the first letter is a capital.
  220. if ($packagePart !== '') {
  221. if ($functionName{0} === '_') {
  222. $error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
  223. $phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
  224. return;
  225. }
  226. if ($functionName{0} !== strtoupper($functionName{0})) {
  227. $error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
  228. $phpcsFile->addError($error, $stackPtr, 'FunctionNoCaptial', $errorData);
  229. return;
  230. }
  231. }
  232. // If it doesn't have a camel caps part, it's not valid.
  233. if (trim($camelCapsPart) === '') {
  234. $error = 'Function name "%s" is not valid; name appears incomplete';
  235. $phpcsFile->addError($error, $stackPtr, 'FunctionInvalid', $errorData);
  236. return;
  237. }
  238. $validName = true;
  239. $newPackagePart = $packagePart;
  240. $newCamelCapsPart = $camelCapsPart;
  241. // Every function must have a camel caps part, so check that first.
  242. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  243. $validName = false;
  244. $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
  245. }
  246. if ($packagePart !== '') {
  247. // Check that each new word starts with a capital.
  248. $nameBits = explode('_', $packagePart);
  249. foreach ($nameBits as $bit) {
  250. if ($bit{0} !== strtoupper($bit{0})) {
  251. $newPackagePart = '';
  252. foreach ($nameBits as $bit) {
  253. $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
  254. }
  255. $validName = false;
  256. break;
  257. }
  258. }
  259. }
  260. if ($validName === false) {
  261. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  262. if ($newPackagePart === '') {
  263. $newName = $newCamelCapsPart;
  264. } else {
  265. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  266. }
  267. $error = 'Function name "%s" is invalid; consider "%s" instead';
  268. $data = $errorData;
  269. $data[] = $newName;
  270. $phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid', $data);
  271. }
  272. }//end processTokenOutsideScope()
  273. }//end class
  274. ?>