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

Source for file AssignThisSniff.php

Documentation is available at AssignThisSniff.php

  1. <?php
  2. /**
  3.  * Ensures this is not assigned to any other var but self.
  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\MySource\Sniffs\Objects;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class AssignThisSniff 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_THIS);
  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.         // Ignore this.something and other uses of "this" that are not
  52.         // direct assignments.
  53.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  54.         if ($tokens[$next]['code'!== T_SEMICOLON{
  55.             if ($tokens[$next]['line'=== $tokens[$stackPtr]['line']{
  56.                 return;
  57.             }
  58.         }
  59.  
  60.         // Something must be assigned to "this".
  61.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  62.         if ($tokens[$prev]['code'!== T_EQUAL{
  63.             return;
  64.         }
  65.  
  66.         // A variable needs to be assigned to "this".
  67.         $prev $phpcsFile->findPrevious(T_WHITESPACE($prev - 1)nulltrue);
  68.         if ($tokens[$prev]['code'!== T_STRING{
  69.             return;
  70.         }
  71.  
  72.         // We can only assign "this" to a var called "self".
  73.         if ($tokens[$prev]['content'!== 'self' && $tokens[$prev]['content'!== '_self'{
  74.             $error 'Keyword "this" can only be assigned to a variable called "self" or "_self"';
  75.             $phpcsFile->addError($error$prev'NotSelf');
  76.         }
  77.  
  78.     }//end process()
  79.  
  80.  
  81. }//end class

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