PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/addons/ResellersCenter/vendor/omnipay-2.3/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php

https://bitbucket.org/tecsecret/central-tecsecret
PHP | 290 lines | 167 code | 42 blank | 81 comment | 32 complexity | 860849d16e8fc8d62c0319c4b7f39393 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT, LGPL-2.1, BSD-2-Clause
  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. if (ltrim($functionName, '_') === '') {
  167. // Ignore special functions.
  168. return;
  169. }
  170. $errorData = array($functionName);
  171. // Is this a magic function. i.e., it is prefixed with "__".
  172. if (preg_match('|^__|', $functionName) !== 0) {
  173. $magicPart = strtolower(substr($functionName, 2));
  174. if (in_array($magicPart, $this->magicFunctions) === false) {
  175. $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
  176. $phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
  177. }
  178. return;
  179. }
  180. // Function names can be in two parts; the package name and
  181. // the function name.
  182. $packagePart = '';
  183. $camelCapsPart = '';
  184. $underscorePos = strrpos($functionName, '_');
  185. if ($underscorePos === false) {
  186. $camelCapsPart = $functionName;
  187. } else {
  188. $packagePart = substr($functionName, 0, $underscorePos);
  189. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  190. // We don't care about _'s on the front.
  191. $packagePart = ltrim($packagePart, '_');
  192. }
  193. // If it has a package part, make sure the first letter is a capital.
  194. if ($packagePart !== '') {
  195. if ($functionName{0} === '_') {
  196. $error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
  197. $phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
  198. return;
  199. }
  200. if ($functionName{0} !== strtoupper($functionName{0})) {
  201. $error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
  202. $phpcsFile->addError($error, $stackPtr, 'FunctionNoCapital', $errorData);
  203. return;
  204. }
  205. }
  206. // If it doesn't have a camel caps part, it's not valid.
  207. if (trim($camelCapsPart) === '') {
  208. $error = 'Function name "%s" is not valid; name appears incomplete';
  209. $phpcsFile->addError($error, $stackPtr, 'FunctionInvalid', $errorData);
  210. return;
  211. }
  212. $validName = true;
  213. $newPackagePart = $packagePart;
  214. $newCamelCapsPart = $camelCapsPart;
  215. // Every function must have a camel caps part, so check that first.
  216. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  217. $validName = false;
  218. $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
  219. }
  220. if ($packagePart !== '') {
  221. // Check that each new word starts with a capital.
  222. $nameBits = explode('_', $packagePart);
  223. foreach ($nameBits as $bit) {
  224. if ($bit{0} !== strtoupper($bit{0})) {
  225. $newPackagePart = '';
  226. foreach ($nameBits as $bit) {
  227. $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
  228. }
  229. $validName = false;
  230. break;
  231. }
  232. }
  233. }
  234. if ($validName === false) {
  235. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  236. if ($newPackagePart === '') {
  237. $newName = $newCamelCapsPart;
  238. } else {
  239. $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
  240. }
  241. $error = 'Function name "%s" is invalid; consider "%s" instead';
  242. $data = $errorData;
  243. $data[] = $newName;
  244. $phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid', $data);
  245. }
  246. }//end processTokenOutsideScope()
  247. }//end class
  248. ?>