PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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