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

Source for file UnnecessaryStringConcatSniff.php

Documentation is available at UnnecessaryStringConcatSniff.php

  1. <?php
  2. /**
  3.  * Checks that two strings are not concatenated together; suggests using one string instead.
  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\Strings;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class UnnecessaryStringConcatSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array(
  25.                                    'PHP',
  26.                                    'JS',
  27.                                   );
  28.  
  29.     /**
  30.      * If true, an error will be thrown; otherwise a warning.
  31.      *
  32.      * @var boolean 
  33.      */
  34.     public $error = true;
  35.  
  36.     /**
  37.      * If true, strings concatenated over multiple lines are allowed.
  38.      *
  39.      * Useful if you break strings over multiple lines to work
  40.      * within a max line length.
  41.      *
  42.      * @var boolean 
  43.      */
  44.     public $allowMultiline = false;
  45.  
  46.  
  47.     /**
  48.      * Returns an array of tokens this test wants to listen for.
  49.      *
  50.      * @return array 
  51.      */
  52.     public function register()
  53.     {
  54.         return array(
  55.                 T_STRING_CONCAT,
  56.                 T_PLUS,
  57.                );
  58.  
  59.     }//end register()
  60.  
  61.  
  62.     /**
  63.      * Processes this sniff, when one of its tokens is encountered.
  64.      *
  65.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  66.      * @param int                         $stackPtr  The position of the current token
  67.      *                                                in the stack passed in $tokens.
  68.      *
  69.      * @return void 
  70.      */
  71.     public function process(File $phpcsFile$stackPtr)
  72.     {
  73.         // Work out which type of file this is for.
  74.         $tokens $phpcsFile->getTokens();
  75.         if ($tokens[$stackPtr]['code'=== T_STRING_CONCAT{
  76.             if ($phpcsFile->tokenizerType === 'JS'{
  77.                 return;
  78.             }
  79.         else {
  80.             if ($phpcsFile->tokenizerType === 'PHP'{
  81.                 return;
  82.             }
  83.         }
  84.  
  85.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  86.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  87.         if ($prev === false || $next === false{
  88.             return;
  89.         }
  90.  
  91.         if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]=== true
  92.             && isset(Tokens::$stringTokens[$tokens[$next]['code']]=== true
  93.         {
  94.             if ($tokens[$prev]['content'][0=== $tokens[$next]['content'][0]{
  95.                 // Before we throw an error for PHP, allow strings to be
  96.                 // combined if they would have < and ? next to each other because
  97.                 // this trick is sometimes required in PHP strings.
  98.                 if ($phpcsFile->tokenizerType === 'PHP'{
  99.                     $prevChar substr($tokens[$prev]['content']-21);
  100.                     $nextChar $tokens[$next]['content'][1];
  101.                     $combined $prevChar.$nextChar;
  102.                     if ($combined === '?'.'>' || $combined === '<'.'?'{
  103.                         return;
  104.                     }
  105.                 }
  106.  
  107.                 if ($this->allowMultiline === true
  108.                     && $tokens[$prev]['line'!== $tokens[$next]['line']
  109.                 {
  110.                     return;
  111.                 }
  112.  
  113.                 $error 'String concat is not required here; use a single string instead';
  114.                 if ($this->error === true{
  115.                     $phpcsFile->addError($error$stackPtr'Found');
  116.                 else {
  117.                     $phpcsFile->addWarning($error$stackPtr'Found');
  118.                 }
  119.             }//end if
  120.         }//end if
  121.  
  122.     }//end process()
  123.  
  124.  
  125. }//end class

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