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

Source for file DisallowShortArraySyntaxSniff.php

Documentation is available at DisallowShortArraySyntaxSniff.php

  1. <?php
  2. /**
  3.  * Bans the use of the PHP short array syntax.
  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\Arrays;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DisallowShortArraySyntaxSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Registers the tokens that this sniff wants to listen for.
  21.      *
  22.      * @return int[] 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(T_OPEN_SHORT_ARRAY);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token
  36.      *                                                in the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $phpcsFile->recordMetric($stackPtr'Short array syntax used''yes');
  43.  
  44.         $error 'Short array syntax is not allowed';
  45.         $fix   $phpcsFile->addFixableError($error$stackPtr'Found');
  46.  
  47.         if ($fix === true{
  48.             $tokens $phpcsFile->getTokens();
  49.             $opener $tokens[$stackPtr]['bracket_opener'];
  50.             $closer $tokens[$stackPtr]['bracket_closer'];
  51.  
  52.             $phpcsFile->fixer->beginChangeset();
  53.             $phpcsFile->fixer->replaceToken($opener'array(');
  54.             $phpcsFile->fixer->replaceToken($closer')');
  55.             $phpcsFile->fixer->endChangeset();
  56.         }
  57.  
  58.     }//end process()
  59.  
  60.  
  61. }//end class

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