PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/yousafsyed/easternglamor
PHP | 285 lines | 164 code | 41 blank | 80 comment | 31 complexity | dc18e718edd1961048aafabbf0343303 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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
  17. }
  18. /**
  19. * PEAR_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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  29. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  30. * @version Release: @package_version@
  31. * @link http://pear.php.net/package/PHP_CodeSniffer
  32. */
  33. class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  34. {
  35. /**
  36. * A list of all PHP magic methods.
  37. *
  38. * @var array
  39. */
  40. protected $magicMethods = array(
  41. 'construct',
  42. 'destruct',
  43. 'call',
  44. 'callstatic',
  45. 'get',
  46. 'set',
  47. 'isset',
  48. 'unset',
  49. 'sleep',
  50. 'wakeup',
  51. 'tostring',
  52. 'set_state',
  53. 'clone',
  54. 'invoke',
  55. 'call',
  56. );
  57. /**
  58. * A list of all PHP magic functions.
  59. *
  60. * @var array
  61. */
  62. protected $magicFunctions = array('autoload');
  63. /**
  64. * Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
  65. */
  66. public function __construct()
  67. {
  68. parent::__construct(array(T_CLASS, T_INTERFACE, T_TRAIT), 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. i.e., is prefixed with "__" ?
  90. if (preg_match('|^__|', $methodName) !== 0) {
  91. $magicPart = strtolower(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. // If it's a private method, it must have an underscore on the front.
  111. if ($isPublic === false && $methodName{0} !== '_') {
  112. $error = 'Private method name "%s" must be prefixed with an underscore';
  113. $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
  114. return;
  115. }
  116. // If it's not a private method, it must not have an underscore on the front.
  117. if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
  118. $error = '%s method name "%s" must not be prefixed with an underscore';
  119. $data = array(
  120. ucfirst($scope),
  121. $errorData[0],
  122. );
  123. $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
  124. return;
  125. }
  126. // If the scope was specified on the method, then the method must be
  127. // camel caps and an underscore should be checked for. If it wasn't
  128. // specified, treat it like a public method and remove the underscore
  129. // prefix if there is one because we cant determine if it is private or
  130. // public.
  131. $testMethodName = $methodName;
  132. if ($scopeSpecified === false && $methodName{0} === '_') {
  133. $testMethodName = substr($methodName, 1);
  134. }
  135. if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
  136. if ($scopeSpecified === true) {
  137. $error = '%s method name "%s" is not in camel caps format';
  138. $data = array(
  139. ucfirst($scope),
  140. $errorData[0],
  141. );
  142. $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
  143. } else {
  144. $error = 'Method name "%s" is not in camel caps format';
  145. $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
  146. }
  147. return;
  148. }
  149. }//end processTokenWithinScope()
  150. /**
  151. * Processes the tokens outside the scope.
  152. *
  153. * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  154. * @param int $stackPtr The position where this token was
  155. * found.
  156. *
  157. * @return void
  158. */
  159. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  160. {
  161. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  162. if ($functionName === null) {
  163. // Ignore closures.
  164. return;
  165. }
  166. $errorData = array($functionName);
  167. // Is this a magic function. i.e., it is prefixed with "__".
  168. if (preg_match('|^__|', $functionName) !== 0) {
  169. $magicPart = strtolower(substr($functionName, 2));
  170. if (in_array($magicPart, $this->magicFunctions) === false) {
  171. $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  172. $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
  173. }
  174. return;
  175. }
  176. // Function names can be in two parts; the package name and
  177. // the function name.
  178. $packagePart = '';
  179. $camelCapsPart = '';
  180. $underscorePos = strrpos($functionName, '_');
  181. if ($underscorePos === false) {
  182. $camelCapsPart = $functionName;
  183. } else {
  184. $packagePart = substr($functionName, 0, $underscorePos);
  185. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  186. // We don't care about _'s on the front.
  187. $packagePart = ltrim($packagePart, '_');
  188. }
  189. // If it has a package part, make sure the first letter is a capital.
  190. if ($packagePart !== '') {
  191. if ($functionName{0} === '_') {
  192. $error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
  193. $phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
  194. return;
  195. }
  196. if ($functionName{0} !== strtoupper($functionName{0})) {
  197. $error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
  198. $phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData);
  199. return;
  200. }
  201. }
  202. // If it doesn't have a camel caps part, it's not valid.
  203. if (trim($camelCapsPart) === '') {
  204. $error = 'Function name "%s" is not valid; name appears incomplete';
  205. $phpcsFile->addError($error, $stackPtr, 'FunctionInvalid', $errorData);
  206. return;
  207. }
  208. $validName = true;
  209. $newPackagePart = $packagePart;
  210. $newCamelCapsPart = $camelCapsPart;
  211. // Every function must have a camel caps part, so check that first.
  212. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  213. $validName = false;
  214. $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
  215. }
  216. if ($packagePart !== '') {
  217. // Check that each new word starts with a capital.
  218. $nameBits = explode('_', $packagePart);
  219. foreach ($nameBits as $bit) {
  220. if ($bit{0} !== strtoupper($bit{0})) {
  221. $newPackagePart = '';
  222. foreach ($nameBits as $bit) {
  223. $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
  224. }
  225. $validName = false;
  226. break;
  227. }
  228. }
  229. }
  230. if ($validName === false) {
  231. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  232. if ($newPackagePart === '') {
  233. $newName = $newCamelCapsPart;
  234. } else {
  235. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  236. }
  237. $error = 'Function name "%s" is invalid; consider "%s" instead';
  238. $data = $errorData;
  239. $data[] = $newName;
  240. $phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid', $data);
  241. }
  242. }//end processTokenOutsideScope()
  243. }//end class
  244. ?>