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.         $this->path trim($path);
  32.         if (is_readable($this->path=== false{
  33.             parent::__construct($this->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.         if ($config->annotations === true{
  44.             $handle fopen($this->path'r');
  45.             if ($handle !== false{
  46.                 $firstContent  fgets($handle);
  47.                 $firstContent .= fgets($handle);
  48.                 fclose($handle);
  49.  
  50.                 if (strpos($firstContent'@codingStandardsIgnoreFile'!== false{
  51.                     // We are ignoring the whole file.
  52.                     $this->ignored = true;
  53.                     return;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         $this->reloadContent();
  59.  
  60.         return parent::__construct($this->path$ruleset$config);
  61.  
  62.     }//end __construct()
  63.  
  64.  
  65.     /**
  66.      * Loads the latest version of the file's content from the file system.
  67.      *
  68.      * @return void 
  69.      */
  70.     function reloadContent()
  71.     {
  72.         $this->setContent(file_get_contents($this->path));
  73.  
  74.     }//end reloadContent()
  75.  
  76.  
  77.     /**
  78.      * Processes the file.
  79.      *
  80.      * @return void 
  81.      */
  82.     public function process()
  83.     {
  84.         if ($this->ignored === true{
  85.             return;
  86.         }
  87.  
  88.         if ($this->configCache['cache'=== false{
  89.             return parent::process();
  90.         }
  91.  
  92.         $hash  md5_file($this->path);
  93.         $cache = Cache::get($this->path);
  94.         if ($cache !== false && $cache['hash'=== $hash{
  95.             // We can't filter metrics, so just load all of them.
  96.             $this->metrics $cache['metrics'];
  97.  
  98.             if ($this->configCache['recordErrors'=== true{
  99.                 // Replay the cached errors and warnings to filter out the ones
  100.                 // we don't need for this specific run.
  101.                 $this->configCache['cache'= false;
  102.                 $this->replayErrors($cache['errors']$cache['warnings']);
  103.                 $this->configCache['cache'= true;
  104.             else {
  105.                 $this->errorCount   $cache['errorCount'];
  106.                 $this->warningCount $cache['warningCount'];
  107.                 $this->fixableCount $cache['fixableCount'];
  108.             }
  109.  
  110.             if (PHP_CODESNIFFER_VERBOSITY > 0
  111.                 || (PHP_CODESNIFFER_CBF === true && empty($this->config->files=== false)
  112.             {
  113.                 echo "[loaded from cache]... ";
  114.             }
  115.  
  116.             $this->numTokens $cache['numTokens'];
  117.             $this->fromCache = true;
  118.             return;
  119.         }//end if
  120.  
  121.         if (PHP_CODESNIFFER_VERBOSITY > 1{
  122.             echo PHP_EOL;
  123.         }
  124.  
  125.         parent::process();
  126.  
  127.         $cache = array(
  128.                   'hash'         => $hash,
  129.                   'errors'       => $this->errors,
  130.                   'warnings'     => $this->warnings,
  131.                   'metrics'      => $this->metrics,
  132.                   'errorCount'   => $this->errorCount,
  133.                   'warningCount' => $this->warningCount,
  134.                   'fixableCount' => $this->fixableCount,
  135.                   'numTokens'    => $this->numTokens,
  136.                  );
  137.  
  138.         Cache::set($this->path$cache);
  139.  
  140.         // During caching, we don't filter out errors in any way, so
  141.         // we need to do that manually now by replaying them.
  142.         if ($this->configCache['recordErrors'=== true{
  143.             $this->configCache['cache'= false;
  144.             $this->replayErrors($this->errors$this->warnings);
  145.             $this->configCache['cache'= true;
  146.         }
  147.  
  148.     }//end process()
  149.  
  150.  
  151.     /**
  152.      * Clears and replays error and warnings for the file.
  153.      *
  154.      * Replaying errors and warnings allows for filtering rules to be changed
  155.      * and then errors and warnings to be reapplied with the new rules. This is
  156.      * particularly useful while caching.
  157.      *
  158.      * @param array $errors   The list of errors to replay.
  159.      * @param array $warnings The list of warnings to replay.
  160.      *
  161.      * @return void 
  162.      */
  163.     private function replayErrors($errors$warnings)
  164.     {
  165.         $this->errors       = array();
  166.         $this->warnings     = array();
  167.         $this->errorCount   = 0;
  168.         $this->warningCount = 0;
  169.         $this->fixableCount = 0;
  170.  
  171.         foreach ($errors as $line => $lineErrors{
  172.             foreach ($lineErrors as $column => $colErrors{
  173.                 foreach ($colErrors as $error{
  174.                     $this->activeListener $error['listener'];
  175.                     $this->addMessage(
  176.                         true,
  177.                         $error['message'],
  178.                         $line,
  179.                         $column,
  180.                         $error['source'],
  181.                         array(),
  182.                         $error['severity'],
  183.                         $error['fixable']
  184.                     );
  185.                 }
  186.             }
  187.         }
  188.  
  189.         foreach ($warnings as $line => $lineErrors{
  190.             foreach ($lineErrors as $column => $colErrors{
  191.                 foreach ($colErrors as $error{
  192.                     $this->activeListener $error['listener'];
  193.                     $this->addMessage(
  194.                         false,
  195.                         $error['message'],
  196.                         $line,
  197.                         $column,
  198.                         $error['source'],
  199.                         array(),
  200.                         $error['severity'],
  201.                         $error['fixable']
  202.                     );
  203.                 }
  204.             }
  205.         }
  206.  
  207.     }//end replayErrors()
  208.  
  209.  
  210. }//end class

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