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

Source for file JoinStringsSniff.php

Documentation is available at JoinStringsSniff.php

  1. <?php
  2. /**
  3.  * Ensures that strings are not joined using array.join().
  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\MySource\Sniffs\Strings;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class JoinStringsSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('JS');
  25.  
  26.  
  27.     /**
  28.      * Returns an array of tokens this test wants to listen for.
  29.      *
  30.      * @return array 
  31.      */
  32.     public function register()
  33.     {
  34.         return array(T_STRING);
  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 The file being scanned.
  43.      * @param integer                     $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 ($tokens[$stackPtr]['content'!== 'join'{
  53.             return;
  54.         }
  55.  
  56.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  57.         if ($tokens[$prev]['code'!== T_OBJECT_OPERATOR{
  58.             return;
  59.         }
  60.  
  61.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($prev - 1)nulltrue);
  62.         if ($tokens[$prev]['code'=== T_CLOSE_SQUARE_BRACKET{
  63.             $opener $tokens[$prev]['bracket_opener'];
  64.             if ($tokens[($opener - 1)]['code'!== T_STRING{
  65.                 // This means the array is declared inline, like x = [a,b,c].join()
  66.                 // and not elsewhere, like x = y[a].join()
  67.                 // The first is not allowed while the second is.
  68.                 $error 'Joining strings using inline arrays is not allowed; use the + operator instead';
  69.                 $phpcsFile->addError($error$stackPtr'ArrayNotAllowed');
  70.             }
  71.         }
  72.  
  73.     }//end process()
  74.  
  75.  
  76. }//end class

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