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

Source for file OpeningFunctionBraceBsdAllmanSniff.php

Documentation is available at OpeningFunctionBraceBsdAllmanSniff.php

  1. <?php
  2. /**
  3.  * Checks that the opening brace of a function is on the line after 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.  
  15. class OpeningFunctionBraceBsdAllmanSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * Should this sniff check function braces?
  20.      *
  21.      * @var boolean 
  22.      */
  23.     public $checkFunctions = true;
  24.  
  25.     /**
  26.      * Should this sniff check closure braces?
  27.      *
  28.      * @var boolean 
  29.      */
  30.     public $checkClosures = false;
  31.  
  32.  
  33.     /**
  34.      * Registers the tokens that this sniff wants to listen for.
  35.      *
  36.      * @return void 
  37.      */
  38.     public function register()
  39.     {
  40.         return array(
  41.                 T_FUNCTION,
  42.                 T_CLOSURE,
  43.                );
  44.  
  45.     }//end register()
  46.  
  47.  
  48.     /**
  49.      * Processes this test, when one of its tokens is encountered.
  50.      *
  51.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  52.      * @param int                         $stackPtr  The position of the current token in the
  53.      *                                                stack passed in $tokens.
  54.      *
  55.      * @return void 
  56.      */
  57.     public function process(File $phpcsFile$stackPtr)
  58.     {
  59.         $tokens $phpcsFile->getTokens();
  60.  
  61.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  62.             return;
  63.         }
  64.  
  65.         if (($tokens[$stackPtr]['code'=== T_FUNCTION
  66.             && (bool) $this->checkFunctions === false)
  67.             || ($tokens[$stackPtr]['code'=== T_CLOSURE
  68.             && (bool) $this->checkClosures === false)
  69.         {
  70.             return;
  71.         }
  72.  
  73.         $openingBrace $tokens[$stackPtr]['scope_opener'];
  74.         $closeBracket $tokens[$stackPtr]['parenthesis_closer'];
  75.         if ($tokens[$stackPtr]['code'=== T_CLOSURE{
  76.             $use $phpcsFile->findNext(T_USE($closeBracket + 1)$tokens[$stackPtr]['scope_opener']);
  77.             if ($use !== false{
  78.                 $openBracket  $phpcsFile->findNext(T_OPEN_PARENTHESIS($use + 1));
  79.                 $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  80.             }
  81.         }
  82.  
  83.         $functionLine $tokens[$closeBracket]['line'];
  84.         $braceLine    $tokens[$openingBrace]['line'];
  85.  
  86.         $lineDifference ($braceLine $functionLine);
  87.  
  88.         if ($lineDifference === 0{
  89.             $error 'Opening brace should be on a new line';
  90.             $fix   $phpcsFile->addFixableError($error$openingBrace'BraceOnSameLine');
  91.             if ($fix === true{
  92.                 $phpcsFile->fixer->beginChangeset();
  93.                 $indent $phpcsFile->findFirstOnLine(array()$openingBrace);
  94.                 if ($tokens[$indent]['code'=== T_WHITESPACE{
  95.                     $phpcsFile->fixer->addContentBefore($openingBrace$tokens[$indent]['content']);
  96.                 }
  97.  
  98.                 $phpcsFile->fixer->addNewlineBefore($openingBrace);
  99.                 $phpcsFile->fixer->endChangeset();
  100.             }
  101.  
  102.             $phpcsFile->recordMetric($stackPtr'Function opening brace placement''same line');
  103.         else if ($lineDifference > 1{
  104.             $error 'Opening brace should be on the line after the declaration; found %s blank line(s)';
  105.             $data  = array(($lineDifference - 1));
  106.             $fix   $phpcsFile->addFixableError($error$openingBrace'BraceSpacing'$data);
  107.             if ($fix === true{
  108.                 for ($i ($tokens[$stackPtr]['parenthesis_closer'+ 1)$i $openingBrace$i++{
  109.                     if ($tokens[$i]['line'=== $braceLine{
  110.                         $phpcsFile->fixer->addNewLineBefore($i);
  111.                         break;
  112.                     }
  113.  
  114.                     $phpcsFile->fixer->replaceToken($i'');
  115.                 }
  116.             }
  117.         }//end if
  118.  
  119.         $next $phpcsFile->findNext(T_WHITESPACE($openingBrace + 1)nulltrue);
  120.         if ($tokens[$next]['line'=== $tokens[$openingBrace]['line']{
  121.             if ($next === $tokens[$stackPtr]['scope_closer']{
  122.                 // Ignore empty functions.
  123.                 return;
  124.             }
  125.  
  126.             $error 'Opening brace must be the last content on the line';
  127.             $fix   $phpcsFile->addFixableError($error$openingBrace'ContentAfterBrace');
  128.             if ($fix === true{
  129.                 $phpcsFile->fixer->addNewline($openingBrace);
  130.             }
  131.         }
  132.  
  133.         // Only continue checking if the opening brace looks good.
  134.         if ($lineDifference !== 1{
  135.             return;
  136.         }
  137.  
  138.         // We need to actually find the first piece of content on this line,
  139.         // as if this is a method with tokens before it (public, static etc)
  140.         // or an if with an else before it, then we need to start the scope
  141.         // checking from there, rather than the current token.
  142.         $lineStart $phpcsFile->findFirstOnLine(T_WHITESPACE$stackPtrtrue);
  143.  
  144.         // The opening brace is on the correct line, now it needs to be
  145.         // checked to be correctly indented.
  146.         $startColumn $tokens[$lineStart]['column'];
  147.         $braceIndent $tokens[$openingBrace]['column'];
  148.  
  149.         if ($braceIndent !== $startColumn{
  150.             $expected ($startColumn - 1);
  151.             $found    ($braceIndent - 1);
  152.  
  153.             $error 'Opening brace indented incorrectly; expected %s spaces, found %s';
  154.             $data  = array(
  155.                       $expected,
  156.                       $found,
  157.                      );
  158.  
  159.             $fix $phpcsFile->addFixableError($error$openingBrace'BraceIndent'$data);
  160.             if ($fix === true{
  161.                 $indent str_repeat(' '$expected);
  162.                 if ($found === 0{
  163.                     $phpcsFile->fixer->addContentBefore($openingBrace$indent);
  164.                 else {
  165.                     $phpcsFile->fixer->replaceToken(($openingBrace - 1)$indent);
  166.                 }
  167.             }
  168.         }//end if
  169.  
  170.         $phpcsFile->recordMetric($stackPtr'Function opening brace placement''new line');
  171.  
  172.     }//end process()
  173.  
  174.  
  175. }//end class

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