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

Source for file File.php

Documentation is available at File.php

  1. <?php
  2. /**
  3.  * Represents a piece of content being checked during the run.
  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\Files;
  11.  
  12. use PHP_CodeSniffer\Ruleset;
  13. use PHP_CodeSniffer\Config;
  14. use PHP_CodeSniffer\Fixer;
  15. use PHP_CodeSniffer\Util;
  16. use PHP_CodeSniffer\Exceptions\RuntimeException;
  17. use PHP_CodeSniffer\Exceptions\TokenizerException;
  18.  
  19. class File
  20. {
  21.  
  22.     /**
  23.      * The absolute path to the file associated with this object.
  24.      *
  25.      * @var string 
  26.      */
  27.     public $path '';
  28.  
  29.     /**
  30.      * The absolute path to the file associated with this object.
  31.      *
  32.      * @var string 
  33.      */
  34.     protected $content '';
  35.  
  36.     /**
  37.      * The config data for the run.
  38.      *
  39.      * @var \PHP_CodeSniffer\Config 
  40.      */
  41.     public $config = null;
  42.  
  43.     /**
  44.      * The ruleset used for the run.
  45.      *
  46.      * @var \PHP_CodeSniffer\Ruleset 
  47.      */
  48.     public $ruleset = null;
  49.  
  50.     /**
  51.      * If TRUE, the entire file is being ignored.
  52.      *
  53.      * @var string 
  54.      */
  55.     public $ignored = false;
  56.  
  57.     /**
  58.      * The EOL character this file uses.
  59.      *
  60.      * @var string 
  61.      */
  62.     public $eolChar '';
  63.  
  64.     /**
  65.      * The Fixer object to control fixing errors.
  66.      *
  67.      * @var \PHP_CodeSniffer\Fixer 
  68.      */
  69.     public $fixer = null;
  70.  
  71.     /**
  72.      * The tokenizer being used for this file.
  73.      *
  74.      * @var \PHP_CodeSniffer\Tokenizers\Tokenizer 
  75.      */
  76.     public $tokenizer = null;
  77.  
  78.     /**
  79.      * Was the file loaded from cache?
  80.      *
  81.      * If TRUE, the file was loaded from a local cache.
  82.      * If FALSE, the file was tokenized and processed fully.
  83.      *
  84.      * @var boolean 
  85.      */
  86.     public $fromCache = false;
  87.  
  88.     /**
  89.      * The number of tokens in this file.
  90.      *
  91.      * Stored here to save calling count() everywhere.
  92.      *
  93.      * @var integer 
  94.      */
  95.     public $numTokens = 0;
  96.  
  97.     /**
  98.      * The tokens stack map.
  99.      *
  100.      * @var array 
  101.      */
  102.     protected $tokens = array();
  103.  
  104.     /**
  105.      * The errors raised from sniffs.
  106.      *
  107.      * @var array 
  108.      * @see getErrors()
  109.      */
  110.     protected $errors = array();
  111.  
  112.     /**
  113.      * The warnings raised from sniffs.
  114.      *
  115.      * @var array 
  116.      * @see getWarnings()
  117.      */
  118.     protected $warnings = array();
  119.  
  120.     /**
  121.      * The metrics recorded by sniffs.
  122.      *
  123.      * @var array 
  124.      * @see getMetrics()
  125.      */
  126.     protected $metrics = array();
  127.  
  128.     /**
  129.      * The total number of errors raised.
  130.      *
  131.      * @var integer 
  132.      */
  133.     protected $errorCount = 0;
  134.  
  135.     /**
  136.      * The total number of warnings raised.
  137.      *
  138.      * @var integer 
  139.      */
  140.     protected $warningCount = 0;
  141.  
  142.     /**
  143.      * The total number of errors and warnings that can be fixed.
  144.      *
  145.      * @var integer 
  146.      */
  147.     protected $fixableCount = 0;
  148.  
  149.     /**
  150.      * The total number of errors and warnings that were fixed.
  151.      *
  152.      * @var integer 
  153.      */
  154.     protected $fixedCount = 0;
  155.  
  156.     /**
  157.      * An array of sniffs that are being ignored.
  158.      *
  159.      * @var array 
  160.      */
  161.     protected $ignoredListeners = array();
  162.  
  163.     /**
  164.      * An array of message codes that are being ignored.
  165.      *
  166.      * @var array 
  167.      */
  168.     protected $ignoredCodes = array();
  169.  
  170.     /**
  171.      * An array of sniffs listening to this file's processing.
  172.      *
  173.      * @var \PHP_CodeSniffer\Sniffs\Sniff[] 
  174.      */
  175.     protected $listeners = array();
  176.  
  177.     /**
  178.      * The class name of the sniff currently processing the file.
  179.      *
  180.      * @var string 
  181.      */
  182.     protected $activeListener '';
  183.  
  184.     /**
  185.      * An array of sniffs being processed and how long they took.
  186.      *
  187.      * @var array 
  188.      */
  189.     protected $listenerTimes = array();
  190.  
  191.     /**
  192.      * A cache of often used config settings to improve performance.
  193.      *
  194.      * Storing them here saves 10k+ calls to __get() in the Config class.
  195.      *
  196.      * @var array 
  197.      */
  198.     protected $configCache = array();
  199.  
  200.  
  201.     /**
  202.      * Constructs a file.
  203.      *
  204.      * @param string                   $path    The absolute path to the file to process.
  205.      * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
  206.      * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
  207.      *
  208.      * @return void 
  209.      */
  210.     public function __construct($pathRuleset $rulesetConfig $config)
  211.     {
  212.         $this->path    $path;
  213.         $this->ruleset $ruleset;
  214.         $this->config  $config;
  215.         $this->fixer   = new Fixer();
  216.  
  217.         $parts     explode('.'$path);
  218.         $extension array_pop($parts);
  219.         if (isset($config->extensions[$extension]=== true{
  220.             $this->tokenizerType $config->extensions[$extension];
  221.         else {
  222.             // Revert to default.
  223.             $this->tokenizerType 'PHP';
  224.         }
  225.  
  226.         $this->configCache['cache']           $this->config->cache;
  227.         $this->configCache['sniffs']          array_map('strtolower'$this->config->sniffs);
  228.         $this->configCache['exclude']         array_map('strtolower'$this->config->exclude);
  229.         $this->configCache['errorSeverity']   $this->config->errorSeverity;
  230.         $this->configCache['warningSeverity'$this->config->warningSeverity;
  231.         $this->configCache['recordErrors']    $this->config->recordErrors;
  232.         $this->configCache['ignorePatterns']  $this->ruleset->getIgnorePatterns();
  233.  
  234.     }//end __construct()
  235.  
  236.  
  237.     /**
  238.      * Set the content of the file.
  239.      *
  240.      * Setting the content also calculates the EOL char being used.
  241.      *
  242.      * @param string $content The file content.
  243.      *
  244.      * @return void 
  245.      */
  246.     public function setContent($content)
  247.     {
  248.         $this->content $content;
  249.         $this->tokens  = array();
  250.  
  251.         try {
  252.             $this->eolChar = Util\Common::detectLineEndings($content);
  253.         catch (RuntimeException $e{
  254.             $this->addWarningOnLine($e->getMessage()1'Internal.DetectLineEndings');
  255.             return;
  256.         }
  257.  
  258.     }//end setContent()
  259.  
  260.  
  261.     /**
  262.      * Reloads the content of the file.
  263.      *
  264.      * By default, we have no idea where our content comes from,
  265.      * so we can't do anything.
  266.      *
  267.      * @return void 
  268.      */
  269.     public function reloadContent()
  270.     {
  271.  
  272.     }//end reloadContent()
  273.  
  274.  
  275.     /**
  276.      * Disables caching of this file.
  277.      *
  278.      * @return void 
  279.      */
  280.     public function disableCaching()
  281.     {
  282.         $this->configCache['cache'= false;
  283.  
  284.     }//end disableCaching()
  285.  
  286.  
  287.     /**
  288.      * Starts the stack traversal and tells listeners when tokens are found.
  289.      *
  290.      * @return void 
  291.      */
  292.     public function process()
  293.     {
  294.         if ($this->ignored === true{
  295.             return;
  296.         }
  297.  
  298.         $this->errors       = array();
  299.         $this->warnings     = array();
  300.         $this->errorCount   = 0;
  301.         $this->warningCount = 0;
  302.         $this->fixableCount = 0;
  303.  
  304.         $this->parse();
  305.  
  306.         $this->fixer->startFile($this);
  307.  
  308.         if (PHP_CODESNIFFER_VERBOSITY > 2{
  309.             echo "\t*** START TOKEN PROCESSING ***".PHP_EOL;
  310.         }
  311.  
  312.         $foundCode        = false;
  313.         $listenerIgnoreTo = array();
  314.         $inTests          defined('PHP_CODESNIFFER_IN_TESTS');
  315.         $checkAnnotations $this->config->annotations;
  316.  
  317.         // Foreach of the listeners that have registered to listen for this
  318.         // token, get them to process it.
  319.         foreach ($this->tokens as $stackPtr => $token{
  320.             // Check for ignored lines.
  321.             if ($checkAnnotations === true
  322.                 && ($token['code'=== T_COMMENT
  323.                 || $token['code'=== T_DOC_COMMENT_TAG
  324.                 || ($inTests === true && $token['code'=== T_INLINE_HTML))
  325.             {
  326.                 if (strpos($token['content']'@codingStandards'!== false{
  327.                     if (strpos($token['content']'@codingStandardsIgnoreFile'!== false{
  328.                         // Ignoring the whole file, just a little late.
  329.                         $this->errors       = array();
  330.                         $this->warnings     = array();
  331.                         $this->errorCount   = 0;
  332.                         $this->warningCount = 0;
  333.                         $this->fixableCount = 0;
  334.                         return;
  335.                     else if (strpos($token['content']'@codingStandardsChangeSetting'!== false{
  336.                         $start   strpos($token['content']'@codingStandardsChangeSetting');
  337.                         $comment substr($token['content']($start + 30));
  338.                         $parts   explode(' '$comment);
  339.                         if ($parts >= 3{
  340.                             $sniffParts explode('.'$parts[0]);
  341.                             if ($sniffParts >= 3{
  342.                                 // If the sniff code is not know to us, it has not been registered in this run.
  343.                                 // But don't throw an error as it could be there for a different standard to use.
  344.                                 if (isset($this->ruleset->sniffCodes[$parts[0]]=== true{
  345.                                     $listenerCode  array_shift($parts);
  346.                                     $propertyCode  array_shift($parts);
  347.                                     $propertyValue rtrim(implode(' '$parts)" */\r\n");
  348.                                     $listenerClass $this->ruleset->sniffCodes[$listenerCode];
  349.                                     $this->ruleset->setSniffProperty($listenerClass$propertyCode$propertyValue);
  350.                                 }
  351.                             }
  352.                         }
  353.                     }//end if
  354.                 }//end if
  355.             }//end if
  356.  
  357.             if (PHP_CODESNIFFER_VERBOSITY > 2{
  358.                 $type    $token['type'];
  359.                 $content = Util\Common::prepareForOutput($token['content']);
  360.                 echo "\t\tProcess token $stackPtr$type => $content".PHP_EOL;
  361.             }
  362.  
  363.             if ($token['code'!== T_INLINE_HTML{
  364.                 $foundCode = true;
  365.             }
  366.  
  367.             if (isset($this->ruleset->tokenListeners[$token['code']]=== false{
  368.                 continue;
  369.             }
  370.  
  371.             foreach ($this->ruleset->tokenListeners[$token['code']] as $listenerData{
  372.                 if (isset($this->ignoredListeners[$listenerData['class']]=== true
  373.                     || (isset($listenerIgnoreTo[$listenerData['class']]=== true
  374.                     && $listenerIgnoreTo[$listenerData['class']] $stackPtr)
  375.                 {
  376.                     // This sniff is ignoring past this token, or the whole file.
  377.                     continue;
  378.                 }
  379.  
  380.                 // Make sure this sniff supports the tokenizer
  381.                 // we are currently using.
  382.                 $class $listenerData['class'];
  383.  
  384.                 if (isset($listenerData['tokenizers'][$this->tokenizerType]=== false{
  385.                     continue;
  386.                 }
  387.  
  388.                 // If the file path matches one of our ignore patterns, skip it.
  389.                 // While there is support for a type of each pattern
  390.                 // (absolute or relative) we don't actually support it here.
  391.                 foreach ($listenerData['ignore'as $pattern{
  392.                     // We assume a / directory separator, as do the exclude rules
  393.                     // most developers write, so we need a special case for any system
  394.                     // that is different.
  395.                     if (DIRECTORY_SEPARATOR === '\\'{
  396.                         $pattern str_replace('/''\\\\'$pattern);
  397.                     }
  398.  
  399.                     $pattern '`'.$pattern.'`i';
  400.                     if (preg_match($pattern$this->path=== 1{
  401.                         $this->ignoredListeners[$class= true;
  402.                         continue(2);
  403.                     }
  404.                 }
  405.  
  406.                 // If the file path does not match one of our include patterns, skip it.
  407.                 // While there is support for a type of each pattern
  408.                 // (absolute or relative) we don't actually support it here.
  409.                 if (empty($listenerData['include']=== false{
  410.                     $included = false;
  411.                     foreach ($listenerData['include'as $pattern{
  412.                         // We assume a / directory separator, as do the exclude rules
  413.                         // most developers write, so we need a special case for any system
  414.                         // that is different.
  415.                         if (DIRECTORY_SEPARATOR === '\\'{
  416.                             $pattern str_replace('/''\\\\'$pattern);
  417.                         }
  418.  
  419.                         $pattern '`'.$pattern.'`i';
  420.                         if (preg_match($pattern$this->path=== 1{
  421.                             $included = true;
  422.                             break;
  423.                         }
  424.                     }
  425.  
  426.                     if ($included === false{
  427.                         $this->ignoredListeners[$class= true;
  428.                         continue;
  429.                     }
  430.                 }//end if
  431.  
  432.                 $this->activeListener $class;
  433.  
  434.                 if (PHP_CODESNIFFER_VERBOSITY > 2{
  435.                     $startTime microtime(true);
  436.                     echo "\t\t\tProcessing ".$this->activeListener.'... ';
  437.                 }
  438.  
  439.                 $ignoreTo $this->ruleset->sniffs[$class]->process($this$stackPtr);
  440.                 if ($ignoreTo !== null{
  441.                     $listenerIgnoreTo[$this->activeListener$ignoreTo;
  442.                 }
  443.  
  444.                 if (PHP_CODESNIFFER_VERBOSITY > 2{
  445.                     $timeTaken (microtime(true$startTime);
  446.                     if (isset($this->listenerTimes[$this->activeListener]=== false{
  447.                         $this->listenerTimes[$this->activeListener= 0;
  448.                     }
  449.  
  450.                     $this->listenerTimes[$this->activeListener+= $timeTaken;
  451.  
  452.                     $timeTaken round(($timeTaken)4);
  453.                     echo "DONE in $timeTaken seconds".PHP_EOL;
  454.                 }
  455.  
  456.                 $this->activeListener '';
  457.             }//end foreach
  458.         }//end foreach
  459.  
  460.         // If short open tags are off but the file being checked uses
  461.         // short open tags, the whole content will be inline HTML
  462.         // and nothing will be checked. So try and handle this case.
  463.         // We don't show this error for STDIN because we can't be sure the content
  464.         // actually came directly from the user. It could be something like
  465.         // refs from a Git pre-push hook.
  466.         if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN'{
  467.             $shortTags = (bool) ini_get('short_open_tag');
  468.             if ($shortTags === false{
  469.                 $error 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
  470.                 $this->addWarning($errornull'Internal.NoCodeFound');
  471.             }
  472.         }
  473.  
  474.         if (PHP_CODESNIFFER_VERBOSITY > 2{
  475.             echo "\t*** END TOKEN PROCESSING ***".PHP_EOL;
  476.             echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL;
  477.  
  478.             asort($this->listenerTimesSORT_NUMERIC);
  479.             $this->listenerTimes array_reverse($this->listenerTimestrue);
  480.             foreach ($this->listenerTimes as $listener => $timeTaken{
  481.                 echo "\t$listener".round(($timeTaken)4).' secs'.PHP_EOL;
  482.             }
  483.  
  484.             echo "\t*** END SNIFF PROCESSING REPORT ***".PHP_EOL;
  485.         }
  486.  
  487.         $this->fixedCount += $this->fixer->getFixCount();
  488.  
  489.     }//end process()
  490.  
  491.  
  492.     /**
  493.      * Tokenizes the file and prepares it for the test run.
  494.      *
  495.      * @return void 
  496.      */
  497.     public function parse()
  498.     {
  499.         if (empty($this->tokens=== false{
  500.             // File has already been parsed.
  501.             return;
  502.         }
  503.  
  504.         try {
  505.             $tokenizerClass  'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType;
  506.             $this->tokenizer = new $tokenizerClass($this->content$this->config$this->eolChar);
  507.             $this->tokens    $this->tokenizer->getTokens();
  508.         catch (TokenizerException $e{
  509.             $this->addWarning($e->getMessage()null'Internal.Tokenizer.Exception');
  510.             if (PHP_CODESNIFFER_VERBOSITY > 0{
  511.                 echo "[$this->tokenizerType => tokenizer error]... ";
  512.                 if (PHP_CODESNIFFER_VERBOSITY > 1{
  513.                     echo PHP_EOL;
  514.                 }
  515.             }
  516.  
  517.             return;
  518.         }
  519.  
  520.         $this->numTokens count($this->tokens);
  521.  
  522.         // Check for mixed line endings as these can cause tokenizer errors and we
  523.         // should let the user know that the results they get may be incorrect.
  524.         // This is done by removing all backslashes, removing the newline char we
  525.         // detected, then converting newlines chars into text. If any backslashes
  526.         // are left at the end, we have additional newline chars in use.
  527.         $contents str_replace('\\'''$this->content);
  528.         $contents str_replace($this->eolChar''$contents);
  529.         $contents str_replace("\n"'\n'$contents);
  530.         $contents str_replace("\r"'\r'$contents);
  531.         if (strpos($contents'\\'!== false{
  532.             $error 'File has mixed line endings; this may cause incorrect results';
  533.             $this->addWarningOnLine($error1'Internal.LineEndings.Mixed');
  534.         }
  535.  
  536.         if (PHP_CODESNIFFER_VERBOSITY > 0{
  537.             if ($this->numTokens === 0{
  538.                 $numLines = 0;
  539.             else {
  540.                 $numLines $this->tokens[($this->numTokens - 1)]['line'];
  541.             }
  542.  
  543.             echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... ";
  544.             if (PHP_CODESNIFFER_VERBOSITY > 1{
  545.                 echo PHP_EOL;
  546.             }
  547.         }
  548.  
  549.     }//end parse()
  550.  
  551.  
  552.     /**
  553.      * Returns the token stack for this file.
  554.      *
  555.      * @return array 
  556.      */
  557.     public function getTokens()
  558.     {
  559.         return $this->tokens;
  560.  
  561.     }//end getTokens()
  562.  
  563.  
  564.     /**
  565.      * Remove vars stored in this file that are no longer required.
  566.      *
  567.      * @return void 
  568.      */
  569.     public function cleanUp()
  570.     {
  571.         $this->listenerTimes = null;
  572.         $this->content       = null;
  573.         $this->tokens        = null;
  574.         $this->tokenizer     = null;
  575.         $this->fixer         = null;
  576.         $this->config        = null;
  577.         $this->ruleset       = null;
  578.  
  579.     }//end cleanUp()
  580.  
  581.  
  582.     /**
  583.      * Records an error against a specific token in the file.
  584.      *
  585.      * @param string  $error    The error message.
  586.      * @param int     $stackPtr The stack position where the error occurred.
  587.      * @param string  $code     A violation code unique to the sniff message.
  588.      * @param array   $data     Replacements for the error message.
  589.      * @param int     $severity The severity level for this error. A value of 0
  590.      *                           will be converted into the default severity level.
  591.      * @param boolean $fixable  Can the error be fixed by the sniff?
  592.      *
  593.      * @return boolean 
  594.      */
  595.     public function addError(
  596.         $error,
  597.         $stackPtr,
  598.         $code,
  599.         $data=array(),
  600.         $severity=0,
  601.         $fixable=false
  602.     {
  603.         if ($stackPtr === null{
  604.             $line   = 1;
  605.             $column = 1;
  606.         else {
  607.             $line   $this->tokens[$stackPtr]['line'];
  608.             $column $this->tokens[$stackPtr]['column'];
  609.         }
  610.  
  611.         return $this->addMessage(true$error$line$column$code$data$severity$fixable);
  612.  
  613.     }//end addError()
  614.  
  615.  
  616.     /**
  617.      * Records a warning against a specific token in the file.
  618.      *
  619.      * @param string  $warning  The error message.
  620.      * @param int     $stackPtr The stack position where the error occurred.
  621.      * @param string  $code     A violation code unique to the sniff message.
  622.      * @param array   $data     Replacements for the warning message.
  623.      * @param int     $severity The severity level for this warning. A value of 0
  624.      *                           will be converted into the default severity level.
  625.      * @param boolean $fixable  Can the warning be fixed by the sniff?
  626.      *
  627.      * @return boolean 
  628.      */
  629.     public function addWarning(
  630.         $warning,
  631.         $stackPtr,
  632.         $code,
  633.         $data=array(),
  634.         $severity=0,
  635.         $fixable=false
  636.     {
  637.         if ($stackPtr === null{
  638.             $line   = 1;
  639.             $column = 1;
  640.         else {
  641.             $line   $this->tokens[$stackPtr]['line'];
  642.             $column $this->tokens[$stackPtr]['column'];
  643.         }
  644.  
  645.         return $this->addMessage(false$warning$line$column$code$data$severity$fixable);
  646.  
  647.     }//end addWarning()
  648.  
  649.  
  650.     /**
  651.      * Records an error against a specific line in the file.
  652.      *
  653.      * @param string $error    The error message.
  654.      * @param int    $line     The line on which the error occurred.
  655.      * @param string $code     A violation code unique to the sniff message.
  656.      * @param array  $data     Replacements for the error message.
  657.      * @param int    $severity The severity level for this error. A value of 0
  658.      *                          will be converted into the default severity level.
  659.      *
  660.      * @return boolean 
  661.      */
  662.     public function addErrorOnLine(
  663.         $error,
  664.         $line,
  665.         $code,
  666.         $data=array(),
  667.         $severity=0
  668.     {
  669.         return $this->addMessage(true$error$line1$code$data$severityfalse);
  670.  
  671.     }//end addErrorOnLine()
  672.  
  673.  
  674.     /**
  675.      * Records a warning against a specific token in the file.
  676.      *
  677.      * @param string $warning  The error message.
  678.      * @param int    $line     The line on which the warning occurred.
  679.      * @param string $code     A violation code unique to the sniff message.
  680.      * @param array  $data     Replacements for the warning message.
  681.      * @param int    $severity The severity level for this warning. A value of 0 will
  682.      *                          will be converted into the default severity level.
  683.      *
  684.      * @return boolean 
  685.      */
  686.     public function addWarningOnLine(
  687.         $warning,
  688.         $line,
  689.         $code,
  690.         $data=array(),
  691.         $severity=0
  692.     {
  693.         return $this->addMessage(false$warning$line1$code$data$severityfalse);
  694.  
  695.     }//end addWarningOnLine()
  696.  
  697.  
  698.     /**
  699.      * Records a fixable error against a specific token in the file.
  700.      *
  701.      * Returns true if the error was recorded and should be fixed.
  702.      *
  703.      * @param string $error    The error message.
  704.      * @param int    $stackPtr The stack position where the error occurred.
  705.      * @param string $code     A violation code unique to the sniff message.
  706.      * @param array  $data     Replacements for the error message.
  707.      * @param int    $severity The severity level for this error. A value of 0
  708.      *                          will be converted into the default severity level.
  709.      *
  710.      * @return boolean 
  711.      */
  712.     public function addFixableError(
  713.         $error,
  714.         $stackPtr,
  715.         $code,
  716.         $data=array(),
  717.         $severity=0
  718.     {
  719.         $recorded $this->addError($error$stackPtr$code$data$severitytrue);
  720.         if ($recorded === true && $this->fixer->enabled === true{
  721.             return true;
  722.         }
  723.  
  724.         return false;
  725.  
  726.     }//end addFixableError()
  727.  
  728.  
  729.     /**
  730.      * Records a fixable warning against a specific token in the file.
  731.      *
  732.      * Returns true if the warning was recorded and should be fixed.
  733.      *
  734.      * @param string $warning  The error message.
  735.      * @param int    $stackPtr The stack position where the error occurred.
  736.      * @param string $code     A violation code unique to the sniff message.
  737.      * @param array  $data     Replacements for the warning message.
  738.      * @param int    $severity The severity level for this warning. A value of 0
  739.      *                          will be converted into the default severity level.
  740.      *
  741.      * @return boolean 
  742.      */
  743.     public function addFixableWarning(
  744.         $warning,
  745.         $stackPtr,
  746.         $code,
  747.         $data=array(),
  748.         $severity=0
  749.     {
  750.         $recorded $this->addWarning($warning$stackPtr$code$data$severitytrue);
  751.         if ($recorded === true && $this->fixer->enabled === true{
  752.             return true;
  753.         }
  754.  
  755.         return false;
  756.  
  757.     }//end addFixableWarning()
  758.  
  759.  
  760.     /**
  761.      * Adds an error to the error stack.
  762.      *
  763.      * @param boolean $error    Is this an error message?
  764.      * @param string  $message  The text of the message.
  765.      * @param int     $line     The line on which the message occurred.
  766.      * @param int     $column   The column at which the message occurred.
  767.      * @param string  $code     A violation code unique to the sniff message.
  768.      * @param array   $data     Replacements for the message.
  769.      * @param int     $severity The severity level for this message. A value of 0
  770.      *                           will be converted into the default severity level.
  771.      * @param boolean $fixable  Can the problem be fixed by the sniff?
  772.      *
  773.      * @return boolean 
  774.      */
  775.     protected function addMessage($error$message$line$column$code$data$severity$fixable)
  776.     {
  777.         if (isset($this->tokenizer->ignoredLines[$line]=== true{
  778.             return false;
  779.         }
  780.  
  781.         $includeAll = true;
  782.         if ($this->configCache['cache'=== false
  783.             || $this->configCache['recordErrors'=== false
  784.         {
  785.             $includeAll = false;
  786.         }
  787.  
  788.         // Work out which sniff generated the message.
  789.         $parts explode('.'$code);
  790.         if ($parts[0=== 'Internal'{
  791.             // An internal message.
  792.             $listenerCode = Util\Common::getSniffCode($this->activeListener);
  793.             $sniffCode    $code;
  794.             $checkCodes   = array($sniffCode);
  795.         else {
  796.             if ($parts[0!== $code{
  797.                 // The full message code has been passed in.
  798.                 $sniffCode    $code;
  799.                 $listenerCode substr($sniffCode0strrpos($sniffCode'.'));
  800.             else {
  801.                 $listenerCode = Util\Common::getSniffCode($this->activeListener);
  802.                 $sniffCode    $listenerCode.'.'.$code;
  803.                 $parts        explode('.'$sniffCode);
  804.             }
  805.  
  806.             $checkCodes = array(
  807.                            $sniffCode,
  808.                            $parts[0].'.'.$parts[1].'.'.$parts[2],
  809.                            $parts[0].'.'.$parts[1],
  810.                            $parts[0],
  811.                           );
  812.         }//end if
  813.  
  814.         // Filter out any messages for sniffs that shouldn't have run
  815.         // due to the use of the --sniffs command line argument.
  816.         if ($includeAll === false
  817.             && ((empty($this->configCache['sniffs']=== false
  818.             && in_array(strtolower($listenerCode)$this->configCache['sniffs']=== false)
  819.             || (empty($this->configCache['exclude']=== false
  820.             && in_array(strtolower($listenerCode)$this->configCache['exclude']=== true))
  821.         {
  822.             return false;
  823.         }
  824.  
  825.         // If we know this sniff code is being ignored for this file, return early.
  826.         foreach ($checkCodes as $checkCode{
  827.             if (isset($this->ignoredCodes[$checkCode]=== true{
  828.                 return false;
  829.             }
  830.         }
  831.  
  832.         $oppositeType 'warning';
  833.         if ($error === false{
  834.             $oppositeType 'error';
  835.         }
  836.  
  837.         foreach ($checkCodes as $checkCode{
  838.             // Make sure this message type has not been set to the opposite message type.
  839.             if (isset($this->ruleset->ruleset[$checkCode]['type']=== true
  840.                 && $this->ruleset->ruleset[$checkCode]['type'=== $oppositeType
  841.             {
  842.                 $error !$error;
  843.                 break;
  844.             }
  845.         }
  846.  
  847.         if ($error === true{
  848.             $configSeverity $this->configCache['errorSeverity'];
  849.             $messageCount   &$this->errorCount;
  850.             $messages       &$this->errors;
  851.         else {
  852.             $configSeverity $this->configCache['warningSeverity'];
  853.             $messageCount   &$this->warningCount;
  854.             $messages       &$this->warnings;
  855.         }
  856.  
  857.         if ($includeAll === false && $configSeverity === 0{
  858.             // Don't bother doing any processing as these messages are just going to
  859.             // be hidden in the reports anyway.
  860.             return false;
  861.         }
  862.  
  863.         if ($severity === 0{
  864.             $severity = 5;
  865.         }
  866.  
  867.         foreach ($checkCodes as $checkCode{
  868.             // Make sure we are interested in this severity level.
  869.             if (isset($this->ruleset->ruleset[$checkCode]['severity']=== true{
  870.                 $severity $this->ruleset->ruleset[$checkCode]['severity'];
  871.                 break;
  872.             }
  873.         }
  874.  
  875.         if ($includeAll === false && $configSeverity $severity{
  876.             return false;
  877.         }
  878.  
  879.         // Make sure we are not ignoring this file.
  880.         foreach ($checkCodes as $checkCode{
  881.             if (isset($this->configCache['ignorePatterns'][$checkCode]=== false{
  882.                 continue;
  883.             }
  884.  
  885.             foreach ($this->configCache['ignorePatterns'][$checkCodeas $pattern => $type{
  886.                 // While there is support for a type of each pattern
  887.                 // (absolute or relative) we don't actually support it here.
  888.                 $replacements = array(
  889.                                  '\\,' => ',',
  890.                                  '*'   => '.*',
  891.                                 );
  892.  
  893.                 // We assume a / directory separator, as do the exclude rules
  894.                 // most developers write, so we need a special case for any system
  895.                 // that is different.
  896.                 if (DIRECTORY_SEPARATOR === '\\'{
  897.                     $replacements['/''\\\\';
  898.                 }
  899.  
  900.                 $pattern '`'.strtr($pattern$replacements).'`i';
  901.                 if (preg_match($pattern$this->path=== 1{
  902.                     $this->ignoredCodes[$checkCode= true;
  903.                     return false;
  904.                 }
  905.             }//end foreach
  906.         }//end foreach
  907.  
  908.         $messageCount++;
  909.         if ($fixable === true{
  910.             $this->fixableCount++;
  911.         }
  912.  
  913.         if ($this->configCache['recordErrors'=== false
  914.             && $includeAll === false
  915.         {
  916.             return true;
  917.         }
  918.  
  919.         // Work out the error message.
  920.         if (isset($this->ruleset->ruleset[$sniffCode]['message']=== true{
  921.             $message $this->ruleset->ruleset[$sniffCode]['message'];
  922.         }
  923.  
  924.         if (empty($data=== false{
  925.             $message vsprintf($message$data);
  926.         }
  927.  
  928.         if (isset($messages[$line]=== false{
  929.             $messages[$line= array();
  930.         }
  931.  
  932.         if (isset($messages[$line][$column]=== false{
  933.             $messages[$line][$column= array();
  934.         }
  935.  
  936.         $messages[$line][$column][= array(
  937.                                        'message'  => $message,
  938.                                        'source'   => $sniffCode,
  939.                                        'listener' => $this->activeListener,
  940.                                        'severity' => $severity,
  941.                                        'fixable'  => $fixable,
  942.                                       );
  943.  
  944.         if (PHP_CODESNIFFER_VERBOSITY > 1
  945.             && $this->fixer->enabled === true
  946.             && $fixable === true
  947.         {
  948.             @ob_end_clean();
  949.             echo "\tE: [Line $line$message ($sniffCode)".PHP_EOL;
  950.             ob_start();
  951.         }
  952.  
  953.         return true;
  954.  
  955.     }//end addMessage()
  956.  
  957.  
  958.     /**
  959.      * Adds an warning to the warning stack.
  960.      *
  961.      * @param int    $stackPtr The stack position where the metric was recorded.
  962.      * @param string $metric   The name of the metric being recorded.
  963.      * @param string $value    The value of the metric being recorded.
  964.      *
  965.      * @return boolean 
  966.      */
  967.     public function recordMetric($stackPtr$metric$value)
  968.     {
  969.         if (isset($this->metrics[$metric]=== false{
  970.             $this->metrics[$metric= array('values' => array($value => 1));
  971.         else {
  972.             if (isset($this->metrics[$metric]['values'][$value]=== false{
  973.                 $this->metrics[$metric]['values'][$value= 1;
  974.             else {
  975.                 $this->metrics[$metric]['values'][$value]++;
  976.             }
  977.         }
  978.  
  979.         return true;
  980.  
  981.     }//end recordMetric()
  982.  
  983.  
  984.     /**
  985.      * Returns the number of errors raised.
  986.      *
  987.      * @return int 
  988.      */
  989.     public function getErrorCount()
  990.     {
  991.         return $this->errorCount;
  992.  
  993.     }//end getErrorCount()
  994.  
  995.  
  996.     /**
  997.      * Returns the number of warnings raised.
  998.      *
  999.      * @return int 
  1000.      */
  1001.     public function getWarningCount()
  1002.     {
  1003.         return $this->warningCount;
  1004.  
  1005.     }//end getWarningCount()
  1006.  
  1007.  
  1008.     /**
  1009.      * Returns the number of successes recorded.
  1010.      *
  1011.      * @return int 
  1012.      */
  1013.     public function getSuccessCount()
  1014.     {
  1015.         return $this->successCount;
  1016.  
  1017.     }//end getSuccessCount()
  1018.  
  1019.  
  1020.     /**
  1021.      * Returns the number of fixable errors/warnings raised.
  1022.      *
  1023.      * @return int 
  1024.      */
  1025.     public function getFixableCount()
  1026.     {
  1027.         return $this->fixableCount;
  1028.  
  1029.     }//end getFixableCount()
  1030.  
  1031.  
  1032.     /**
  1033.      * Returns the number of fixed errors/warnings.
  1034.      *
  1035.      * @return int 
  1036.      */
  1037.     public function getFixedCount()
  1038.     {
  1039.         return $this->fixedCount;
  1040.  
  1041.     }//end getFixedCount()
  1042.  
  1043.  
  1044.     /**
  1045.      * Returns the list of ignored lines.
  1046.      *
  1047.      * @return array 
  1048.      */
  1049.     public function getIgnoredLines()
  1050.     {
  1051.         return $this->tokenizer->ignoredLines;
  1052.  
  1053.     }//end getIgnoredLines()
  1054.  
  1055.  
  1056.     /**
  1057.      * Returns the errors raised from processing this file.
  1058.      *
  1059.      * @return array 
  1060.      */
  1061.     public function getErrors()
  1062.     {
  1063.         return $this->errors;
  1064.  
  1065.     }//end getErrors()
  1066.  
  1067.  
  1068.     /**
  1069.      * Returns the warnings raised from processing this file.
  1070.      *
  1071.      * @return array 
  1072.      */
  1073.     public function getWarnings()
  1074.     {
  1075.         return $this->warnings;
  1076.  
  1077.     }//end getWarnings()
  1078.  
  1079.  
  1080.     /**
  1081.      * Returns the metrics found while processing this file.
  1082.      *
  1083.      * @return array 
  1084.      */
  1085.     public function getMetrics()
  1086.     {
  1087.         return $this->metrics;
  1088.  
  1089.     }//end getMetrics()
  1090.  
  1091.  
  1092.     /**
  1093.      * Returns the absolute filename of this file.
  1094.      *
  1095.      * @return string 
  1096.      */
  1097.     public function getFilename()
  1098.     {
  1099.         return $this->path;
  1100.  
  1101.     }//end getFilename()
  1102.  
  1103.  
  1104.     /**
  1105.      * Returns the declaration names for classes, interfaces, traits, and functions.
  1106.      *
  1107.      * @param int $stackPtr The position of the declaration token which
  1108.      *                       declared the class, interface, trait, or function.
  1109.      *
  1110.      * @return string|nullThe name of the class, interface, trait, or function;
  1111.      *                      or NULL if the function or class is anonymous.
  1112.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type
  1113.      *                                                       T_FUNCTION, T_CLASS, T_ANON_CLASS,
  1114.      *                                                       T_CLOSURE, T_TRAIT, or T_INTERFACE.
  1115.      */
  1116.     public function getDeclarationName($stackPtr)
  1117.     {
  1118.         $tokenCode $this->tokens[$stackPtr]['code'];
  1119.  
  1120.         if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE{
  1121.             return null;
  1122.         }
  1123.  
  1124.         if ($tokenCode !== T_FUNCTION
  1125.             && $tokenCode !== T_CLASS
  1126.             && $tokenCode !== T_INTERFACE
  1127.             && $tokenCode !== T_TRAIT
  1128.         {
  1129.             throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT');
  1130.         }
  1131.  
  1132.         if ($tokenCode === T_FUNCTION
  1133.             && strtolower($this->tokens[$stackPtr]['content']!== 'function'
  1134.         {
  1135.             // This is a function declared without the "function" keyword.
  1136.             // So this token is the function name.
  1137.             return $this->tokens[$stackPtr]['content'];
  1138.         }
  1139.  
  1140.         $content = null;
  1141.         for ($i $stackPtr$i $this->numTokens$i++{
  1142.             if ($this->tokens[$i]['code'=== T_STRING{
  1143.                 $content $this->tokens[$i]['content'];
  1144.                 break;
  1145.             }
  1146.         }
  1147.  
  1148.         return $content;
  1149.  
  1150.     }//end getDeclarationName()
  1151.  
  1152.  
  1153.     /**
  1154.      * Returns the method parameters for the specified function token.
  1155.      *
  1156.      * Each parameter is in the following format:
  1157.      *
  1158.      * <code>
  1159.      *   0 => array(
  1160.      *         'name'              => '$var',  // The variable name.
  1161.      *         'token'             => integer, // The stack pointer to the variable name.
  1162.      *         'content'           => string,  // The full content of the variable definition.
  1163.      *         'pass_by_reference' => boolean, // Is the variable passed by reference?
  1164.      *         'variable_length'   => boolean, // Is the param of variable length through use of `...` ?
  1165.      *         'type_hint'         => string,  // The type hint for the variable.
  1166.      *         'nullable_type'     => boolean, // Is the variable using a nullable type?
  1167.      *        )
  1168.      * </code>
  1169.      *
  1170.      * Parameters with default values have an additional array index of
  1171.      * 'default' with the value of the default as a string.
  1172.      *
  1173.      * @param int $stackPtr The position in the stack of the function token
  1174.      *                       to acquire the parameters for.
  1175.      *
  1176.      * @return array 
  1177.      * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified $stackPtr is not of
  1178.      *                                                         type T_FUNCTION or T_CLOSURE.
  1179.      */
  1180.     public function getMethodParameters($stackPtr)
  1181.     {
  1182.         if ($this->tokens[$stackPtr]['code'!== T_FUNCTION
  1183.             && $this->tokens[$stackPtr]['code'!== T_CLOSURE
  1184.         {
  1185.             throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
  1186.         }
  1187.  
  1188.         $opener $this->tokens[$stackPtr]['parenthesis_opener'];
  1189.         $closer $this->tokens[$stackPtr]['parenthesis_closer'];
  1190.  
  1191.         $vars            = array();
  1192.         $currVar         = null;
  1193.         $paramStart      ($opener + 1);
  1194.         $defaultStart    = null;
  1195.         $paramCount      = 0;
  1196.         $passByReference = false;
  1197.         $variableLength  = false;
  1198.         $typeHint        '';
  1199.         $nullableType    = false;
  1200.  
  1201.         for ($i $paramStart$i <= $closer$i++{
  1202.             // Check to see if this token has a parenthesis or bracket opener. If it does
  1203.             // it's likely to be an array which might have arguments in it. This
  1204.             // could cause problems in our parsing below, so lets just skip to the
  1205.             // end of it.
  1206.             if (isset($this->tokens[$i]['parenthesis_opener']=== true{
  1207.                 // Don't do this if it's the close parenthesis for the method.
  1208.                 if ($i !== $this->tokens[$i]['parenthesis_closer']{
  1209.                     $i ($this->tokens[$i]['parenthesis_closer'+ 1);
  1210.                 }
  1211.             }
  1212.  
  1213.             if (isset($this->tokens[$i]['bracket_opener']=== true{
  1214.                 // Don't do this if it's the close parenthesis for the method.
  1215.                 if ($i !== $this->tokens[$i]['bracket_closer']{
  1216.                     $i ($this->tokens[$i]['bracket_closer'+ 1);
  1217.                 }
  1218.             }
  1219.  
  1220.             switch ($this->tokens[$i]['code']{
  1221.             case T_BITWISE_AND:
  1222.                 if ($defaultStart === null{
  1223.                     $passByReference = true;
  1224.                 }
  1225.                 break;
  1226.             case T_VARIABLE:
  1227.                 $currVar $i;
  1228.                 break;
  1229.             case T_ELLIPSIS:
  1230.                 $variableLength = true;
  1231.                 break;
  1232.             case T_ARRAY_HINT:
  1233.             case T_CALLABLE:
  1234.                 $typeHint .= $this->tokens[$i]['content'];
  1235.                 break;
  1236.             case T_SELF:
  1237.             case T_PARENT:
  1238.             case T_STATIC:
  1239.                 // Self is valid, the others invalid, but were probably intended as type hints.
  1240.                 if (isset($defaultStart=== false{
  1241.                     $typeHint .= $this->tokens[$i]['content'];
  1242.                 }
  1243.                 break;
  1244.             case T_STRING:
  1245.                 // This is a string, so it may be a type hint, but it could
  1246.                 // also be a constant used as a default value.
  1247.                 $prevComma = false;
  1248.                 for ($t $i$t >= $opener$t--{
  1249.                     if ($this->tokens[$t]['code'=== T_COMMA{
  1250.                         $prevComma $t;
  1251.                         break;
  1252.                     }
  1253.                 }
  1254.  
  1255.                 if ($prevComma !== false{
  1256.                     $nextEquals = false;
  1257.                     for ($t $prevComma$t $i$t++{
  1258.                         if ($this->tokens[$t]['code'=== T_EQUAL{
  1259.                             $nextEquals $t;
  1260.                             break;
  1261.                         }
  1262.                     }
  1263.  
  1264.                     if ($nextEquals !== false{
  1265.                         break;
  1266.                     }
  1267.                 }
  1268.  
  1269.                 if ($defaultStart === null{
  1270.                     $typeHint .= $this->tokens[$i]['content'];
  1271.                 }
  1272.                 break;
  1273.             case T_NS_SEPARATOR:
  1274.                 // Part of a type hint or default value.
  1275.                 if ($defaultStart === null{
  1276.                     $typeHint .= $this->tokens[$i]['content'];
  1277.                 }
  1278.                 break;
  1279.             case T_NULLABLE:
  1280.                 if ($defaultStart === null{
  1281.                     $nullableType = true;
  1282.                     $typeHint    .= $this->tokens[$i]['content'];
  1283.                 }
  1284.                 break;
  1285.             case T_CLOSE_PARENTHESIS:
  1286.             case T_COMMA:
  1287.                 // If it's null, then there must be no parameters for this
  1288.                 // method.
  1289.                 if ($currVar === null{
  1290.                     continue;
  1291.                 }
  1292.  
  1293.                 $vars[$paramCount]            = array();
  1294.                 $vars[$paramCount]['token']   $currVar;
  1295.                 $vars[$paramCount]['name']    $this->tokens[$currVar]['content'];
  1296.                 $vars[$paramCount]['content'trim($this->getTokensAsString($paramStart($i $paramStart)));
  1297.  
  1298.                 if ($defaultStart !== null{
  1299.                     $vars[$paramCount]['default'trim($this->getTokensAsString($defaultStart($i $defaultStart)));
  1300.                 }
  1301.  
  1302.                 $vars[$paramCount]['pass_by_reference'$passByReference;
  1303.                 $vars[$paramCount]['variable_length']   $variableLength;
  1304.                 $vars[$paramCount]['type_hint']         $typeHint;
  1305.                 $vars[$paramCount]['nullable_type']     $nullableType;
  1306.  
  1307.                 // Reset the vars, as we are about to process the next parameter.
  1308.                 $defaultStart    = null;
  1309.                 $paramStart      ($i + 1);
  1310.                 $passByReference = false;
  1311.                 $variableLength  = false;
  1312.                 $typeHint        '';
  1313.                 $nullableType    = false;
  1314.  
  1315.                 $paramCount++;
  1316.                 break;
  1317.             case T_EQUAL:
  1318.                 $defaultStart ($i + 1);
  1319.                 break;
  1320.             }//end switch
  1321.         }//end for
  1322.  
  1323.         return $vars;
  1324.  
  1325.     }//end getMethodParameters()
  1326.  
  1327.  
  1328.     /**
  1329.      * Returns the visibility and implementation properties of a method.
  1330.      *
  1331.      * The format of the array is:
  1332.      * <code>
  1333.      *   array(
  1334.      *    'scope'           => 'public', // public protected or protected
  1335.      *    'scope_specified' => true,     // true is scope keyword was found.
  1336.      *    'is_abstract'     => false,    // true if the abstract keyword was found.
  1337.      *    'is_final'        => false,    // true if the final keyword was found.
  1338.      *    'is_static'       => false,    // true if the static keyword was found.
  1339.      *   );
  1340.      * </code>
  1341.      *
  1342.      * @param int $stackPtr The position in the stack of the function token to
  1343.      *                       acquire the properties for.
  1344.      *
  1345.      * @return array 
  1346.      * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
  1347.      *                                                         T_FUNCTION token.
  1348.      */
  1349.     public function getMethodProperties($stackPtr)
  1350.     {
  1351.         if ($this->tokens[$stackPtr]['code'!== T_FUNCTION
  1352.             && $this->tokens[$stackPtr]['code'!== T_CLOSURE
  1353.         {
  1354.             throw new TokenizerException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
  1355.         }
  1356.  
  1357.         if ($this->tokens[$stackPtr]['code'=== T_FUNCTION{
  1358.             $valid = array(
  1359.                       T_PUBLIC      => T_PUBLIC,
  1360.                       T_PRIVATE     => T_PRIVATE,
  1361.                       T_PROTECTED   => T_PROTECTED,
  1362.                       T_STATIC      => T_STATIC,
  1363.                       T_FINAL       => T_FINAL,
  1364.                       T_ABSTRACT    => T_ABSTRACT,
  1365.                       T_WHITESPACE  => T_WHITESPACE,
  1366.                       T_COMMENT     => T_COMMENT,
  1367.                       T_DOC_COMMENT => T_DOC_COMMENT,
  1368.                      );
  1369.         else {
  1370.             $valid = array(
  1371.                       T_STATIC      => T_STATIC,
  1372.                       T_WHITESPACE  => T_WHITESPACE,
  1373.                       T_COMMENT     => T_COMMENT,
  1374.                       T_DOC_COMMENT => T_DOC_COMMENT,
  1375.                      );
  1376.         }
  1377.  
  1378.         $scope          'public';
  1379.         $scopeSpecified = false;
  1380.         $isAbstract     = false;
  1381.         $isFinal        = false;
  1382.         $isStatic       = false;
  1383.  
  1384.         for ($i ($stackPtr - 1)$i > 0; $i--{
  1385.             if (isset($valid[$this->tokens[$i]['code']]=== false{
  1386.                 break;
  1387.             }
  1388.  
  1389.             switch ($this->tokens[$i]['code']{
  1390.             case T_PUBLIC:
  1391.                 $scope          'public';
  1392.                 $scopeSpecified = true;
  1393.                 break;
  1394.             case T_PRIVATE:
  1395.                 $scope          'private';
  1396.                 $scopeSpecified = true;
  1397.                 break;
  1398.             case T_PROTECTED:
  1399.                 $scope          'protected';
  1400.                 $scopeSpecified = true;
  1401.                 break;
  1402.             case T_ABSTRACT:
  1403.                 $isAbstract = true;
  1404.                 break;
  1405.             case T_FINAL:
  1406.                 $isFinal = true;
  1407.                 break;
  1408.             case T_STATIC:
  1409.                 $isStatic = true;
  1410.                 break;
  1411.             }//end switch
  1412.         }//end for
  1413.  
  1414.         return array(
  1415.                 'scope'           => $scope,
  1416.                 'scope_specified' => $scopeSpecified,
  1417.                 'is_abstract'     => $isAbstract,
  1418.                 'is_final'        => $isFinal,
  1419.                 'is_static'       => $isStatic,
  1420.                );
  1421.  
  1422.     }//end getMethodProperties()
  1423.  
  1424.  
  1425.     /**
  1426.      * Returns the visibility and implementation properties of the class member
  1427.      * variable found at the specified position in the stack.
  1428.      *
  1429.      * The format of the array is:
  1430.      *
  1431.      * <code>
  1432.      *   array(
  1433.      *    'scope'       => 'public', // public protected or protected
  1434.      *    'is_static'   => false,    // true if the static keyword was found.
  1435.      *   );
  1436.      * </code>
  1437.      *
  1438.      * @param int $stackPtr The position in the stack of the T_VARIABLE token to
  1439.      *                       acquire the properties for.
  1440.      *
  1441.      * @return array 
  1442.      * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
  1443.      *                                                         T_VARIABLE token, or if the position is not
  1444.      *                                                         a class member variable.
  1445.      */
  1446.     public function getMemberProperties($stackPtr)
  1447.     {
  1448.         if ($this->tokens[$stackPtr]['code'!== T_VARIABLE{
  1449.             throw new TokenizerException('$stackPtr must be of type T_VARIABLE');
  1450.         }
  1451.  
  1452.         $conditions array_keys($this->tokens[$stackPtr]['conditions']);
  1453.         $ptr        array_pop($conditions);
  1454.         if (isset($this->tokens[$ptr]=== false
  1455.             || ($this->tokens[$ptr]['code'!== T_CLASS
  1456.             && $this->tokens[$ptr]['code'!== T_ANON_CLASS
  1457.             && $this->tokens[$ptr]['code'!== T_TRAIT)
  1458.         {
  1459.             if (isset($this->tokens[$ptr]=== true
  1460.                 && $this->tokens[$ptr]['code'=== T_INTERFACE
  1461.             {
  1462.                 // T_VARIABLEs in interfaces can actually be method arguments
  1463.                 // but they wont be seen as being inside the method because there
  1464.                 // are no scope openers and closers for abstract methods. If it is in
  1465.                 // parentheses, we can be pretty sure it is a method argument.
  1466.                 if (isset($this->tokens[$stackPtr]['nested_parenthesis']=== false
  1467.                     || empty($this->tokens[$stackPtr]['nested_parenthesis']=== true
  1468.                 {
  1469.                     $error 'Possible parse error: interfaces may not include member vars';
  1470.                     $this->addWarning($error$stackPtr'Internal.ParseError.InterfaceHasMemberVar');
  1471.                     return array();
  1472.                 }
  1473.             else {
  1474.                 throw new TokenizerException('$stackPtr is not a class member var');
  1475.             }
  1476.         }
  1477.  
  1478.         $valid = array(
  1479.                   T_PUBLIC      => T_PUBLIC,
  1480.                   T_PRIVATE     => T_PRIVATE,
  1481.                   T_PROTECTED   => T_PROTECTED,
  1482.                   T_STATIC      => T_STATIC,
  1483.                   T_WHITESPACE  => T_WHITESPACE,
  1484.                   T_COMMENT     => T_COMMENT,
  1485.                   T_DOC_COMMENT => T_DOC_COMMENT,
  1486.                   T_VARIABLE    => T_VARIABLE,
  1487.                   T_COMMA       => T_COMMA,
  1488.                  );
  1489.  
  1490.         $scope          'public';
  1491.         $scopeSpecified = false;
  1492.         $isStatic       = false;
  1493.  
  1494.         for ($i ($stackPtr - 1)$i > 0; $i--{
  1495.             if (isset($valid[$this->tokens[$i]['code']]=== false{
  1496.                 break;
  1497.             }
  1498.  
  1499.             switch ($this->tokens[$i]['code']{
  1500.             case T_PUBLIC:
  1501.                 $scope          'public';
  1502.                 $scopeSpecified = true;
  1503.                 break;
  1504.             case T_PRIVATE:
  1505.                 $scope          'private';
  1506.                 $scopeSpecified = true;
  1507.                 break;
  1508.             case T_PROTECTED:
  1509.                 $scope          'protected';
  1510.                 $scopeSpecified = true;
  1511.                 break;
  1512.             case T_STATIC:
  1513.                 $isStatic = true;
  1514.                 break;
  1515.             }
  1516.         }//end for
  1517.  
  1518.         return array(
  1519.                 'scope'           => $scope,
  1520.                 'scope_specified' => $scopeSpecified,
  1521.                 'is_static'       => $isStatic,
  1522.                );
  1523.  
  1524.     }//end getMemberProperties()
  1525.  
  1526.  
  1527.     /**
  1528.      * Returns the visibility and implementation properties of a class.
  1529.      *
  1530.      * The format of the array is:
  1531.      * <code>
  1532.      *   array(
  1533.      *    'is_abstract' => false, // true if the abstract keyword was found.
  1534.      *    'is_final'    => false, // true if the final keyword was found.
  1535.      *   );
  1536.      * </code>
  1537.      *
  1538.      * @param int $stackPtr The position in the stack of the T_CLASS token to
  1539.      *                       acquire the properties for.
  1540.      *
  1541.      * @return array 
  1542.      * @throws \PHP_CodeSniffer\Exceptions\TokenizerException If the specified position is not a
  1543.      *                                                         T_CLASS token.
  1544.      */
  1545.     public function getClassProperties($stackPtr)
  1546.     {
  1547.         if ($this->tokens[$stackPtr]['code'!== T_CLASS{
  1548.             throw new TokenizerException('$stackPtr must be of type T_CLASS');
  1549.         }
  1550.  
  1551.         $valid = array(
  1552.                   T_FINAL       => T_FINAL,
  1553.                   T_ABSTRACT    => T_ABSTRACT,
  1554.                   T_WHITESPACE  => T_WHITESPACE,
  1555.                   T_COMMENT     => T_COMMENT,
  1556.                   T_DOC_COMMENT => T_DOC_COMMENT,
  1557.                  );
  1558.  
  1559.         $isAbstract = false;
  1560.         $isFinal    = false;
  1561.  
  1562.         for ($i ($stackPtr - 1)$i > 0; $i--{
  1563.             if (isset($valid[$this->tokens[$i]['code']]=== false{
  1564.                 break;
  1565.             }
  1566.  
  1567.             switch ($this->tokens[$i]['code']{
  1568.             case T_ABSTRACT:
  1569.                 $isAbstract = true;
  1570.                 break;
  1571.  
  1572.             case T_FINAL:
  1573.                 $isFinal = true;
  1574.                 break;
  1575.             }
  1576.         }//end for
  1577.  
  1578.         return array(
  1579.                 'is_abstract' => $isAbstract,
  1580.                 'is_final'    => $isFinal,
  1581.                );
  1582.  
  1583.     }//end getClassProperties()
  1584.  
  1585.  
  1586.     /**
  1587.      * Determine if the passed token is a reference operator.
  1588.      *
  1589.      * Returns true if the specified token position represents a reference.
  1590.      * Returns false if the token represents a bitwise operator.
  1591.      *
  1592.      * @param int $stackPtr The position of the T_BITWISE_AND token.
  1593.      *
  1594.      * @return boolean 
  1595.      */
  1596.     public function isReference($stackPtr)
  1597.     {
  1598.         if ($this->tokens[$stackPtr]['code'!== T_BITWISE_AND{
  1599.             return false;
  1600.         }
  1601.  
  1602.         $tokenBefore $this->findPrevious(
  1603.             Util\Tokens::$emptyTokens,
  1604.             ($stackPtr - 1),
  1605.             null,
  1606.             true
  1607.         );
  1608.  
  1609.         if ($this->tokens[$tokenBefore]['code'=== T_FUNCTION{
  1610.             // Function returns a reference.
  1611.             return true;
  1612.         }
  1613.  
  1614.         if ($this->tokens[$tokenBefore]['code'=== T_DOUBLE_ARROW{
  1615.             // Inside a foreach loop or array assignment, this is a reference.
  1616.             return true;
  1617.         }
  1618.  
  1619.         if ($this->tokens[$tokenBefore]['code'=== T_AS{
  1620.             // Inside a foreach loop, this is a reference.
  1621.             return true;
  1622.         }
  1623.  
  1624.         if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]=== true{
  1625.             // This is directly after an assignment. It's a reference. Even if
  1626.             // it is part of an operation, the other tests will handle it.
  1627.             return true;
  1628.         }
  1629.  
  1630.         $tokenAfter $this->findNext(
  1631.             Util\Tokens::$emptyTokens,
  1632.             ($stackPtr + 1),
  1633.             null,
  1634.             true
  1635.         );
  1636.  
  1637.         if ($this->tokens[$tokenAfter]['code'=== T_NEW{
  1638.             return true;
  1639.         }
  1640.  
  1641.         if (isset($this->tokens[$stackPtr]['nested_parenthesis']=== true{
  1642.             $brackets    $this->tokens[$stackPtr]['nested_parenthesis'];
  1643.             $lastBracket array_pop($brackets);
  1644.             if (isset($this->tokens[$lastBracket]['parenthesis_owner']=== true{
  1645.                 $owner $this->tokens[$this->tokens[$lastBracket]['parenthesis_owner']];
  1646.                 if ($owner['code'=== T_FUNCTION
  1647.                     || $owner['code'=== T_CLOSURE
  1648.                 {
  1649.                     $params $this->getMethodParameters($this->tokens[$lastBracket]['parenthesis_owner']);
  1650.                     foreach ($params as $param{
  1651.                         $varToken $tokenAfter;
  1652.                         if ($param['variable_length'=== true{
  1653.                             $varToken $this->findNext(
  1654.                                 (Util\Tokens::$emptyTokens + array(T_ELLIPSIS)),
  1655.                                 ($stackPtr + 1),
  1656.                                 null,
  1657.                                 true
  1658.                             );
  1659.                         }
  1660.  
  1661.                         if ($param['token'=== $varToken
  1662.                             && $param['pass_by_reference'=== true
  1663.                         {
  1664.                             // Function parameter declared to be passed by reference.
  1665.                             return true;
  1666.                         }
  1667.                     }
  1668.                 }//end if
  1669.             else {
  1670.                 $prev = false;
  1671.                 for ($t ($this->tokens[$lastBracket]['parenthesis_opener'- 1)$t >= 0; $t--{
  1672.                     if ($this->tokens[$t]['code'!== T_WHITESPACE{
  1673.                         $prev $t;
  1674.                         break;
  1675.                     }
  1676.                 }
  1677.  
  1678.                 if ($prev !== false && $this->tokens[$prev]['code'=== T_USE{
  1679.                     // Closure use by reference.
  1680.                     return true;
  1681.                 }
  1682.             }//end if
  1683.         }//end if
  1684.  
  1685.         // Pass by reference in function calls and assign by reference in arrays.
  1686.         if ($this->tokens[$tokenBefore]['code'=== T_OPEN_PARENTHESIS
  1687.             || $this->tokens[$tokenBefore]['code'=== T_COMMA
  1688.             || $this->tokens[$tokenBefore]['code'=== T_OPEN_SHORT_ARRAY
  1689.         {
  1690.             if ($this->tokens[$tokenAfter]['code'=== T_VARIABLE{
  1691.                 return true;
  1692.             else {
  1693.                 $skip   = Util\Tokens::$emptyTokens;
  1694.                 $skip[= T_NS_SEPARATOR;
  1695.                 $skip[T_SELF;
  1696.                 $skip[T_PARENT;
  1697.                 $skip[= T_STATIC;
  1698.                 $skip[= T_STRING;
  1699.                 $skip[= T_NAMESPACE;
  1700.                 $skip[= T_DOUBLE_COLON;
  1701.  
  1702.                 $nextSignificantAfter $this->findNext(
  1703.                     $skip,
  1704.                     ($stackPtr + 1),
  1705.                     null,
  1706.                     true
  1707.                 );
  1708.                 if ($this->tokens[$nextSignificantAfter]['code'=== T_VARIABLE{
  1709.                     return true;
  1710.                 }
  1711.             }//end if
  1712.         }//end if
  1713.  
  1714.         return false;
  1715.  
  1716.     }//end isReference()
  1717.  
  1718.  
  1719.     /**
  1720.      * Returns the content of the tokens from the specified start position in
  1721.      * the token stack for the specified length.
  1722.      *
  1723.      * @param int $start  The position to start from in the token stack.
  1724.      * @param int $length The length of tokens to traverse from the start pos.
  1725.      *
  1726.      * @return string The token contents.
  1727.      */
  1728.     public function getTokensAsString($start$length)
  1729.     {
  1730.         $str '';
  1731.         $end ($start $length);
  1732.         if ($end $this->numTokens{
  1733.             $end $this->numTokens;
  1734.         }
  1735.  
  1736.         for ($i $start$i $end$i++{
  1737.             $str .= $this->tokens[$i]['content'];
  1738.         }
  1739.  
  1740.         return $str;
  1741.  
  1742.     }//end getTokensAsString()
  1743.  
  1744.  
  1745.     /**
  1746.      * Returns the position of the previous specified token(s).
  1747.      *
  1748.      * If a value is specified, the previous token of the specified type(s)
  1749.      * containing the specified value will be returned.
  1750.      *
  1751.      * Returns false if no token can be found.
  1752.      *
  1753.      * @param int|array$types   The type(s) of tokens to search for.
  1754.      * @param int       $start   The position to start searching from in the
  1755.      *                            token stack.
  1756.      * @param int       $end     The end position to fail if no token is found.
  1757.      *                            if not specified or null, end will default to
  1758.      *                            the start of the token stack.
  1759.      * @param bool      $exclude If true, find the previous token that is NOT of
  1760.      *                            the types specified in $types.
  1761.      * @param string    $value   The value that the token(s) must be equal to.
  1762.      *                            If value is omitted, tokens with any value will
  1763.      *                            be returned.
  1764.      * @param bool      $local   If true, tokens outside the current statement
  1765.      *                            will not be checked. IE. checking will stop
  1766.      *                            at the previous semi-colon found.
  1767.      *
  1768.      * @return int|bool
  1769.      * @see    findNext()
  1770.      */
  1771.     public function findPrevious(
  1772.         $types,
  1773.         $start,
  1774.         $end=null,
  1775.         $exclude=false,
  1776.         $value=null,
  1777.         $local=false
  1778.     {
  1779.         $types = (array) $types;
  1780.  
  1781.         if ($end === null{
  1782.             $end = 0;
  1783.         }
  1784.  
  1785.         for ($i $start$i >= $end$i--{
  1786.             $found = (bool) $exclude;
  1787.             foreach ($types as $type{
  1788.                 if ($this->tokens[$i]['code'=== $type{
  1789.                     $found !$exclude;
  1790.                     break;
  1791.                 }
  1792.             }
  1793.  
  1794.             if ($found === true{
  1795.                 if ($value === null{
  1796.                     return $i;
  1797.                 else if ($this->tokens[$i]['content'=== $value{
  1798.                     return $i;
  1799.                 }
  1800.             }
  1801.  
  1802.             if ($local === true{
  1803.                 if (isset($this->tokens[$i]['scope_opener']=== true
  1804.                     && $i === $this->tokens[$i]['scope_closer']
  1805.                 {
  1806.                     $i $this->tokens[$i]['scope_opener'];
  1807.                 else if (isset($this->tokens[$i]['bracket_opener']=== true
  1808.                     && $i === $this->tokens[$i]['bracket_closer']
  1809.                 {
  1810.                     $i $this->tokens[$i]['bracket_opener'];
  1811.                 else if (isset($this->tokens[$i]['parenthesis_opener']=== true
  1812.                     && $i === $this->tokens[$i]['parenthesis_closer']
  1813.                 {
  1814.                     $i $this->tokens[$i]['parenthesis_opener'];
  1815.                 else if ($this->tokens[$i]['code'=== T_SEMICOLON{
  1816.                     break;
  1817.                 }
  1818.             }
  1819.         }//end for
  1820.  
  1821.         return false;
  1822.  
  1823.     }//end findPrevious()
  1824.  
  1825.  
  1826.     /**
  1827.      * Returns the position of the next specified token(s).
  1828.      *
  1829.      * If a value is specified, the next token of the specified type(s)
  1830.      * containing the specified value will be returned.
  1831.      *
  1832.      * Returns false if no token can be found.
  1833.      *
  1834.      * @param int|array$types   The type(s) of tokens to search for.
  1835.      * @param int       $start   The position to start searching from in the
  1836.      *                            token stack.
  1837.      * @param int       $end     The end position to fail if no token is found.
  1838.      *                            if not specified or null, end will default to
  1839.      *                            the end of the token stack.
  1840.      * @param bool      $exclude If true, find the next token that is NOT of
  1841.      *                            a type specified in $types.
  1842.      * @param string    $value   The value that the token(s) must be equal to.
  1843.      *                            If value is omitted, tokens with any value will
  1844.      *                            be returned.
  1845.      * @param bool      $local   If true, tokens outside the current statement
  1846.      *                            will not be checked. i.e., checking will stop
  1847.      *                            at the next semi-colon found.
  1848.      *
  1849.      * @return int|bool
  1850.      * @see    findPrevious()
  1851.      */
  1852.     public function findNext(
  1853.         $types,
  1854.         $start,
  1855.         $end=null,
  1856.         $exclude=false,
  1857.         $value=null,
  1858.         $local=false
  1859.     {
  1860.         $types = (array) $types;
  1861.  
  1862.         if ($end === null || $end $this->numTokens{
  1863.             $end $this->numTokens;
  1864.         }
  1865.  
  1866.         for ($i $start$i $end$i++{
  1867.             $found = (bool) $exclude;
  1868.             foreach ($types as $type{
  1869.                 if ($this->tokens[$i]['code'=== $type{
  1870.                     $found !$exclude;
  1871.                     break;
  1872.                 }
  1873.             }
  1874.  
  1875.             if ($found === true{
  1876.                 if ($value === null{
  1877.                     return $i;
  1878.                 else if ($this->tokens[$i]['content'=== $value{
  1879.                     return $i;
  1880.                 }
  1881.             }
  1882.  
  1883.             if ($local === true && $this->tokens[$i]['code'=== T_SEMICOLON{
  1884.                 break;
  1885.             }
  1886.         }//end for
  1887.  
  1888.         return false;
  1889.  
  1890.     }//end findNext()
  1891.  
  1892.  
  1893.     /**
  1894.      * Returns the position of the first non-whitespace token in a statement.
  1895.      *
  1896.      * @param int       $start  The position to start searching from in the token stack.
  1897.      * @param int|array$ignore Token types that should not be considered stop points.
  1898.      *
  1899.      * @return int 
  1900.      */
  1901.     public function findStartOfStatement($start$ignore=null)
  1902.     {
  1903.         $endTokens = Util\Tokens::$blockOpeners;
  1904.  
  1905.         $endTokens[T_COLON]            = true;
  1906.         $endTokens[T_COMMA]            = true;
  1907.         $endTokens[T_DOUBLE_ARROW]     = true;
  1908.         $endTokens[T_SEMICOLON]        = true;
  1909.         $endTokens[T_OPEN_TAG]         = true;
  1910.         $endTokens[T_CLOSE_TAG]        = true;
  1911.         $endTokens[T_OPEN_SHORT_ARRAY= true;
  1912.  
  1913.         if ($ignore !== null{
  1914.             $ignore = (array) $ignore;
  1915.             foreach ($ignore as $code{
  1916.                 if (isset($endTokens[$code]=== true{
  1917.                     unset($endTokens[$code]);
  1918.                 }
  1919.             }
  1920.         }
  1921.  
  1922.         $lastNotEmpty $start;
  1923.  
  1924.         for ($i $start$i >= 0; $i--{
  1925.             if (isset($endTokens[$this->tokens[$i]['code']]=== true{
  1926.                 // Found the end of the previous statement.
  1927.                 return $lastNotEmpty;
  1928.             }
  1929.  
  1930.             if (isset($this->tokens[$i]['scope_opener']=== true
  1931.                 && $i === $this->tokens[$i]['scope_closer']
  1932.             {
  1933.                 // Found the end of the previous scope block.
  1934.                 return $lastNotEmpty;
  1935.             }
  1936.  
  1937.             // Skip nested statements.
  1938.             if (isset($this->tokens[$i]['bracket_opener']=== true
  1939.                 && $i === $this->tokens[$i]['bracket_closer']
  1940.             {
  1941.                 $i $this->tokens[$i]['bracket_opener'];
  1942.             else if (isset($this->tokens[$i]['parenthesis_opener']=== true
  1943.                 && $i === $this->tokens[$i]['parenthesis_closer']
  1944.             {
  1945.                 $i $this->tokens[$i]['parenthesis_opener'];
  1946.             }
  1947.  
  1948.             if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]=== false{
  1949.                 $lastNotEmpty $i;
  1950.             }
  1951.         }//end for
  1952.  
  1953.         return 0;
  1954.  
  1955.     }//end findStartOfStatement()
  1956.  
  1957.  
  1958.     /**
  1959.      * Returns the position of the last non-whitespace token in a statement.
  1960.      *
  1961.      * @param int       $start  The position to start searching from in the token stack.
  1962.      * @param int|array$ignore Token types that should not be considered stop points.
  1963.      *
  1964.      * @return int 
  1965.      */
  1966.     public function findEndOfStatement($start$ignore=null)
  1967.     {
  1968.         $endTokens = array(
  1969.                       T_COLON                => true,
  1970.                       T_COMMA                => true,
  1971.                       T_DOUBLE_ARROW         => true,
  1972.                       T_SEMICOLON            => true,
  1973.                       T_CLOSE_PARENTHESIS    => true,
  1974.                       T_CLOSE_SQUARE_BRACKET => true,
  1975.                       T_CLOSE_CURLY_BRACKET  => true,
  1976.                       T_CLOSE_SHORT_ARRAY    => true,
  1977.                       T_OPEN_TAG             => true,
  1978.                       T_CLOSE_TAG            => true,
  1979.                      );
  1980.  
  1981.         if ($ignore !== null{
  1982.             $ignore = (array) $ignore;
  1983.             foreach ($ignore as $code{
  1984.                 if (isset($endTokens[$code]=== true{
  1985.                     unset($endTokens[$code]);
  1986.                 }
  1987.             }
  1988.         }
  1989.  
  1990.         $lastNotEmpty $start;
  1991.  
  1992.         for ($i $start$i $this->numTokens$i++{
  1993.             if ($i !== $start && isset($endTokens[$this->tokens[$i]['code']]=== true{
  1994.                 // Found the end of the statement.
  1995.                 if ($this->tokens[$i]['code'=== T_CLOSE_PARENTHESIS
  1996.                     || $this->tokens[$i]['code'=== T_CLOSE_SQUARE_BRACKET
  1997.                     || $this->tokens[$i]['code'=== T_CLOSE_CURLY_BRACKET
  1998.                     || $this->tokens[$i]['code'=== T_CLOSE_SHORT_ARRAY
  1999.                     || $this->tokens[$i]['code'=== T_OPEN_TAG
  2000.                     || $this->tokens[$i]['code'=== T_CLOSE_TAG
  2001.                 {
  2002.                     return $lastNotEmpty;
  2003.                 }
  2004.  
  2005.                 return $i;
  2006.             }
  2007.  
  2008.             // Skip nested statements.
  2009.             if (isset($this->tokens[$i]['scope_closer']=== true
  2010.                 && ($i === $this->tokens[$i]['scope_opener']
  2011.                 || $i === $this->tokens[$i]['scope_condition'])
  2012.             {
  2013.                 $i $this->tokens[$i]['scope_closer'];
  2014.             else if (isset($this->tokens[$i]['bracket_closer']=== true
  2015.                 && $i === $this->tokens[$i]['bracket_opener']
  2016.             {
  2017.                 $i $this->tokens[$i]['bracket_closer'];
  2018.             else if (isset($this->tokens[$i]['parenthesis_closer']=== true
  2019.                 && $i === $this->tokens[$i]['parenthesis_opener']
  2020.             {
  2021.                 $i $this->tokens[$i]['parenthesis_closer'];
  2022.             }
  2023.  
  2024.             if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]=== false{
  2025.                 $lastNotEmpty $i;
  2026.             }
  2027.         }//end for
  2028.  
  2029.         return ($this->numTokens - 1);
  2030.  
  2031.     }//end findEndOfStatement()
  2032.  
  2033.  
  2034.     /**
  2035.      * Returns the position of the first token on a line, matching given type.
  2036.      *
  2037.      * Returns false if no token can be found.
  2038.      *
  2039.      * @param int|array$types   The type(s) of tokens to search for.
  2040.      * @param int       $start   The position to start searching from in the
  2041.      *                            token stack. The first token matching on
  2042.      *                            this line before this token will be returned.
  2043.      * @param bool      $exclude If true, find the token that is NOT of
  2044.      *                            the types specified in $types.
  2045.      * @param string    $value   The value that the token must be equal to.
  2046.      *                            If value is omitted, tokens with any value will
  2047.      *                            be returned.
  2048.      *
  2049.      * @return int | bool
  2050.      */
  2051.     public function findFirstOnLine($types$start$exclude=false$value=null)
  2052.     {
  2053.         if (is_array($types=== false{
  2054.             $types = array($types);
  2055.         }
  2056.  
  2057.         $foundToken = false;
  2058.  
  2059.         for ($i $start$i >= 0; $i--{
  2060.             if ($this->tokens[$i]['line'$this->tokens[$start]['line']{
  2061.                 break;
  2062.             }
  2063.  
  2064.             $found $exclude;
  2065.             foreach ($types as $type{
  2066.                 if ($exclude === false{
  2067.                     if ($this->tokens[$i]['code'=== $type{
  2068.                         $found = true;
  2069.                         break;
  2070.                     }
  2071.                 else {
  2072.                     if ($this->tokens[$i]['code'=== $type{
  2073.                         $found = false;
  2074.                         break;
  2075.                     }
  2076.                 }
  2077.             }
  2078.  
  2079.             if ($found === true{
  2080.                 if ($value === null{
  2081.                     $foundToken $i;
  2082.                 else if ($this->tokens[$i]['content'=== $value{
  2083.                     $foundToken $i;
  2084.                 }
  2085.             }
  2086.         }//end for
  2087.  
  2088.         return $foundToken;
  2089.  
  2090.     }//end findFirstOnLine()
  2091.  
  2092.  
  2093.     /**
  2094.      * Determine if the passed token has a condition of one of the passed types.
  2095.      *
  2096.      * @param int       $stackPtr The position of the token we are checking.
  2097.      * @param int|array$types    The type(s) of tokens to search for.
  2098.      *
  2099.      * @return boolean 
  2100.      */
  2101.     public function hasCondition($stackPtr$types)
  2102.     {
  2103.         // Check for the existence of the token.
  2104.         if (isset($this->tokens[$stackPtr]=== false{
  2105.             return false;
  2106.         }
  2107.  
  2108.         // Make sure the token has conditions.
  2109.         if (isset($this->tokens[$stackPtr]['conditions']=== false{
  2110.             return false;
  2111.         }
  2112.  
  2113.         $types      = (array) $types;
  2114.         $conditions $this->tokens[$stackPtr]['conditions'];
  2115.  
  2116.         foreach ($types as $type{
  2117.             if (in_array($type$conditions=== true{
  2118.                 // We found a token with the required type.
  2119.                 return true;
  2120.             }
  2121.         }
  2122.  
  2123.         return false;
  2124.  
  2125.     }//end hasCondition()
  2126.  
  2127.  
  2128.     /**
  2129.      * Return the position of the condition for the passed token.
  2130.      *
  2131.      * Returns FALSE if the token does not have the condition.
  2132.      *
  2133.      * @param int $stackPtr The position of the token we are checking.
  2134.      * @param int $type     The type of token to search for.
  2135.      *
  2136.      * @return int 
  2137.      */
  2138.     public function getCondition($stackPtr$type)
  2139.     {
  2140.         // Check for the existence of the token.
  2141.         if (isset($this->tokens[$stackPtr]=== false{
  2142.             return false;
  2143.         }
  2144.  
  2145.         // Make sure the token has conditions.
  2146.         if (isset($this->tokens[$stackPtr]['conditions']=== false{
  2147.             return false;
  2148.         }
  2149.  
  2150.         $conditions $this->tokens[$stackPtr]['conditions'];
  2151.         foreach ($conditions as $token => $condition{
  2152.             if ($condition === $type{
  2153.                 return $token;
  2154.             }
  2155.         }
  2156.  
  2157.         return false;
  2158.  
  2159.     }//end getCondition()
  2160.  
  2161.  
  2162.     /**
  2163.      * Returns the name of the class that the specified class extends.
  2164.      * (works for classes, anonymous classes and interfaces)
  2165.      *
  2166.      * Returns FALSE on error or if there is no extended class name.
  2167.      *
  2168.      * @param int $stackPtr The stack position of the class.
  2169.      *
  2170.      * @return string|false
  2171.      */
  2172.     public function findExtendedClassName($stackPtr)
  2173.     {
  2174.         // Check for the existence of the token.
  2175.         if (isset($this->tokens[$stackPtr]=== false{
  2176.             return false;
  2177.         }
  2178.  
  2179.         if ($this->tokens[$stackPtr]['code'!== T_CLASS
  2180.             && $this->tokens[$stackPtr]['code'!== T_ANON_CLASS
  2181.             && $this->tokens[$stackPtr]['code'!== T_INTERFACE
  2182.         {
  2183.             return false;
  2184.         }
  2185.  
  2186.         if (isset($this->tokens[$stackPtr]['scope_closer']=== false{
  2187.             return false;
  2188.         }
  2189.  
  2190.         $classCloserIndex $this->tokens[$stackPtr]['scope_closer'];
  2191.         $extendsIndex     $this->findNext(T_EXTENDS$stackPtr$classCloserIndex);
  2192.         if (false === $extendsIndex{
  2193.             return false;
  2194.         }
  2195.  
  2196.         $find = array(
  2197.                  T_NS_SEPARATOR,
  2198.                  T_STRING,
  2199.                  T_WHITESPACE,
  2200.                 );
  2201.  
  2202.         $end  $this->findNext($find($extendsIndex + 1)$classCloserIndextrue);
  2203.         $name $this->getTokensAsString(($extendsIndex + 1)($end $extendsIndex - 1));
  2204.         $name trim($name);
  2205.  
  2206.         if ($name === ''{
  2207.             return false;
  2208.         }
  2209.  
  2210.         return $name;
  2211.  
  2212.     }//end findExtendedClassName()
  2213.  
  2214.  
  2215.     /**
  2216.      * Returns the names of the interfaces that the specified class implements.
  2217.      *
  2218.      * Returns FALSE on error or if there are no implemented interface names.
  2219.      *
  2220.      * @param int $stackPtr The stack position of the class.
  2221.      *
  2222.      * @return array|false
  2223.      */
  2224.     public function findImplementedInterfaceNames($stackPtr)
  2225.     {
  2226.         // Check for the existence of the token.
  2227.         if (isset($this->tokens[$stackPtr]=== false{
  2228.             return false;
  2229.         }
  2230.  
  2231.         if ($this->tokens[$stackPtr]['code'!== T_CLASS
  2232.             && $this->tokens[$stackPtr]['code'!== T_ANON_CLASS
  2233.         {
  2234.             return false;
  2235.         }
  2236.  
  2237.         if (isset($this->tokens[$stackPtr]['scope_closer']=== false{
  2238.             return false;
  2239.         }
  2240.  
  2241.         $classOpenerIndex $this->tokens[$stackPtr]['scope_opener'];
  2242.         $implementsIndex  $this->findNext(T_IMPLEMENTS$stackPtr$classOpenerIndex);
  2243.         if ($implementsIndex === false{
  2244.             return false;
  2245.         }
  2246.  
  2247.         $find = array(
  2248.                  T_NS_SEPARATOR,
  2249.                  T_STRING,
  2250.                  T_WHITESPACE,
  2251.                  T_COMMA,
  2252.                 );
  2253.  
  2254.         $end  $this->findNext($find($implementsIndex + 1)($classOpenerIndex + 1)true);
  2255.         $name $this->getTokensAsString(($implementsIndex + 1)($end $implementsIndex - 1));
  2256.         $name trim($name);
  2257.  
  2258.         if ($name === ''{
  2259.             return false;
  2260.         else {
  2261.             $names explode(','$name);
  2262.             $names array_map('trim'$names);
  2263.             return $names;
  2264.         }
  2265.  
  2266.     }//end findImplementedInterfaceNames()
  2267.  
  2268.  
  2269. }//end class

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