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

Source for file StaticThisUsageSniff.php

Documentation is available at StaticThisUsageSniff.php

  1. <?php
  2. /**
  3.  * Checks for usage of $this in static methods, which will cause runtime errors.
  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\Scope;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class StaticThisUsageSniff extends AbstractScopeSniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Constructs the test with the tokens it wishes to listen for.
  21.      */
  22.     public function __construct()
  23.     {
  24.         parent::__construct(array(T_CLASS)array(T_FUNCTION));
  25.  
  26.     }//end __construct()
  27.  
  28.  
  29.     /**
  30.      * Processes this test, when one of its tokens is encountered.
  31.      *
  32.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
  33.      * @param int                         $stackPtr  The position of the current token in the
  34.      *                                                stack passed in $tokens.
  35.      * @param int                         $currScope A pointer to the start of the scope.
  36.      *
  37.      * @return void 
  38.      */
  39.     public function processTokenWithinScope(File $phpcsFile$stackPtr$currScope)
  40.     {
  41.         $tokens   $phpcsFile->getTokens();
  42.         $function $tokens[($stackPtr + 2)];
  43.  
  44.         if ($function['code'!== T_STRING{
  45.             return;
  46.         }
  47.  
  48.         $functionName $function['content'];
  49.         $classOpener  $tokens[$currScope]['scope_condition'];
  50.         $className    $tokens[($classOpener + 2)]['content'];
  51.  
  52.         $methodProps $phpcsFile->getMethodProperties($stackPtr);
  53.  
  54.         if ($methodProps['is_static'=== true{
  55.             if (isset($tokens[$stackPtr]['scope_closer']=== false{
  56.                 // There is no scope opener or closer, so the function
  57.                 // must be abstract.
  58.                 return;
  59.             }
  60.  
  61.             $thisUsage $stackPtr;
  62.             while (($thisUsage $phpcsFile->findNext(array(T_VARIABLE)($thisUsage + 1)$tokens[$stackPtr]['scope_closer']false'$this')) !== false{
  63.                 if ($thisUsage === false{
  64.                     return;
  65.                 }
  66.  
  67.                 $error 'Usage of "$this" in static methods will cause runtime errors';
  68.                 $phpcsFile->addError($error$thisUsage'Found');
  69.             }
  70.         }//end if
  71.  
  72.     }//end processTokenWithinScope()
  73.  
  74.  
  75.     /**
  76.      * Processes a token that is found within the scope that this test is
  77.      * listening to.
  78.      *
  79.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  80.      * @param int                         $stackPtr  The position in the stack where this
  81.      *                                                token was found.
  82.      *
  83.      * @return void 
  84.      */
  85.     protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  86.     {
  87.  
  88.     }//end processTokenOutsideScope()
  89.  
  90.  
  91. }//end class

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