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

Source for file FunctionOpeningBraceSpaceSniff.php

Documentation is available at FunctionOpeningBraceSpaceSniff.php

  1. <?php
  2. /**
  3.  * Checks that there is no empty line after the opening 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 FunctionOpeningBraceSpaceSniff 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_opener']=== false{
  58.             // Probably an interface method.
  59.             return;
  60.         }
  61.  
  62.         $openBrace   $tokens[$stackPtr]['scope_opener'];
  63.         $nextContent $phpcsFile->findNext(T_WHITESPACE($openBrace + 1)nulltrue);
  64.  
  65.         if ($nextContent === $tokens[$stackPtr]['scope_closer']{
  66.              // The next bit of content is the closing brace, so this
  67.              // is an empty function and should have a blank line
  68.              // between the opening and closing braces.
  69.             return;
  70.         }
  71.  
  72.         $braceLine $tokens[$openBrace]['line'];
  73.         $nextLine  $tokens[$nextContent]['line'];
  74.  
  75.         $found ($nextLine $braceLine - 1);
  76.         if ($found > 0{
  77.             $error 'Expected 0 blank lines after opening function brace; %s found';
  78.             $data  = array($found);
  79.             $fix   $phpcsFile->addFixableError($error$openBrace'SpacingAfter'$data);
  80.             if ($fix === true{
  81.                 $phpcsFile->fixer->beginChangeset();
  82.                 for ($i ($openBrace + 1)$i $nextContent$i++{
  83.                     if ($tokens[$i]['line'=== $nextLine{
  84.                         break;
  85.                     }
  86.  
  87.                     $phpcsFile->fixer->replaceToken($i'');
  88.                 }
  89.  
  90.                 $phpcsFile->fixer->addNewline($openBrace);
  91.                 $phpcsFile->fixer->endChangeset();
  92.             }
  93.         }
  94.  
  95.     }//end process()
  96.  
  97.  
  98. }//end class

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