Skip to content

Commit 9c56b39

Browse files
committed
Command for clearing result cache
1 parent 0345d3c commit 9c56b39

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

bin/phpstan

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<?php declare(strict_types=1);
33

44
use PHPStan\Command\AnalyseCommand;
5+
use PHPStan\Command\ClearResultCacheCommand;
56
use PHPStan\Command\DumpDependenciesCommand;
67
use PHPStan\Command\WorkerCommand;
78

@@ -75,5 +76,6 @@ use PHPStan\Command\WorkerCommand;
7576
$application->add(new AnalyseCommand($reversedComposerAutoloaderProjectPaths));
7677
$application->add(new DumpDependenciesCommand($reversedComposerAutoloaderProjectPaths));
7778
$application->add(new WorkerCommand($reversedComposerAutoloaderProjectPaths));
79+
$application->add(new ClearResultCacheCommand($reversedComposerAutoloaderProjectPaths));
7880
$application->run();
7981
})();

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,18 @@ private function getStubFiles(): array
405405
return $stubFiles;
406406
}
407407

408+
public function clear(): string
409+
{
410+
if (!is_file($this->cacheFilePath)) {
411+
return dirname($this->cacheFilePath);
412+
}
413+
414+
@unlink($this->cacheFilePath);
415+
if (is_file($this->cacheFilePath)) {
416+
throw new \PHPStan\ShouldNotHappenException('File still exists.');
417+
}
418+
419+
return dirname($this->cacheFilePath);
420+
}
421+
408422
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
use PHPStan\Analyser\ResultCache\ResultCacheManager;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class ClearResultCacheCommand extends Command
12+
{
13+
14+
private const NAME = 'clear-result-cache';
15+
16+
/** @var string[] */
17+
private $composerAutoloaderProjectPaths;
18+
19+
/**
20+
* @param string[] $composerAutoloaderProjectPaths
21+
*/
22+
public function __construct(
23+
array $composerAutoloaderProjectPaths
24+
)
25+
{
26+
parent::__construct();
27+
$this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths;
28+
}
29+
30+
protected function configure(): void
31+
{
32+
$this->setName(self::NAME)
33+
->setDescription('Clears the result cache.')
34+
->setDefinition([
35+
new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'),
36+
new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'),
37+
]);
38+
}
39+
40+
protected function execute(InputInterface $input, OutputInterface $output): int
41+
{
42+
$autoloadFile = $input->getOption('autoload-file');
43+
$configuration = $input->getOption('configuration');
44+
45+
if (
46+
(!is_string($autoloadFile) && $autoloadFile !== null)
47+
|| (!is_string($configuration) && $configuration !== null)
48+
) {
49+
throw new \PHPStan\ShouldNotHappenException();
50+
}
51+
52+
try {
53+
$inceptionResult = CommandHelper::begin(
54+
$input,
55+
$output,
56+
['.'],
57+
null,
58+
null,
59+
$autoloadFile,
60+
$this->composerAutoloaderProjectPaths,
61+
$configuration,
62+
'0',
63+
false,
64+
false
65+
);
66+
} catch (\PHPStan\Command\InceptionNotSuccessfulException $e) {
67+
return 1;
68+
}
69+
70+
$container = $inceptionResult->getContainer();
71+
72+
/** @var ResultCacheManager $resultCacheManager */
73+
$resultCacheManager = $container->getByType(ResultCacheManager::class);
74+
$path = $resultCacheManager->clear();
75+
76+
$output->writeln('<info>Result cache cleared from directory:</info>');
77+
$output->writeln($path);
78+
79+
return 0;
80+
}
81+
82+
}

0 commit comments

Comments
 (0)