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

Source for file OpeningFunctionBraceKernighanRitchieSniff.php

Documentation is available at OpeningFunctionBraceKernighanRitchieSniff.php

  1. <?php
  2. /**
  3.  * Checks that the opening brace of a function is on the same line as 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.  
  10. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Functions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class OpeningFunctionBraceKernighanRitchieSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Should this sniff check function braces?
  22.      *
  23.      * @var boolean 
  24.      */
  25.     public $checkFunctions = true;
  26.  
  27.     /**
  28.      * Should this sniff check closure braces?
  29.      *
  30.      * @var boolean 
  31.      */
  32.     public $checkClosures = false;
  33.  
  34.  
  35.     /**
  36.      * Registers the tokens that this sniff wants to listen for.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function register()
  41.     {
  42.         return array(
  43.                 T_FUNCTION,
  44.                 T_CLOSURE,
  45.                );
  46.  
  47.     }//end register()
  48.  
  49.  
  50.     /**
  51.      * Processes this test, when one of its tokens is encountered.
  52.      *
  53.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  54.      * @param int                         $stackPtr  The position of the current token in the
  55.      *                                                stack passed in $tokens.
  56.      *
  57.      * @return void 
  58.      */
  59.     public function process(File $phpcsFile$stackPtr)
  60.     {
  61.         $tokens $phpcsFile->getTokens();
  62.  
  63.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  64.             return;
  65.         }
  66.  
  67.         if (($tokens[$stackPtr]['code'=== T_FUNCTION
  68.             && (bool) $this->checkFunctions === false)
  69.             || ($tokens[$stackPtr]['code'=== T_CLOSURE
  70.             && (bool) $this->checkClosures === false)
  71.         {
  72.             return;
  73.         }
  74.  
  75.         $openingBrace $tokens[$stackPtr]['scope_opener'];
  76.         $closeBracket $tokens[$stackPtr]['parenthesis_closer'];
  77.         if ($tokens[$stackPtr]['code'=== T_CLOSURE{
  78.             $use $phpcsFile->findNext(T_USE($closeBracket + 1)$tokens[$stackPtr]['scope_opener']);
  79.             if ($use !== false{
  80.                 $openBracket  $phpcsFile->findNext(T_OPEN_PARENTHESIS($use + 1));
  81.                 $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  82.             }
  83.         }
  84.  
  85.         $functionLine $tokens[$closeBracket]['line'];
  86.         $braceLine    $tokens[$openingBrace]['line'];
  87.  
  88.         $lineDifference ($braceLine $functionLine);
  89.  
  90.         if ($lineDifference > 0{
  91.             $phpcsFile->recordMetric($stackPtr'Function opening brace placement''new line');
  92.             $error 'Opening brace should be on the same line as the declaration';
  93.             $fix   $phpcsFile->addFixableError($error$openingBrace'BraceOnNewLine');
  94.             if ($fix === true{
  95.                 $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($openingBrace - 1)$closeBrackettrue);
  96.                 $phpcsFile->fixer->beginChangeset();
  97.                 $phpcsFile->fixer->addContent($prev' {');
  98.                 $phpcsFile->fixer->replaceToken($openingBrace'');
  99.                 if ($tokens[($openingBrace + 1)]['code'=== T_WHITESPACE
  100.                     && $tokens[($openingBrace + 2)]['line'$tokens[$openingBrace]['line']
  101.                 {
  102.                     // Brace is followed by a new line, so remove it to ensure we don't
  103.                     // leave behind a blank line at the top of the block.
  104.                     $phpcsFile->fixer->replaceToken(($openingBrace + 1)'');
  105.  
  106.                     if ($tokens[($openingBrace - 1)]['code'=== T_WHITESPACE
  107.                         && $tokens[($openingBrace - 1)]['line'=== $tokens[$openingBrace]['line']
  108.                         && $tokens[($openingBrace - 2)]['line'$tokens[$openingBrace]['line']
  109.                     {
  110.                         // Brace is preceeded by indent, so remove it to ensure we don't
  111.                         // leave behind more indent than is required for the first line.
  112.                         $phpcsFile->fixer->replaceToken(($openingBrace - 1)'');
  113.                     }
  114.                 }
  115.  
  116.                 $phpcsFile->fixer->endChangeset();
  117.             }//end if
  118.         }//end if
  119.  
  120.         $phpcsFile->recordMetric($stackPtr'Function opening brace placement''same line');
  121.  
  122.         $next $phpcsFile->findNext(T_WHITESPACE($openingBrace + 1)nulltrue);
  123.         if ($tokens[$next]['line'=== $tokens[$openingBrace]['line']{
  124.             if ($next === $tokens[$stackPtr]['scope_closer']{
  125.                 // Ignore empty functions.
  126.                 return;
  127.             }
  128.  
  129.             $error 'Opening brace must be the last content on the line';
  130.             $fix   $phpcsFile->addFixableError($error$openingBrace'ContentAfterBrace');
  131.             if ($fix === true{
  132.                 $phpcsFile->fixer->addNewline($openingBrace);
  133.             }
  134.         }
  135.  
  136.         // Only continue checking if the opening brace looks good.
  137.         if ($lineDifference > 0{
  138.             return;
  139.         }
  140.  
  141.         // We are looking for tabs, even if they have been replaced, because
  142.         // we enforce a space here.
  143.         if (isset($tokens[($openingBrace - 1)]['orig_content']=== true{
  144.             $spacing $tokens[($openingBrace - 1)]['content'];
  145.         else {
  146.             $spacing $tokens[($openingBrace - 1)]['content'];
  147.         }
  148.  
  149.         if ($tokens[($openingBrace - 1)]['code'!== T_WHITESPACE{
  150.             $length = 0;
  151.         else if ($spacing === "\t"{
  152.             $length '\t';
  153.         else {
  154.             $length strlen($spacing);
  155.         }
  156.  
  157.         if ($length !== 1{
  158.             $error 'Expected 1 space before opening brace; found %s';
  159.             $data  = array($length);
  160.             $fix   $phpcsFile->addFixableError($error$closeBracket'SpaceBeforeBrace'$data);
  161.             if ($fix === true{
  162.                 if ($length === 0 || $length === '\t'{
  163.                     $phpcsFile->fixer->addContentBefore($openingBrace' ');
  164.                 else {
  165.                     $phpcsFile->fixer->replaceToken(($openingBrace - 1)' ');
  166.                 }
  167.             }
  168.         }
  169.  
  170.     }//end process()
  171.  
  172.  
  173. }//end class

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