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

Source for file ScopeClosingBraceSniff.php

Documentation is available at ScopeClosingBraceSniff.php

  1. <?php
  2. /**
  3.  * Checks that the closing braces of scopes are aligned correctly.
  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. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ScopeClosingBraceSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return Tokens::$scopeOpeners;
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
  36.      * @param int                         $stackPtr  The position of the current token in the
  37.      *                                                stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         // If this is an inline condition (ie. there is no scope opener), then
  46.         // return, as this is not a new scope.
  47.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  48.             return;
  49.         }
  50.  
  51.         // We need to actually find the first piece of content on this line,
  52.         // as if this is a method with tokens before it (public, static etc)
  53.         // or an if with an else before it, then we need to start the scope
  54.         // checking from there, rather than the current token.
  55.         $lineStart $phpcsFile->findFirstOnLine(array(T_WHITESPACET_INLINE_HTML)$stackPtrtrue);
  56.         while ($tokens[$lineStart]['code'=== T_CONSTANT_ENCAPSED_STRING
  57.             && $tokens[($lineStart - 1)]['code'=== T_CONSTANT_ENCAPSED_STRING
  58.         {
  59.             $lineStart $phpcsFile->findFirstOnLine(array(T_WHITESPACET_INLINE_HTML)($lineStart - 1)true);
  60.         }
  61.  
  62.         $startColumn $tokens[$lineStart]['column'];
  63.         $scopeStart  $tokens[$stackPtr]['scope_opener'];
  64.         $scopeEnd    $tokens[$stackPtr]['scope_closer'];
  65.  
  66.         // Check that the closing brace is on it's own line.
  67.         $lastContent $phpcsFile->findPrevious(array(T_INLINE_HTMLT_WHITESPACET_OPEN_TAG)($scopeEnd - 1)$scopeStarttrue);
  68.         if ($tokens[$lastContent]['line'=== $tokens[$scopeEnd]['line']{
  69.             $error 'Closing brace must be on a line by itself';
  70.             $fix   $phpcsFile->addFixableError($error$scopeEnd'ContentBefore');
  71.             if ($fix === true{
  72.                 $phpcsFile->fixer->addNewlineBefore($scopeEnd);
  73.             }
  74.  
  75.             return;
  76.         }
  77.  
  78.         // Check now that the closing brace is lined up correctly.
  79.         $lineStart   $phpcsFile->findFirstOnLine(array(T_WHITESPACET_INLINE_HTML)$scopeEndtrue);
  80.         $braceIndent $tokens[$lineStart]['column'];
  81.         if ($tokens[$stackPtr]['code'!== T_DEFAULT
  82.             && $tokens[$stackPtr]['code'!== T_CASE
  83.             && $braceIndent !== $startColumn
  84.         {
  85.             $error 'Closing brace indented incorrectly; expected %s spaces, found %s';
  86.             $data  = array(
  87.                       ($startColumn - 1),
  88.                       ($braceIndent - 1),
  89.                      );
  90.  
  91.             $fix $phpcsFile->addFixableError($error$scopeEnd'Indent'$data);
  92.             if ($fix === true{
  93.                 $diff ($startColumn $braceIndent);
  94.                 if ($diff > 0{
  95.                     $phpcsFile->fixer->addContentBefore($lineStartstr_repeat(' '$diff));
  96.                 else {
  97.                     $phpcsFile->fixer->substrToken(($lineStart - 1)0$diff);
  98.                 }
  99.             }
  100.         }//end if
  101.  
  102.     }//end process()
  103.  
  104.  
  105. }//end class

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