PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file FunctionClosingBraceSniff.php

Documentation is available at FunctionClosingBraceSniff.php

  1. <?php
  2. /**
  3.  * Checks that the closing brace of a function goes directly after the body.
  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.  
  10. namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class FunctionClosingBraceSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_FUNCTION,
  28.                 T_CLOSURE,
  29.                );
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this sniff, when one of its tokens is encountered.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  38.      * @param int                         $stackPtr  The position of the current token in
  39.      *                                                the stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  48.             // Probably an interface method.
  49.             return;
  50.         }
  51.  
  52.         $closeBrace  $tokens[$stackPtr]['scope_closer'];
  53.         $prevContent $phpcsFile->findPrevious(T_WHITESPACE($closeBrace - 1)nulltrue);
  54.         $found       ($tokens[$closeBrace]['line'$tokens[$prevContent]['line'- 1);
  55.  
  56.         if ($found < 0{
  57.             // Brace isn't on a new line, so not handled by us.
  58.             return;
  59.         }
  60.  
  61.         if ($found === 0{
  62.             // All is good.
  63.             return;
  64.         }
  65.  
  66.         $error 'Function closing brace must go on the next line following the body; found %s blank lines before brace';
  67.         $data  = array($found);
  68.         $fix   $phpcsFile->addFixableError($error$closeBrace'SpacingBeforeClose'$data);
  69.  
  70.         if ($fix === true{
  71.             $phpcsFile->fixer->beginChangeset();
  72.             for ($i ($prevContent + 1)$i $closeBrace$i++{
  73.                 if ($tokens[$i]['line'=== $tokens[$prevContent]['line']{
  74.                     continue;
  75.                 }
  76.  
  77.                 // Don't remove any identation before the brace.
  78.                 if ($tokens[$i]['line'=== $tokens[$closeBrace]['line']{
  79.                     break;
  80.                 }
  81.  
  82.                 $phpcsFile->fixer->replaceToken($i'');
  83.             }
  84.  
  85.             $phpcsFile->fixer->endChangeset();
  86.         }
  87.  
  88.     }//end process()
  89.  
  90.  
  91. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.