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.         // We are only interested in the first token in a multi-line string.
  48.         if ($tokens[$stackPtr]['code'=== $tokens[($stackPtr - 1)]['code']{
  49.             return;
  50.         }
  51.  
  52.         $workingString   $tokens[$stackPtr]['content'];
  53.         $lastStringToken $stackPtr;
  54.  
  55.         $i ($stackPtr + 1);
  56.         if (isset($tokens[$i]=== true{
  57.             while ($i $phpcsFile->numTokens
  58.                 && $tokens[$i]['code'=== $tokens[$stackPtr]['code']
  59.             {
  60.                 $workingString  .= $tokens[$i]['content'];
  61.                 $lastStringToken $i;
  62.                 $i++;
  63.             }
  64.         }
  65.  
  66.         // Check if it's a double quoted string.
  67.         if (strpos($workingString'"'=== false{
  68.             return;
  69.         }
  70.  
  71.         // Make sure it's not a part of a string started in a previous line.
  72.         // If it is, then we have already checked it.
  73.         if ($workingString[0!== '"'{
  74.             return;
  75.         }
  76.  
  77.         // The use of variables in double quoted strings is not allowed.
  78.         if ($tokens[$stackPtr]['code'=== T_DOUBLE_QUOTED_STRING{
  79.             $stringTokens token_get_all('<?php '.$workingString);
  80.             foreach ($stringTokens as $token{
  81.                 if (is_array($token=== true && $token[0=== T_VARIABLE{
  82.                     $error 'Variable "%s" not allowed in double quoted string; use concatenation instead';
  83.                     $data  = array($token[1]);
  84.                     $phpcsFile->addError($error$stackPtr'ContainsVar'$data);
  85.                 }
  86.             }
  87.  
  88.             return;
  89.         }//end if
  90.  
  91.         $allowedChars = array(
  92.                          '\0',
  93.                          '\1',
  94.                          '\2',
  95.                          '\3',
  96.                          '\4',
  97.                          '\5',
  98.                          '\6',
  99.                          '\7',
  100.                          '\n',
  101.                          '\r',
  102.                          '\f',
  103.                          '\t',
  104.                          '\v',
  105.                          '\x',
  106.                          '\b',
  107.                          '\e',
  108.                          '\u',
  109.                          '\'',
  110.                         );
  111.  
  112.         foreach ($allowedChars as $testChar{
  113.             if (strpos($workingString$testChar!== false{
  114.                 return;
  115.             }
  116.         }
  117.  
  118.         $error 'String %s does not require double quotes; use single quotes instead';
  119.         $data  = array(str_replace(array("\r""\n")array('\r''\n')$workingString));
  120.         $fix   $phpcsFile->addFixableError($error$stackPtr'NotRequired'$data);
  121.  
  122.         if ($fix === true{
  123.             $phpcsFile->fixer->beginChangeset();
  124.             $innerContent substr($workingString1-1);
  125.             $innerContent str_replace('\"''"'$innerContent);
  126.             $phpcsFile->fixer->replaceToken($stackPtr"'$innerContent'");
  127.             while ($lastStringToken !== $stackPtr{
  128.                 $phpcsFile->fixer->replaceToken($lastStringToken'');
  129.                 $lastStringToken--;
  130.             }
  131.  
  132.             $phpcsFile->fixer->endChangeset();
  133.         }
  134.  
  135.     }//end process()
  136.  
  137.  
  138. }//end class

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