Skip to content

Commit

Permalink
Allow phpcbf to use the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed May 13, 2024
1 parent 4fd52f7 commit 1815326
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
37 changes: 21 additions & 16 deletions src/Files/LocalFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public function process()
$hash = md5_file($this->path);
$hash .= fileperms($this->path);
$cache = Cache::get($this->path);
if ($cache !== false && $cache['hash'] === $hash) {

if ($cache !== false && $cache['hash'] === $hash
&& (PHP_CODESNIFFER_CBF === false || $cache['fixableCount'] === 0)
) {
// We can't filter metrics, so just load all of them.
$this->metrics = $cache['metrics'];

Expand All @@ -112,9 +115,7 @@ public function process()
$this->fixableCount = $cache['fixableCount'];
}

if (PHP_CODESNIFFER_VERBOSITY > 0
|| (PHP_CODESNIFFER_CBF === true && empty($this->config->files) === false)
) {
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo "[loaded from cache]... ";
}

Expand All @@ -129,18 +130,22 @@ public function process()

parent::process();

$cache = [
'hash' => $hash,
'errors' => $this->errors,
'warnings' => $this->warnings,
'metrics' => $this->metrics,
'errorCount' => $this->errorCount,
'warningCount' => $this->warningCount,
'fixableCount' => $this->fixableCount,
'numTokens' => $this->numTokens,
];

Cache::set($this->path, $cache);
if (PHP_CODESNIFFER_CBF === false || $this->fixedCount === 0) {
$cache = [
'hash' => $hash,
'errors' => $this->errors,
'warnings' => $this->warnings,
'metrics' => $this->metrics,
'errorCount' => $this->errorCount,
'warningCount' => $this->warningCount,
'fixableCount' => $this->fixableCount,
'numTokens' => $this->numTokens,
];

Cache::set($this->path, $cache);
} else {
Cache::set($this->path, false);
}

// During caching, we don't filter out errors in any way, so
// we need to do that manually now by replaying them.
Expand Down
2 changes: 2 additions & 0 deletions src/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public function fixFile()
return false;
}

$this->currentFile->disableCaching();

$this->enabled = true;

$this->loops = 0;
Expand Down
12 changes: 5 additions & 7 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ public function runPHPCBF()
}

// Override some of the command line settings that might break the fixes.
$this->config->generator = null;
$this->config->explain = false;
$this->config->interactive = false;
$this->config->cache = false;
$this->config->showSources = false;
$this->config->recordErrors = false;
$this->config->reportFile = null;
$this->config->generator = null;
$this->config->explain = false;
$this->config->interactive = false;
$this->config->showSources = false;
$this->config->reportFile = null;

// Only use the "Cbf" report, but allow for the Performance report as well.
$originalReports = array_change_key_case($this->config->reports, CASE_LOWER);
Expand Down

0 comments on commit 1815326

Please sign in to comment.