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

Source for file ArrayBracketSpacingSniff.php

Documentation is available at ArrayBracketSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure that there are no spaces around square brackets.
  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\Arrays;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ArrayBracketSpacingSniff 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 array(
  28.                 T_OPEN_SQUARE_BRACKET,
  29.                 T_CLOSE_SQUARE_BRACKET,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this sniff, when one of its tokens is encountered.
  37.      *
  38.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
  39.      * @param int                         $stackPtr  The position of the current token in the
  40.      *                                                stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         if (($tokens[$stackPtr]['code'=== T_OPEN_SQUARE_BRACKET
  49.             && isset($tokens[$stackPtr]['bracket_closer']=== false)
  50.             || ($tokens[$stackPtr]['code'=== T_CLOSE_SQUARE_BRACKET
  51.             && isset($tokens[$stackPtr]['bracket_opener']=== false)
  52.         {
  53.             // Bow out for parse error/during live coding.
  54.             return;
  55.         }
  56.  
  57.         // Square brackets can not have a space before them.
  58.         $prevType $tokens[($stackPtr - 1)]['code'];
  59.         if ($prevType === T_WHITESPACE{
  60.             $nonSpace $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 2)nulltrue);
  61.             $expected $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content'];
  62.             $found    $phpcsFile->getTokensAsString($nonSpace($stackPtr $nonSpace)).$tokens[$stackPtr]['content'];
  63.             $error    'Space found before square bracket; expected "%s" but found "%s"';
  64.             $data     = array(
  65.                          $expected,
  66.                          $found,
  67.                         );
  68.             $fix      $phpcsFile->addFixableError($error$stackPtr'SpaceBeforeBracket'$data);
  69.             if ($fix === true{
  70.                 $phpcsFile->fixer->replaceToken(($stackPtr - 1)'');
  71.             }
  72.         }
  73.  
  74.         // Open square brackets can't ever have spaces after them.
  75.         if ($tokens[$stackPtr]['code'=== T_OPEN_SQUARE_BRACKET{
  76.             $nextType $tokens[($stackPtr + 1)]['code'];
  77.             if ($nextType === T_WHITESPACE{
  78.                 $nonSpace $phpcsFile->findNext(T_WHITESPACE($stackPtr + 2)nulltrue);
  79.                 $expected $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content'];
  80.                 $found    $phpcsFile->getTokensAsString($stackPtr($nonSpace $stackPtr + 1));
  81.                 $error    'Space found after square bracket; expected "%s" but found "%s"';
  82.                 $data     = array(
  83.                              $expected,
  84.                              $found,
  85.                             );
  86.                 $fix      $phpcsFile->addFixableError($error$stackPtr'SpaceAfterBracket'$data);
  87.                 if ($fix === true{
  88.                     $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  89.                 }
  90.             }
  91.         }
  92.  
  93.     }//end process()
  94.  
  95.  
  96. }//end class

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