/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php

https://github.com/squizlabs/PHP_CodeSniffer · PHP · 227 lines · 143 code · 42 blank · 42 comment · 35 complexity · 0bc92fd0ccc2f943cd180ec160196082 MD5 · raw file

  1. <?php
  2. /**
  3. * Checks that the opening brace of a function is on the line after the function declaration.
  4. *
  5. * @author Greg Sherwood <gsherwood@squiz.net>
  6. * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8. */
  9. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Functions;
  10. use PHP_CodeSniffer\Files\File;
  11. use PHP_CodeSniffer\Sniffs\Sniff;
  12. use PHP_CodeSniffer\Util\Tokens;
  13. class OpeningFunctionBraceBsdAllmanSniff implements Sniff
  14. {
  15. /**
  16. * Should this sniff check function braces?
  17. *
  18. * @var boolean
  19. */
  20. public $checkFunctions = true;
  21. /**
  22. * Should this sniff check closure braces?
  23. *
  24. * @var boolean
  25. */
  26. public $checkClosures = false;
  27. /**
  28. * Registers the tokens that this sniff wants to listen for.
  29. *
  30. * @return int[]
  31. */
  32. public function register()
  33. {
  34. return [
  35. T_FUNCTION,
  36. T_CLOSURE,
  37. ];
  38. }//end register()
  39. /**
  40. * Processes this test, when one of its tokens is encountered.
  41. *
  42. * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  43. * @param int $stackPtr The position of the current token in the
  44. * stack passed in $tokens.
  45. *
  46. * @return void
  47. */
  48. public function process(File $phpcsFile, $stackPtr)
  49. {
  50. $tokens = $phpcsFile->getTokens();
  51. if (isset($tokens[$stackPtr]['scope_opener']) === false) {
  52. return;
  53. }
  54. if (($tokens[$stackPtr]['code'] === T_FUNCTION
  55. && (bool) $this->checkFunctions === false)
  56. || ($tokens[$stackPtr]['code'] === T_CLOSURE
  57. && (bool) $this->checkClosures === false)
  58. ) {
  59. return;
  60. }
  61. $openingBrace = $tokens[$stackPtr]['scope_opener'];
  62. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  63. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  64. $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
  65. if ($use !== false) {
  66. $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
  67. $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
  68. }
  69. }
  70. // Find the end of the function declaration.
  71. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true);
  72. $functionLine = $tokens[$prev]['line'];
  73. $braceLine = $tokens[$openingBrace]['line'];
  74. $lineDifference = ($braceLine - $functionLine);
  75. $metricType = 'Function';
  76. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  77. $metricType = 'Closure';
  78. }
  79. if ($lineDifference === 0) {
  80. $error = 'Opening brace should be on a new line';
  81. $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine');
  82. if ($fix === true) {
  83. $hasTrailingAnnotation = false;
  84. for ($nextLine = ($openingBrace + 1); $nextLine < $phpcsFile->numTokens; $nextLine++) {
  85. if ($tokens[$openingBrace]['line'] !== $tokens[$nextLine]['line']) {
  86. break;
  87. }
  88. if (isset(Tokens::$phpcsCommentTokens[$tokens[$nextLine]['code']]) === true) {
  89. $hasTrailingAnnotation = true;
  90. }
  91. }
  92. $phpcsFile->fixer->beginChangeset();
  93. $indent = $phpcsFile->findFirstOnLine([], $openingBrace);
  94. if ($hasTrailingAnnotation === false || $nextLine === false) {
  95. if ($tokens[$indent]['code'] === T_WHITESPACE) {
  96. $phpcsFile->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']);
  97. }
  98. if ($tokens[($openingBrace - 1)]['code'] === T_WHITESPACE) {
  99. $phpcsFile->fixer->replaceToken(($openingBrace - 1), '');
  100. }
  101. $phpcsFile->fixer->addNewlineBefore($openingBrace);
  102. } else {
  103. $phpcsFile->fixer->replaceToken($openingBrace, '');
  104. $phpcsFile->fixer->addNewlineBefore($nextLine);
  105. $phpcsFile->fixer->addContentBefore($nextLine, '{');
  106. if ($tokens[$indent]['code'] === T_WHITESPACE) {
  107. $phpcsFile->fixer->addContentBefore($nextLine, $tokens[$indent]['content']);
  108. }
  109. }
  110. $phpcsFile->fixer->endChangeset();
  111. }//end if
  112. $phpcsFile->recordMetric($stackPtr, "$metricType opening brace placement", 'same line');
  113. } else if ($lineDifference > 1) {
  114. $error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
  115. $data = [($lineDifference - 1)];
  116. $prevNonWs = $phpcsFile->findPrevious(T_WHITESPACE, ($openingBrace - 1), $closeBracket, true);
  117. if ($prevNonWs !== $prev) {
  118. // There must be a comment between the end of the function declaration and the open brace.
  119. // Report, but don't fix.
  120. $phpcsFile->addError($error, $openingBrace, 'BraceSpacing', $data);
  121. } else {
  122. $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceSpacing', $data);
  123. if ($fix === true) {
  124. $phpcsFile->fixer->beginChangeset();
  125. for ($i = $openingBrace; $i > $prev; $i--) {
  126. if ($tokens[$i]['line'] === $tokens[$openingBrace]['line']) {
  127. if ($tokens[$i]['column'] === 1) {
  128. $phpcsFile->fixer->addNewLineBefore($i);
  129. }
  130. continue;
  131. }
  132. $phpcsFile->fixer->replaceToken($i, '');
  133. }
  134. $phpcsFile->fixer->endChangeset();
  135. }
  136. }//end if
  137. }//end if
  138. $ignore = Tokens::$phpcsCommentTokens;
  139. $ignore[] = T_WHITESPACE;
  140. $next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true);
  141. if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
  142. if ($next === $tokens[$stackPtr]['scope_closer']) {
  143. // Ignore empty functions.
  144. return;
  145. }
  146. $error = 'Opening brace must be the last content on the line';
  147. $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
  148. if ($fix === true) {
  149. $phpcsFile->fixer->addNewline($openingBrace);
  150. }
  151. }
  152. // Only continue checking if the opening brace looks good.
  153. if ($lineDifference !== 1) {
  154. return;
  155. }
  156. // We need to actually find the first piece of content on this line,
  157. // as if this is a method with tokens before it (public, static etc)
  158. // or an if with an else before it, then we need to start the scope
  159. // checking from there, rather than the current token.
  160. $lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
  161. // The opening brace is on the correct line, now it needs to be
  162. // checked to be correctly indented.
  163. $startColumn = $tokens[$lineStart]['column'];
  164. $braceIndent = $tokens[$openingBrace]['column'];
  165. if ($braceIndent !== $startColumn) {
  166. $expected = ($startColumn - 1);
  167. $found = ($braceIndent - 1);
  168. $error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
  169. $data = [
  170. $expected,
  171. $found,
  172. ];
  173. $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceIndent', $data);
  174. if ($fix === true) {
  175. $indent = str_repeat(' ', $expected);
  176. if ($found === 0) {
  177. $phpcsFile->fixer->addContentBefore($openingBrace, $indent);
  178. } else {
  179. $phpcsFile->fixer->replaceToken(($openingBrace - 1), $indent);
  180. }
  181. }
  182. }//end if
  183. $phpcsFile->recordMetric($stackPtr, "$metricType opening brace placement", 'new line');
  184. }//end process()
  185. }//end class