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

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