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

Source for file DoubleQuoteUsageSniff.php

Documentation is available at DoubleQuoteUsageSniff.php

  1. <?php
  2. /**
  3.  * Makes sure that any use of double quotes strings are warranted.
  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.  
  15. class DoubleQuoteUsageSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_CONSTANT_ENCAPSED_STRING,
  28.                 T_DOUBLE_QUOTED_STRING,
  29.                );
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this test, when one of its tokens is encountered.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  38.      * @param int                         $stackPtr  The position of the current token
  39.      *                                                in the stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         // If tabs are being converted to spaces by the tokeniser, the
  48.         // original content should be used instead of the converted content.
  49.         if (isset($tokens[$stackPtr]['orig_content']=== true{
  50.             $workingString $tokens[$stackPtr]['orig_content'];
  51.         else {
  52.             $workingString $tokens[$stackPtr]['content'];
  53.         }
  54.  
  55.         $lastStringToken $stackPtr;
  56.  
  57.         $i ($stackPtr + 1);
  58.         if (isset($tokens[$i]=== true{
  59.             while ($i $phpcsFile->numTokens
  60.                 && $tokens[$i]['code'=== $tokens[$stackPtr]['code']
  61.             {
  62.                 if (isset($tokens[$i]['orig_content']=== true{
  63.                     $workingString .= $tokens[$i]['orig_content'];
  64.                 else {
  65.                     $workingString .= $tokens[$i]['content'];
  66.                 }
  67.  
  68.                 $lastStringToken $i;
  69.                 $i++;
  70.             }
  71.         }
  72.  
  73.         $skipTo ($lastStringToken + 1);
  74.  
  75.         // Check if it's a double quoted string.
  76.         if ($workingString[0!== '"' || substr($workingString-1!== '"'{
  77.             return $skipTo;
  78.         }
  79.  
  80.         // The use of variables in double quoted strings is not allowed.
  81.         if ($tokens[$stackPtr]['code'=== T_DOUBLE_QUOTED_STRING{
  82.             $stringTokens token_get_all('<?php '.$workingString);
  83.             foreach ($stringTokens as $token{
  84.                 if (is_array($token=== true && $token[0=== T_VARIABLE{
  85.                     $error 'Variable "%s" not allowed in double quoted string; use concatenation instead';
  86.                     $data  = array($token[1]);
  87.                     $phpcsFile->addError($error$stackPtr'ContainsVar'$data);
  88.                 }
  89.             }
  90.  
  91.             return $skipTo;
  92.         }//end if
  93.  
  94.         $allowedChars = array(
  95.                          '\0',
  96.                          '\1',
  97.                          '\2',
  98.                          '\3',
  99.                          '\4',
  100.                          '\5',
  101.                          '\6',
  102.                          '\7',
  103.                          '\n',
  104.                          '\r',
  105.                          '\f',
  106.                          '\t',
  107.                          '\v',
  108.                          '\x',
  109.                          '\b',
  110.                          '\e',
  111.                          '\u',
  112.                          '\'',
  113.                         );
  114.  
  115.         foreach ($allowedChars as $testChar{
  116.             if (strpos($workingString$testChar!== false{
  117.                 return $skipTo;
  118.             }
  119.         }
  120.  
  121.         $error 'String %s does not require double quotes; use single quotes instead';
  122.         $data  = array(str_replace(array("\r""\n")array('\r''\n')$workingString));
  123.         $fix   $phpcsFile->addFixableError($error$stackPtr'NotRequired'$data);
  124.  
  125.         if ($fix === true{
  126.             $phpcsFile->fixer->beginChangeset();
  127.             $innerContent substr($workingString1-1);
  128.             $innerContent str_replace('\"''"'$innerContent);
  129.             $innerContent str_replace('\\$''$'$innerContent);
  130.             $phpcsFile->fixer->replaceToken($stackPtr"'$innerContent'");
  131.             while ($lastStringToken !== $stackPtr{
  132.                 $phpcsFile->fixer->replaceToken($lastStringToken'');
  133.                 $lastStringToken--;
  134.             }
  135.  
  136.             $phpcsFile->fixer->endChangeset();
  137.         }
  138.  
  139.         return $skipTo;
  140.  
  141.     }//end process()
  142.  
  143.  
  144. }//end class

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