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

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