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

Source for file LocalFile.php

Documentation is available at LocalFile.php

  1. <?php
  2. /**
  3.  * A local file represents a chunk of text has a file system location.
  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\Util\Cache;
  15.  
  16. class LocalFile extends File
  17. {
  18.  
  19.  
  20.     /**
  21.      * Creates a LocalFile object and sets the content.
  22.      *
  23.      * @param string                   $path    The absolute path to the file.
  24.      * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
  25.      * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
  26.      *
  27.      * @return void 
  28.      */
  29.     public function __construct($pathRuleset $rulesetConfig $config)
  30.     {
  31.         $path trim($path);
  32.         if (is_readable($path=== false{
  33.             parent::__construct($path$ruleset$config);
  34.             $error 'Error opening file; file no longer exists or you do not have access to read the file';
  35.             $this->addMessage(true$error11'Internal.LocalFile'array()5false);
  36.             $this->ignored = true;
  37.             return;
  38.         }
  39.  
  40.         // Before we go and spend time tokenizing this file, just check
  41.         // to see if there is a tag up top to indicate that the whole
  42.         // file should be ignored. It must be on one of the first two lines.
  43.         $handle fopen($path'r');
  44.         if ($handle !== false{
  45.             $firstContent  fgets($handle);
  46.             $firstContent .= fgets($handle);
  47.             fclose($handle);
  48.  
  49.             if (strpos($firstContent'@codingStandardsIgnoreFile'!== false{
  50.                 // We are ignoring the whole file.
  51.                 if (PHP_CODESNIFFER_VERBOSITY > 0{
  52.                     echo 'Ignoring '.basename($path).PHP_EOL;
  53.                 }
  54.  
  55.                 $this->ignored = true;
  56.                 return;
  57.             }
  58.         }
  59.  
  60.         $this->path $path;
  61.         $this->reloadContent();
  62.  
  63.         return parent::__construct($path$ruleset$config);
  64.  
  65.     }//end __construct()
  66.  
  67.  
  68.     /**
  69.      * Loads the latest version of the file's content from the file system.
  70.      *
  71.      * @return void 
  72.      */
  73.     function reloadContent()
  74.     {
  75.         $this->setContent(file_get_contents($this->path));
  76.  
  77.     }//end reloadContent()
  78.  
  79.  
  80.     /**
  81.      * Processes the file.
  82.      *
  83.      * @return void 
  84.      */
  85.     public function process()
  86.     {
  87.         if ($this->ignored === true{
  88.             return;
  89.         }
  90.  
  91.         if ($this->configCache['cache'=== false{
  92.             return parent::process();
  93.         }
  94.  
  95.         $hash  md5_file($this->path);
  96.         $cache = Cache::get($this->path);
  97.         if ($cache !== false && $cache['hash'=== $hash{
  98.             // We can't filter metrics, so just load all of them.
  99.             $this->metrics $cache['metrics'];
  100.  
  101.             if ($this->configCache['recordErrors'=== true{
  102.                 // Replay the cached errors and warnings to filter out the ones
  103.                 // we don't need for this specific run.
  104.                 $this->configCache['cache'= false;
  105.                 $this->replayErrors($cache['errors']$cache['warnings']);
  106.                 $this->configCache['cache'= true;
  107.             else {
  108.                 $this->errorCount   $cache['errorCount'];
  109.                 $this->warningCount $cache['warningCount'];
  110.                 $this->fixableCount $cache['fixableCount'];
  111.             }
  112.  
  113.             if (PHP_CODESNIFFER_VERBOSITY > 0
  114.                 || (PHP_CODESNIFFER_CBF === true && empty($this->config->files=== false)
  115.             {
  116.                 echo "[loaded from cache]... ";
  117.             }
  118.  
  119.             $this->numTokens $cache['numTokens'];
  120.             $this->fromCache = true;
  121.             return;
  122.         }//end if
  123.  
  124.         if (PHP_CODESNIFFER_VERBOSITY > 1{
  125.             echo PHP_EOL;
  126.         }
  127.  
  128.         parent::process();
  129.  
  130.         $cache = array(
  131.                   'hash'         => $hash,
  132.                   'errors'       => $this->errors,
  133.                   'warnings'     => $this->warnings,
  134.                   'metrics'      => $this->metrics,
  135.                   'errorCount'   => $this->errorCount,
  136.                   'warningCount' => $this->warningCount,
  137.                   'fixableCount' => $this->fixableCount,
  138.                   'numTokens'    => $this->numTokens,
  139.                  );
  140.  
  141.         Cache::set($this->path$cache);
  142.  
  143.         // During caching, we don't filter out errors in any way, so
  144.         // we need to do that manually now by replaying them.
  145.         if ($this->configCache['recordErrors'=== true{
  146.             $this->configCache['cache'= false;
  147.             $this->replayErrors($this->errors$this->warnings);
  148.             $this->configCache['cache'= true;
  149.         }
  150.  
  151.     }//end process()
  152.  
  153.  
  154.     /**
  155.      * Clears and replays error and warnings for the file.
  156.      *
  157.      * Replaying errors and warnings allows for filtering rules to be changed
  158.      * and then errors and warnings to be reapplied with the new rules. This is
  159.      * particularly useful while caching.
  160.      *
  161.      * @param array $errors   The list of errors to replay.
  162.      * @param array $warnings The list of warnings to replay.
  163.      *
  164.      * @return void 
  165.      */
  166.     private function replayErrors($errors$warnings)
  167.     {
  168.         $this->errors       = array();
  169.         $this->warnings     = array();
  170.         $this->errorCount   = 0;
  171.         $this->warningCount = 0;
  172.         $this->fixableCount = 0;
  173.  
  174.         foreach ($errors as $line => $lineErrors{
  175.             foreach ($lineErrors as $column => $colErrors{
  176.                 foreach ($colErrors as $error{
  177.                     $this->activeListener $error['listener'];
  178.                     $this->addMessage(
  179.                         true,
  180.                         $error['message'],
  181.                         $line,
  182.                         $column,
  183.                         $error['source'],
  184.                         array(),
  185.                         $error['severity'],
  186.                         $error['fixable']
  187.                     );
  188.                 }
  189.             }
  190.         }
  191.  
  192.         foreach ($warnings as $line => $lineErrors{
  193.             foreach ($lineErrors as $column => $colErrors{
  194.                 foreach ($colErrors as $error{
  195.                     $this->activeListener $error['listener'];
  196.                     $this->addMessage(
  197.                         false,
  198.                         $error['message'],
  199.                         $line,
  200.                         $column,
  201.                         $error['source'],
  202.                         array(),
  203.                         $error['severity'],
  204.                         $error['fixable']
  205.                     );
  206.                 }
  207.             }
  208.         }
  209.  
  210.     }//end replayErrors()
  211.  
  212.  
  213. }//end class

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