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\PEAR\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.      * The number of spaces code should be indented.
  21.      *
  22.      * @var integer 
  23.      */
  24.     public $indent = 4;
  25.  
  26.  
  27.      /**
  28.       * Returns an array of tokens this test wants to listen for.
  29.       *
  30.       * @return int[] 
  31.       */
  32.     public function register()
  33.     {
  34.         return Tokens::$scopeOpeners;
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes this test, when one of its tokens is encountered.
  41.      *
  42.      * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
  43.      * @param int                         $stackPtr  The position of the current token
  44.      *                                                in the stack passed in $tokens.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $tokens $phpcsFile->getTokens();
  51.  
  52.         // If this is an inline condition (ie. there is no scope opener), then
  53.         // return, as this is not a new scope.
  54.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  55.             return;
  56.         }
  57.  
  58.         $scopeStart $tokens[$stackPtr]['scope_opener'];
  59.         $scopeEnd   $tokens[$stackPtr]['scope_closer'];
  60.  
  61.         // If the scope closer doesn't think it belongs to this scope opener
  62.         // then the opener is sharing its closer with other tokens. We only
  63.         // want to process the closer once, so skip this one.
  64.         if (isset($tokens[$scopeEnd]['scope_condition']=== false
  65.             || $tokens[$scopeEnd]['scope_condition'!== $stackPtr
  66.         {
  67.             return;
  68.         }
  69.  
  70.         // We need to actually find the first piece of content on this line,
  71.         // because if this is a method with tokens before it (public, static etc)
  72.         // or an if with an else before it, then we need to start the scope
  73.         // checking from there, rather than the current token.
  74.         $lineStart ($stackPtr - 1);
  75.         for ($lineStart$lineStart > 0; $lineStart--{
  76.             if (strpos($tokens[$lineStart]['content']$phpcsFile->eolChar!== false{
  77.                 break;
  78.             }
  79.         }
  80.  
  81.         $lineStart++;
  82.  
  83.         $startColumn = 1;
  84.         if ($tokens[$lineStart]['code'=== T_WHITESPACE{
  85.             $startColumn $tokens[($lineStart + 1)]['column'];
  86.         else if ($tokens[$lineStart]['code'=== T_INLINE_HTML{
  87.             $trimmed ltrim($tokens[$lineStart]['content']);
  88.             if ($trimmed === ''{
  89.                 $startColumn $tokens[($lineStart + 1)]['column'];
  90.             else {
  91.                 $startColumn (strlen($tokens[$lineStart]['content']strlen($trimmed));
  92.             }
  93.         }
  94.  
  95.         // Check that the closing brace is on it's own line.
  96.         $lastContent $phpcsFile->findPrevious(
  97.             array(
  98.              T_WHITESPACE,
  99.              T_INLINE_HTML,
  100.              T_OPEN_TAG,
  101.             ),
  102.             ($scopeEnd - 1),
  103.             $scopeStart,
  104.             true
  105.         );
  106.  
  107.         if ($tokens[$lastContent]['line'=== $tokens[$scopeEnd]['line']{
  108.             $error 'Closing brace must be on a line by itself';
  109.             $fix   $phpcsFile->addFixableError($error$scopeEnd'Line');
  110.             if ($fix === true{
  111.                 $phpcsFile->fixer->addNewlineBefore($scopeEnd);
  112.             }
  113.  
  114.             return;
  115.         }
  116.  
  117.         // Check now that the closing brace is lined up correctly.
  118.         $lineStart ($scopeEnd - 1);
  119.         for ($lineStart$lineStart > 0; $lineStart--{
  120.             if (strpos($tokens[$lineStart]['content']$phpcsFile->eolChar!== false{
  121.                 break;
  122.             }
  123.         }
  124.  
  125.         $lineStart++;
  126.  
  127.         $braceIndent = 0;
  128.         if ($tokens[$lineStart]['code'=== T_WHITESPACE{
  129.             $braceIndent ($tokens[($lineStart + 1)]['column'- 1);
  130.         else if ($tokens[$lineStart]['code'=== T_INLINE_HTML{
  131.             $trimmed ltrim($tokens[$lineStart]['content']);
  132.             if ($trimmed === ''{
  133.                 $braceIndent ($tokens[($lineStart + 1)]['column'- 1);
  134.             else {
  135.                 $braceIndent (strlen($tokens[$lineStart]['content']strlen($trimmed- 1);
  136.             }
  137.         }
  138.  
  139.         $fix = false;
  140.         if ($tokens[$stackPtr]['code'=== T_CASE
  141.             || $tokens[$stackPtr]['code'=== T_DEFAULT
  142.         {
  143.             // BREAK statements should be indented n spaces from the
  144.             // CASE or DEFAULT statement.
  145.             $expectedIndent ($startColumn $this->indent - 1);
  146.             if ($braceIndent !== $expectedIndent{
  147.                 $error 'Case breaking statement indented incorrectly; expected %s spaces, found %s';
  148.                 $data  = array(
  149.                           $expectedIndent,
  150.                           $braceIndent,
  151.                          );
  152.                 $fix   $phpcsFile->addFixableError($error$scopeEnd'BreakIndent'$data);
  153.             }
  154.         else {
  155.             $expectedIndent max(0($startColumn - 1));
  156.             if ($braceIndent !== $expectedIndent{
  157.                 $error 'Closing brace indented incorrectly; expected %s spaces, found %s';
  158.                 $data  = array(
  159.                           $expectedIndent,
  160.                           $braceIndent,
  161.                          );
  162.                 $fix   $phpcsFile->addFixableError($error$scopeEnd'Indent'$data);
  163.             }
  164.         }//end if
  165.  
  166.         if ($fix === true{
  167.             $spaces str_repeat(' '$expectedIndent);
  168.             if ($braceIndent === 0{
  169.                 $phpcsFile->fixer->addContentBefore($lineStart$spaces);
  170.             else {
  171.                 $phpcsFile->fixer->replaceToken($lineStartltrim($tokens[$lineStart]['content']));
  172.                 $phpcsFile->fixer->addContentBefore($lineStart$spaces);
  173.             }
  174.         }
  175.  
  176.     }//end process()
  177.  
  178.  
  179. }//end class

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