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

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