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

/sources/CodeSniffer/Standards/Zend/Sniffs/NamingConventions/ValidFunctionNameSniff.php

https://github.com/proofek/SQLI_CodeSniffer
PHP | 237 lines | 134 code | 29 blank | 74 comment | 25 complexity | a0bde17c3750213cd73746ea30161596 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodingStandard
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  20. */
  21. if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  22. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
  23. }
  24. /**
  25. * Zend_Sniffs_NamingConventions_ValidFunctionNameSniff
  26. *
  27. * Ensures method names are correct depending on whether they are public
  28. * or private, and that functions are named correctly
  29. *
  30. * @category Zend
  31. * @package Zend_CodingStandard
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Sniffs_NamingConventions_ValidFunctionNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  36. {
  37. /**
  38. * A list of all PHP magic methods
  39. *
  40. * @var array
  41. */
  42. private $_magicMethods = array('construct', 'destruct', 'call', 'callStatic', 'get', 'set', 'isset',
  43. 'unset', 'sleep', 'wakeup', 'toString', 'set_state', 'clone');
  44. /**
  45. * A list of all PHP magic functions
  46. *
  47. * @var array
  48. */
  49. private $_magicFunctions = array('autoload');
  50. /**
  51. * Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff
  52. */
  53. public function __construct()
  54. {
  55. parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
  56. }
  57. /**
  58. * Processes the tokens within the scope
  59. *
  60. * @param PHP_CodeSniffer_File $phpcsFile The file being processed
  61. * @param integer $stackPtr The position where this token was found
  62. * @param integer $currScope The position of the current scope
  63. * @return void
  64. */
  65. public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
  66. {
  67. $className = $phpcsFile->getDeclarationName($currScope);
  68. $methodName = $phpcsFile->getDeclarationName($stackPtr);
  69. // Is this a magic method. IE is prefixed with "__"
  70. if (preg_match('|^__|', $methodName) !== 0) {
  71. $magicPart = substr($methodName, 2);
  72. if (in_array($magicPart, $this->_magicMethods) === false) {
  73. $error = "Method name \"$className::$methodName\" is invalid; "
  74. . 'only PHP magic methods should be prefixed with a double underscore';
  75. $phpcsFile->addError($error, $stackPtr);
  76. }
  77. return;
  78. }
  79. // PHP4 constructors are allowed to break our rules
  80. if ($methodName === $className) {
  81. return;
  82. }
  83. // PHP4 destructors are allowed to break our rules
  84. if ($methodName === '_' . $className) {
  85. return;
  86. }
  87. $methodProps = $phpcsFile->getMethodProperties($stackPtr);
  88. $isPublic = ($methodProps['scope'] === 'public') ? true : false;
  89. $scope = $methodProps['scope'];
  90. $scopeSpecified = $methodProps['scope_specified'];
  91. // If it's not a public method, it must have an underscore on the front
  92. if ($isPublic === false and $methodName{0} !== '_') {
  93. $error = ucfirst($scope) . " method name \"$className::$methodName\" must be prefixed with an underscore";
  94. $phpcsFile->addError($error, $stackPtr);
  95. return;
  96. }
  97. // If it's a public method, it must not have an underscore on the front
  98. if ($isPublic === true and $scopeSpecified === true and $methodName{0} === '_') {
  99. $error = ucfirst($scope) . " method name \"$className::$methodName\" must not be prefixed "
  100. . 'with an underscore';
  101. $phpcsFile->addError($error, $stackPtr);
  102. return;
  103. }
  104. // If the scope was specified on the method, then the method must be camel caps and an underscore should
  105. // be checked for. If it wasn't specified, treat it like a public method and remove the underscore
  106. // prefix if there is one because we cant determine if it is private or public
  107. $testMethodName = $methodName;
  108. if ($scopeSpecified === false and $methodName{0} === '_') {
  109. $testMethodName = substr($methodName, 1);
  110. }
  111. if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
  112. if ($scopeSpecified === true) {
  113. $error = ucfirst($scope) . " method name \"$className::$methodName\" is not in camel caps format";
  114. } else {
  115. $error = "Method name \"$className::$methodName\" is not in camel caps format";
  116. }
  117. $phpcsFile->addError($error, $stackPtr);
  118. return;
  119. }
  120. }
  121. /**
  122. * Processes the tokens outside the scope
  123. *
  124. * @param PHP_CodeSniffer_File $phpcsFile The file being processed
  125. * @param integer $stackPtr The position where this token was found
  126. * @return void
  127. */
  128. public function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  129. {
  130. $functionName = $phpcsFile->getDeclarationName($stackPtr);
  131. // Is this a magic function. IE. is prefixed with "__"
  132. if (preg_match('|^__|', $functionName) !== 0) {
  133. $magicPart = substr($functionName, 2);
  134. if (in_array($magicPart, $this->_magicFunctions) === false) {
  135. $error = 'Function name ' . $functionName . ' is invalid; only PHP magic methods should be '
  136. . 'prefixed with a double underscore';
  137. $phpcsFile->addError($error, $stackPtr);
  138. }
  139. return;
  140. }
  141. // Function names can be in two parts; the package name and
  142. // the function name
  143. $packagePart = '';
  144. $camelCapsPart = '';
  145. $underscorePos = strrpos($functionName, '_');
  146. if ($underscorePos === false) {
  147. $camelCapsPart = $functionName;
  148. } else {
  149. $packagePart = substr($functionName, 0, $underscorePos);
  150. $camelCapsPart = substr($functionName, ($underscorePos + 1));
  151. // We don't care about _'s on the front
  152. $packagePart = ltrim($packagePart, '_');
  153. }
  154. // If it has a package part, make sure the first letter is a capital
  155. if ($packagePart !== '') {
  156. if ($functionName{0} === '_') {
  157. $error = 'Function name ' . $functionName . ' is invalid; only private methods should be '
  158. . 'prefixed with an underscore';
  159. $phpcsFile->addError($error, $stackPtr);
  160. return;
  161. }
  162. if ($functionName{0} !== strtoupper($functionName{0})) {
  163. $error = 'Function name ' . $functionName . ' is prefixed with a package name but does '
  164. . 'not begin with a capital letter';
  165. $phpcsFile->addError($error, $stackPtr);
  166. return;
  167. }
  168. }
  169. // If it doesn't have a camel caps part, it's not valid
  170. if (trim($camelCapsPart) === '') {
  171. $error = "Function name \"$functionName\" is not valid; name appears incomplete";
  172. $phpcsFile->addError($error, $stackPtr);
  173. return;
  174. }
  175. $validName = true;
  176. $newPackagePart = $packagePart;
  177. $newCamelCapsPart = $camelCapsPart;
  178. // Every function must have a camel caps part, so check that first
  179. if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
  180. $validName = false;
  181. $newCamelCapsPart = strtolower($camelCapsPart{0}) . substr($camelCapsPart, 1);
  182. }
  183. if ($packagePart !== '') {
  184. // Check that each new word starts with a capital
  185. $nameBits = explode('_', $packagePart);
  186. foreach ($nameBits as $bit) {
  187. if ($bit{0} !== strtoupper($bit{0})) {
  188. $newPackagePart = '';
  189. foreach ($nameBits as $bit) {
  190. $newPackagePart .= strtoupper($bit{0}) . substr($bit, 1) . '_';
  191. }
  192. $validName = false;
  193. break;
  194. }
  195. }
  196. }
  197. if ($validName === false) {
  198. $newName = rtrim($newPackagePart, '_') . '_' . $newCamelCapsPart;
  199. if ($newPackagePart === '') {
  200. $newName = $newCamelCapsPart;
  201. } else {
  202. $newName = rtrim($newPackagePart, '_') . '_' . $newCamelCapsPart;
  203. }
  204. $error = "Function name \"$functionName\" is invalid; consider \"$newName\" instead";
  205. $phpcsFile->addError($error, $stackPtr);
  206. }
  207. }
  208. }