Share Pastebin
Guest
Public paste!

DoubleQuoteUsageSniff

By: a guest | Mar 19th, 2010 | Syntax: PHP | Size: 3.31 KB | Hits: 66 | Expires: Never
Copy text to clipboard
  1. <?php
  2. /**
  3.  * Zend_Sniffs_Strings_DoubleQuoteUsageSniff.
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category  PHP
  8.  * @package   PHP_CodeSniffer
  9.  * @author    Juan Sotuyo <juansotuyo@gmail.com>
  10.  * @copyright 2010 Juan Sotuyo (Buenos Aires, Argentina)
  11.  * @license   BSD Licence
  12.  * @version   1.0
  13.  * @link      http://pear.php.net/package/PHP_CodeSniffer
  14.  */
  15.  
  16. /**
  17.  * Zend_Sniffs_Strings_DoubleQuoteUsageSniff.
  18.  *
  19.  * Makes sure that any use of Double Quotes ("") are warranted.
  20.  *
  21.  * @category  PHP
  22.  * @package   PHP_CodeSniffer
  23.  * @author    Juan Sotuyo <juansotuyo@gmail.com>
  24.  * @copyright 2010 Juan Sotuyo (Buenos Aires, Argentina)
  25.  * @license   BSD Licence
  26.  * @version   Release: 1.2.2
  27.  * @link      http://pear.php.net/package/PHP_CodeSniffer
  28.  */
  29. class Zend_Sniffs_Strings_DoubleQuoteUsageSniff implements PHP_CodeSniffer_Sniff
  30. {
  31.  
  32.     protected static $allowedChars = array(
  33.                          '\0',
  34.                          '\n',
  35.                          '\r',
  36.                          '\f',
  37.                          '\t',
  38.                          '\v',
  39.                          '\x',
  40.                         );
  41.  
  42.     /**
  43.      * Returns an array of tokens this test wants to listen for.
  44.      *
  45.      * @return array
  46.      */
  47.     public function register()
  48.     {
  49.         return array(
  50.                 T_CONSTANT_ENCAPSED_STRING,
  51.                 T_DOUBLE_QUOTED_STRING,
  52.                );
  53.  
  54.     }//end register()
  55.  
  56.  
  57.     /**
  58.      * Processes this test, when one of its tokens is encountered.
  59.      *
  60.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  61.      * @param int                  $stackPtr  The position of the current token
  62.      *                                        in the stack passed in $tokens.
  63.      *
  64.      * @return void
  65.      */
  66.     public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  67.     {
  68.         $tokens = $phpcsFile->getTokens();
  69.         $string = $tokens[$stackPtr]['content'];
  70.  
  71.         $varFound = false;
  72.  
  73.         // The use of variables in double quoted strings is not allowed.
  74.         if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
  75.             $stringTokens = token_get_all('<?php '.$string);
  76.             foreach ($stringTokens as $token) {
  77.                 if (is_array($token) === true && $token[0] === T_VARIABLE) {
  78.                     $varFound = true;
  79.                 }
  80.             }
  81.         }
  82.  
  83.         // Check the var is not in the "${...}" format
  84.         $matches = preg_match('/\$\{[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\}/', $string);
  85.         if ($matches !== false && $matches > 0) {
  86.             $error = 'String "'.$string.'" contains a variable in the format: ${variable} which is not allowed; use no braces or {$var} instead';
  87.             $phpcsFile->addError($error, $stackPtr);
  88.         }
  89.  
  90.         if ($varFound) {
  91.             return;
  92.         }
  93.  
  94.         // Are apostrophes included in the string?
  95.         if (strstr($string, "'") !== false) {
  96.             return;
  97.         }
  98.  
  99.         foreach (self::$allowedChars as $char) {
  100.             if (strstr($string, $char) !== false) {
  101.                 return;
  102.             }
  103.         }
  104.  
  105.         $error = 'String '.$string.' dows not require double quotes; use single quotes instead.';
  106.         $phpcsFile->addError($error, $stackPtr);
  107.     }//end process()
  108.  
  109.  
  110. }//end class
  111.  
  112. ?>