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

Source for file AbstractVariableSniff.php

Documentation is available at AbstractVariableSniff.php

  1. <?php
  2. /**
  3.  * A class to find T_VARIABLE tokens.
  4.  *
  5.  * This class can distinguish between normal T_VARIABLE tokens, and those tokens
  6.  * that represent class members. If a class member is encountered, then the
  7.  * processMemberVar method is called so the extending class can process it. If
  8.  * the token is found to be a normal T_VARIABLE token, then processVariable is
  9.  * called.
  10.  *
  11.  * @author    Greg Sherwood <gsherwood@squiz.net>
  12.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  13.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14.  */
  15.  
  16. namespace PHP_CodeSniffer\Sniffs;
  17.  
  18. use PHP_CodeSniffer\Files\File;
  19. use PHP_CodeSniffer\Util\Tokens;
  20. use PHP_CodeSniffer\Exceptions\RuntimeException;
  21.  
  22. abstract class AbstractVariableSniff extends AbstractScopeSniff
  23. {
  24.  
  25.     /**
  26.      * The end token of the current function that we are in.
  27.      *
  28.      * @var integer 
  29.      */
  30.     private $endFunction = -1;
  31.  
  32.     /**
  33.      * TRUE if a function is currently open.
  34.      *
  35.      * @var boolean 
  36.      */
  37.     private $functionOpen = false;
  38.  
  39.     /**
  40.      * The current PHP_CodeSniffer file that we are processing.
  41.      *
  42.      * @var \PHP_CodeSniffer\Files\File 
  43.      */
  44.     protected $currentFile = null;
  45.  
  46.  
  47.     /**
  48.      * Constructs an AbstractVariableTest.
  49.      */
  50.     public function __construct()
  51.     {
  52.         $scopes = Tokens::$ooScopeTokens;
  53.  
  54.         $listen = array(
  55.                    T_FUNCTION,
  56.                    T_VARIABLE,
  57.                    T_DOUBLE_QUOTED_STRING,
  58.                    T_HEREDOC,
  59.                   );
  60.  
  61.         parent::__construct($scopes$listentrue);
  62.  
  63.     }//end __construct()
  64.  
  65.  
  66.     /**
  67.      * Processes the token in the specified PHP_CodeSniffer_File.
  68.      *
  69.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
  70.      *                                                token was found.
  71.      * @param int                         $stackPtr  The position where the token was found.
  72.      * @param int                         $currScope The current scope opener token.
  73.      *
  74.      * @return void 
  75.      */
  76.     final protected function processTokenWithinScope(File $phpcsFile$stackPtr$currScope)
  77.     {
  78.         if ($this->currentFile !== $phpcsFile{
  79.             $this->currentFile  $phpcsFile;
  80.             $this->functionOpen = false;
  81.             $this->endFunction  = -1;
  82.         }
  83.  
  84.         $tokens $phpcsFile->getTokens();
  85.  
  86.         if ($stackPtr $this->endFunction{
  87.             $this->functionOpen = false;
  88.         }
  89.  
  90.         if ($tokens[$stackPtr]['code'=== T_FUNCTION
  91.             && $this->functionOpen === false
  92.         {
  93.             $this->functionOpen = true;
  94.  
  95.             $methodProps $phpcsFile->getMethodProperties($stackPtr);
  96.  
  97.             // If the function is abstract, or is in an interface,
  98.             // then set the end of the function to it's closing semicolon.
  99.             if ($methodProps['is_abstract'=== true
  100.                 || $tokens[$currScope]['code'=== T_INTERFACE
  101.             {
  102.                 $this->endFunction
  103.                     = $phpcsFile->findNext(array(T_SEMICOLON)$stackPtr);
  104.             else {
  105.                 if (isset($tokens[$stackPtr]['scope_closer']=== false{
  106.                     $error 'Possible parse error: non-abstract method defined as abstract';
  107.                     $phpcsFile->addWarning($error$stackPtr'Internal.ParseError.NonAbstractDefinedAbstract');
  108.                     return;
  109.                 }
  110.  
  111.                 $this->endFunction $tokens[$stackPtr]['scope_closer'];
  112.             }
  113.         }//end if
  114.  
  115.         if ($tokens[$stackPtr]['code'=== T_DOUBLE_QUOTED_STRING
  116.             || $tokens[$stackPtr]['code'=== T_HEREDOC
  117.         {
  118.             // Check to see if this string has a variable in it.
  119.             $pattern '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
  120.             if (preg_match($pattern$tokens[$stackPtr]['content']!== 0{
  121.                 $this->processVariableInString($phpcsFile$stackPtr);
  122.             }
  123.  
  124.             return;
  125.         }
  126.  
  127.         if ($this->functionOpen === true{
  128.             if ($tokens[$stackPtr]['code'=== T_VARIABLE{
  129.                 $this->processVariable($phpcsFile$stackPtr);
  130.             }
  131.         else {
  132.             // What if we assign a member variable to another?
  133.             // ie. private $_count = $this->_otherCount + 1;.
  134.             $this->processMemberVar($phpcsFile$stackPtr);
  135.         }
  136.  
  137.     }//end processTokenWithinScope()
  138.  
  139.  
  140.     /**
  141.      * Processes the token outside the scope in the file.
  142.      *
  143.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
  144.      *                                                token was found.
  145.      * @param int                         $stackPtr  The position where the token was found.
  146.      *
  147.      * @return void 
  148.      */
  149.     final protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  150.     {
  151.         $tokens $phpcsFile->getTokens();
  152.         // These variables are not member vars.
  153.         if ($tokens[$stackPtr]['code'=== T_VARIABLE{
  154.             $this->processVariable($phpcsFile$stackPtr);
  155.         else if ($tokens[$stackPtr]['code'=== T_DOUBLE_QUOTED_STRING
  156.             || $tokens[$stackPtr]['code'=== T_HEREDOC
  157.         {
  158.             // Check to see if this string has a variable in it.
  159.             $pattern '|(?<!\\\\)(?:\\\\{2})*\${?[a-zA-Z0-9_]+}?|';
  160.             if (preg_match($pattern$tokens[$stackPtr]['content']!== 0{
  161.                 $this->processVariableInString($phpcsFile$stackPtr);
  162.             }
  163.         }
  164.  
  165.     }//end processTokenOutsideScope()
  166.  
  167.  
  168.     /**
  169.      * Called to process class member vars.
  170.      *
  171.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
  172.      *                                                token was found.
  173.      * @param int                         $stackPtr  The position where the token was found.
  174.      *
  175.      * @return void 
  176.      */
  177.     abstract protected function processMemberVar(File $phpcsFile$stackPtr);
  178.  
  179.  
  180.     /**
  181.      * Called to process normal member vars.
  182.      *
  183.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
  184.      *                                                token was found.
  185.      * @param int                         $stackPtr  The position where the token was found.
  186.      *
  187.      * @return void 
  188.      */
  189.     abstract protected function processVariable(File $phpcsFile$stackPtr);
  190.  
  191.  
  192.     /**
  193.      * Called to process variables found in double quoted strings or heredocs.
  194.      *
  195.      * Note that there may be more than one variable in the string, which will
  196.      * result only in one call for the string or one call per line for heredocs.
  197.      *
  198.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this
  199.      *                                                token was found.
  200.      * @param int                         $stackPtr  The position where the double quoted
  201.      *                                                string was found.
  202.      *
  203.      * @return void 
  204.      */
  205.     abstract protected function processVariableInString(File $phpcsFile$stackPtr);
  206.  
  207.  
  208. }//end class

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