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_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.                 $phpcsFile->fixer->endChangeset();
  100.             }
  101.         }
  102.  
  103.         $phpcsFile->recordMetric($stackPtr'Function opening brace placement''same line');
  104.  
  105.         $next $phpcsFile->findNext(T_WHITESPACE($openingBrace + 1)nulltrue);
  106.         if ($tokens[$next]['line'=== $tokens[$openingBrace]['line']{
  107.             if ($next === $tokens[$stackPtr]['scope_closer']{
  108.                 // Ignore empty functions.
  109.                 return;
  110.             }
  111.  
  112.             $error 'Opening brace must be the last content on the line';
  113.             $fix   $phpcsFile->addFixableError($error$openingBrace'ContentAfterBrace');
  114.             if ($fix === true{
  115.                 $phpcsFile->fixer->addNewline($openingBrace);
  116.             }
  117.         }
  118.  
  119.         // Only continue checking if the opening brace looks good.
  120.         if ($lineDifference > 0{
  121.             return;
  122.         }
  123.  
  124.         if ($tokens[($closeBracket + 1)]['code'!== T_WHITESPACE{
  125.             $length = 0;
  126.         else if ($tokens[($closeBracket + 1)]['content'=== "\t"{
  127.             $length '\t';
  128.         else {
  129.             $length strlen($tokens[($closeBracket + 1)]['content']);
  130.         }
  131.  
  132.         if ($length !== 1{
  133.             $error 'Expected 1 space after closing parenthesis; found %s';
  134.             $data  = array($length);
  135.             $fix   $phpcsFile->addFixableError($error$closeBracket'SpaceAfterBracket'$data);
  136.             if ($fix === true{
  137.                 if ($length === 0 || $length === '\t'{
  138.                     $phpcsFile->fixer->addContent($closeBracket' ');
  139.                 else {
  140.                     $phpcsFile->fixer->replaceToken(($closeBracket + 1)' ');
  141.                 }
  142.             }
  143.         }
  144.  
  145.     }//end process()
  146.  
  147.  
  148. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.