/plugins/org.phpsrc.eclipse.pti.library.pear_1.2.2.R20120127000000/php/library/PEAR/PHP/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

https://bitbucket.org/4NN3CK/myeclipse4php · PHP · 224 lines · 115 code · 32 blank · 77 comment · 25 complexity · 170d4371e30d19b36bf4d104e5d761c0 MD5 · raw file

  1. <?php
  2. /**
  3. * PEAR_Sniffs_Functions_FunctionDeclarationSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * PEAR_Sniffs_Functions_FunctionDeclarationSniff.
  16. *
  17. * Ensure single and multi-line function declarations are defined correctly.
  18. *
  19. * @category PHP
  20. * @package PHP_CodeSniffer
  21. * @author Greg Sherwood <gsherwood@squiz.net>
  22. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  23. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  24. * @version Release: 1.3.2
  25. * @link http://pear.php.net/package/PHP_CodeSniffer
  26. */
  27. class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff
  28. {
  29. /**
  30. * Returns an array of tokens this test wants to listen for.
  31. *
  32. * @return array
  33. */
  34. public function register()
  35. {
  36. return array(T_FUNCTION);
  37. }//end register()
  38. /**
  39. * Processes this test, when one of its tokens is encountered.
  40. *
  41. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  42. * @param int $stackPtr The position of the current token
  43. * in the stack passed in $tokens.
  44. *
  45. * @return void
  46. */
  47. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  48. {
  49. $tokens = $phpcsFile->getTokens();
  50. // Check if this is a single line or multi-line declaration.
  51. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  52. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  53. if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
  54. $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
  55. } else {
  56. $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
  57. }
  58. }//end process()
  59. /**
  60. * Processes single-line declarations.
  61. *
  62. * Just uses the Generic BSD-Allman brace sniff.
  63. *
  64. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  65. * @param int $stackPtr The position of the current token
  66. * in the stack passed in $tokens.
  67. * @param array $tokens The stack of tokens that make up
  68. * the file.
  69. *
  70. * @return void
  71. */
  72. public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
  73. {
  74. if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) {
  75. throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found');
  76. }
  77. $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff();
  78. $sniff->process($phpcsFile, $stackPtr);
  79. }//end processSingleLineDeclaration()
  80. /**
  81. * Processes mutli-line declarations.
  82. *
  83. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  84. * @param int $stackPtr The position of the current token
  85. * in the stack passed in $tokens.
  86. * @param array $tokens The stack of tokens that make up
  87. * the file.
  88. *
  89. * @return void
  90. */
  91. public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
  92. {
  93. // We need to work out how far indented the function
  94. // declaration itself is, so we can work out how far to
  95. // indent parameters.
  96. $functionIndent = 0;
  97. for ($i = ($stackPtr - 1); $i >= 0; $i--) {
  98. if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
  99. $i++;
  100. break;
  101. }
  102. }
  103. if ($tokens[$i]['code'] === T_WHITESPACE) {
  104. $functionIndent = strlen($tokens[$i]['content']);
  105. }
  106. // Each line between the parenthesis should be indented 4 spaces.
  107. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  108. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  109. $lastLine = $tokens[$openBracket]['line'];
  110. for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
  111. if ($tokens[$i]['line'] !== $lastLine) {
  112. if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
  113. // Closing brace needs to be indented to the same level
  114. // as the function.
  115. $expectedIndent = $functionIndent;
  116. } else {
  117. $expectedIndent = ($functionIndent + 4);
  118. }
  119. // We changed lines, so this should be a whitespace indent token.
  120. if ($tokens[$i]['code'] !== T_WHITESPACE) {
  121. $foundIndent = 0;
  122. } else {
  123. $foundIndent = strlen($tokens[$i]['content']);
  124. }
  125. if ($expectedIndent !== $foundIndent) {
  126. $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s';
  127. $data = array(
  128. $expectedIndent,
  129. $foundIndent,
  130. );
  131. $phpcsFile->addError($error, $i, 'Indent', $data);
  132. }
  133. $lastLine = $tokens[$i]['line'];
  134. }//end if
  135. if ($tokens[$i]['code'] === T_ARRAY) {
  136. // Skip arrays as they have their own indentation rules.
  137. $i = $tokens[$i]['parenthesis_closer'];
  138. $lastLine = $tokens[$i]['line'];
  139. continue;
  140. }
  141. }//end for
  142. if (isset($tokens[$stackPtr]['scope_opener']) === true) {
  143. // The openning brace needs to be one space away
  144. // from the closing parenthesis.
  145. $next = $tokens[($closeBracket + 1)];
  146. if ($next['code'] !== T_WHITESPACE) {
  147. $length = 0;
  148. } else if ($next['content'] === $phpcsFile->eolChar) {
  149. $length = -1;
  150. } else {
  151. $length = strlen($next['content']);
  152. }
  153. if ($length !== 1) {
  154. $data = array($length);
  155. $code = 'SpaceBeforeOpenBrace';
  156. $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found ';
  157. if ($length === -1) {
  158. $error .= 'newline';
  159. $code = 'NewlineBeforeOpenBrace';
  160. } else {
  161. $error .= '%s spaces';
  162. }
  163. $phpcsFile->addError($error, ($closeBracket + 1), $code, $data);
  164. return;
  165. }
  166. // And just in case they do something funny before the brace...
  167. $next = $phpcsFile->findNext(
  168. T_WHITESPACE,
  169. ($closeBracket + 1),
  170. null,
  171. true
  172. );
  173. if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
  174. $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration';
  175. $phpcsFile->addError($error, $next, 'NoSpaceBeforeOpenBrace');
  176. }
  177. }//end if
  178. // The closing parenthesis must be on a new line, even
  179. // when checking abstract function definitions.
  180. $prev = $phpcsFile->findPrevious(
  181. T_WHITESPACE,
  182. ($closeBracket - 1),
  183. null,
  184. true
  185. );
  186. if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
  187. $error = 'The closing parenthesis of a multi-line function declaration must be on a new line';
  188. $phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
  189. }
  190. }//end processMultiLineDeclaration()
  191. }//end class
  192. ?>