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

Source for file EchoedStringsSniff.php

Documentation is available at EchoedStringsSniff.php

  1. <?php
  2. /**
  3.  * Makes sure that any strings that are "echoed" are not enclosed in 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\Strings;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class EchoedStringsSniff 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(T_ECHO);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token in the
  37.      *                                                stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         $firstContent $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  46.         // If the first non-whitespace token is not an opening parenthesis, then we are not concerned.
  47.         if ($tokens[$firstContent]['code'!== T_OPEN_PARENTHESIS{
  48.             $phpcsFile->recordMetric($stackPtr'Brackets around echoed strings''no');
  49.             return;
  50.         }
  51.  
  52.         $end $phpcsFile->findNext(array(T_SEMICOLONT_CLOSE_TAG)$stackPtrnullfalse);
  53.  
  54.         // If the token before the semi-colon is not a closing parenthesis, then we are not concerned.
  55.         $prev $phpcsFile->findPrevious(T_WHITESPACE($end - 1)nulltrue);
  56.         if ($tokens[$prev]['code'!== T_CLOSE_PARENTHESIS{
  57.             $phpcsFile->recordMetric($stackPtr'Brackets around echoed strings''no');
  58.             return;
  59.         }
  60.  
  61.         // If the parenthesis don't match, then we are not concerned.
  62.         if ($tokens[$firstContent]['parenthesis_closer'!== $prev{
  63.             $phpcsFile->recordMetric($stackPtr'Brackets around echoed strings''no');
  64.             return;
  65.         }
  66.  
  67.         $phpcsFile->recordMetric($stackPtr'Brackets around echoed strings''yes');
  68.  
  69.         if (($phpcsFile->findNext(Tokens::$operators$stackPtr$endfalse)) === false{
  70.             // There are no arithmetic operators in this.
  71.             $error 'Echoed strings should not be bracketed';
  72.             $fix   $phpcsFile->addFixableError($error$stackPtr'HasBracket');
  73.             if ($fix === true{
  74.                 $phpcsFile->fixer->beginChangeset();
  75.                 $phpcsFile->fixer->replaceToken($firstContent'');
  76.                 if ($tokens[($firstContent - 1)]['code'!== T_WHITESPACE{
  77.                     $phpcsFile->fixer->addContent(($firstContent - 1)' ');
  78.                 }
  79.  
  80.                 $phpcsFile->fixer->replaceToken($prev'');
  81.                 $phpcsFile->fixer->endChangeset();
  82.             }
  83.         }
  84.  
  85.     }//end process()
  86.  
  87.  
  88. }//end class

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