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

Source for file FunctionClosingBraceSpaceSniff.php

Documentation is available at FunctionClosingBraceSpaceSniff.php

  1. <?php
  2. /**
  3.  * Checks that there is one empty line before the closing brace of a function.
  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\Squiz\Sniffs\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class FunctionClosingBraceSpaceSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  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(
  37.                 T_FUNCTION,
  38.                 T_CLOSURE,
  39.                );
  40.  
  41.     }//end register()
  42.  
  43.  
  44.     /**
  45.      * Processes this test, when one of its tokens is encountered.
  46.      *
  47.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  48.      * @param int                         $stackPtr  The position of the current token
  49.      *                                                in the stack passed in $tokens.
  50.      *
  51.      * @return void 
  52.      */
  53.     public function process(File $phpcsFile$stackPtr)
  54.     {
  55.         $tokens $phpcsFile->getTokens();
  56.  
  57.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  58.             // Probably an interface method.
  59.             return;
  60.         }
  61.  
  62.         $closeBrace  $tokens[$stackPtr]['scope_closer'];
  63.         $prevContent $phpcsFile->findPrevious(T_WHITESPACE($closeBrace - 1)nulltrue);
  64.  
  65.         // Special case for empty JS functions.
  66.         if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']{
  67.             // In this case, the opening and closing brace must be
  68.             // right next to each other.
  69.             if ($tokens[$stackPtr]['scope_closer'!== ($tokens[$stackPtr]['scope_opener'+ 1)) {
  70.                 $error 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
  71.                 $fix   $phpcsFile->addFixableError($error$closeBrace'SpacingBetween');
  72.                 if ($fix === true{
  73.                     $phpcsFile->fixer->beginChangeset();
  74.                     for ($i ($tokens[$stackPtr]['scope_opener'+ 1)$i $closeBrace$i++{
  75.                         $phpcsFile->fixer->replaceToken($i'');
  76.                     }
  77.  
  78.                     $phpcsFile->fixer->endChangeset();
  79.                 }
  80.             }
  81.  
  82.             return;
  83.         }
  84.  
  85.         $nestedFunction = false;
  86.         if ($phpcsFile->hasCondition($stackPtrT_FUNCTION=== true
  87.             || $phpcsFile->hasCondition($stackPtrT_CLOSURE=== true
  88.             || isset($tokens[$stackPtr]['nested_parenthesis']=== true
  89.         {
  90.             $nestedFunction = true;
  91.         }
  92.  
  93.         $braceLine $tokens[$closeBrace]['line'];
  94.         $prevLine  $tokens[$prevContent]['line'];
  95.         $found     ($braceLine $prevLine - 1);
  96.  
  97.         $afterKeyword  $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  98.         $beforeKeyword $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  99.         if ($nestedFunction === true{
  100.             if ($found < 0{
  101.                 $error 'Closing brace of nested function must be on a new line';
  102.                 $fix   $phpcsFile->addFixableError($error$closeBrace'ContentBeforeClose');
  103.                 if ($fix === true{
  104.                     $phpcsFile->fixer->addNewlineBefore($closeBrace);
  105.                 }
  106.             else if ($found > 0{
  107.                 $error 'Expected 0 blank lines before closing brace of nested function; %s found';
  108.                 $data  = array($found);
  109.                 $fix   $phpcsFile->addFixableError($error$closeBrace'SpacingBeforeNestedClose'$data);
  110.  
  111.                 if ($fix === true{
  112.                     $phpcsFile->fixer->beginChangeset();
  113.                     $changeMade = false;
  114.                     for ($i ($prevContent + 1)$i $closeBrace$i++{
  115.                         // Try and maintain indentation.
  116.                         if ($tokens[$i]['line'=== ($braceLine - 1)) {
  117.                             break;
  118.                         }
  119.  
  120.                         $phpcsFile->fixer->replaceToken($i'');
  121.                         $changeMade = true;
  122.                     }
  123.  
  124.                     // Special case for when the last content contains the newline
  125.                     // token as well, like with a comment.
  126.                     if ($changeMade === false{
  127.                         $phpcsFile->fixer->replaceToken(($prevContent + 1)'');
  128.                     }
  129.  
  130.                     $phpcsFile->fixer->endChangeset();
  131.                 }//end if
  132.             }//end if
  133.         else {
  134.             if ($found !== 1{
  135.                 if ($found < 0{
  136.                     $found = 0;
  137.                 }
  138.  
  139.                 $error 'Expected 1 blank line before closing function brace; %s found';
  140.                 $data  = array($found);
  141.                 $fix   $phpcsFile->addFixableError($error$closeBrace'SpacingBeforeClose'$data);
  142.  
  143.                 if ($fix === true{
  144.                     if ($found > 1{
  145.                         $phpcsFile->fixer->beginChangeset();
  146.                         for ($i ($prevContent + 1)$i ($closeBrace - 1)$i++{
  147.                             $phpcsFile->fixer->replaceToken($i'');
  148.                         }
  149.  
  150.                         $phpcsFile->fixer->replaceToken($i$phpcsFile->eolChar);
  151.                         $phpcsFile->fixer->endChangeset();
  152.                     else {
  153.                         // Try and maintain indentation.
  154.                         if ($tokens[($closeBrace - 1)]['code'=== T_WHITESPACE{
  155.                             $phpcsFile->fixer->addNewlineBefore($closeBrace - 1);
  156.                         else {
  157.                             $phpcsFile->fixer->addNewlineBefore($closeBrace);
  158.                         }
  159.                     }
  160.                 }
  161.             }//end if
  162.         }//end if
  163.  
  164.     }//end process()
  165.  
  166.  
  167. }//end class

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