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

Source for file DisallowObjectStringIndexSniff.php

Documentation is available at DisallowObjectStringIndexSniff.php

  1. <?php
  2. /**
  3.  * Ensures that object indexes are written in dot notation.
  4.  *
  5.  * @author    Sertan Danis <sdanis@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\Objects;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DisallowObjectStringIndexSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('JS');
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(T_OPEN_SQUARE_BRACKET);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  42.      * @param int                         $stackPtr  The position of the current token
  43.      *                                                in the stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile, $stackPtr)
  48.     {
  49.         $tokens = $phpcsFile->getTokens();
  50.  
  51.         // Check if the next non whitespace token is a string.
  52.         $index = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  53.         if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
  54.             return;
  55.         }
  56.  
  57.         // Make sure it is the only thing in the square brackets.
  58.         $next = $phpcsFile->findNext(T_WHITESPACE, ($index + 1), null, true);
  59.         if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
  60.             return;
  61.         }
  62.  
  63.         // Allow indexes that have dots in them because we can't write
  64.         // them in dot notation.
  65.         $content = trim($tokens[$index]['content'], '"\' ');
  66.         if (strpos($content, '.') !== false) {
  67.             return;
  68.         }
  69.  
  70.         // Also ignore reserved words.
  71.         if ($content === 'super') {
  72.             return;
  73.         }
  74.  
  75.         // Token before the opening square bracket cannot be a var name.
  76.         $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  77.         if ($tokens[$prev]['code'] === T_STRING) {
  78.             $error = 'Object indexes must be written in dot notation';
  79.             $phpcsFile->addError($error, $prev, 'Found');
  80.         }
  81.  
  82.     }//end process()
  83.  
  84.  
  85. }//end class

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