Skip to content

Commit

Permalink
Do not pass a fake path to analyse in ClearResultCacheCommand; check …
Browse files Browse the repository at this point in the history
…paths count later when asking for files
  • Loading branch information
ondrejmirtes committed Aug 24, 2023
1 parent 3befb53 commit 4e37a2d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} catch (PathNotFoundException $e) {
$inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('<error>%s</error>', $e->getMessage()));
return 1;
} catch (InceptionNotSuccessfulException $e) {
return 1;
}

if (count($files) === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ClearResultCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$inceptionResult = CommandHelper::begin(
$input,
$output,
['.'],
[],
$memoryLimit,
$autoloadFile,
$this->composerAutoloaderProjectPaths,
Expand Down
11 changes: 5 additions & 6 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,6 @@ public static function begin(
$containerFactory->clearOldContainers($tmpDir);
}

if (count($paths) === 0) {
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
throw new InceptionNotSuccessfulException();
}

/** @var bool|null $customRulesetUsed */
$customRulesetUsed = $container->getParameter('customRulesetUsed');
if ($customRulesetUsed === null) {
Expand Down Expand Up @@ -473,7 +468,11 @@ public static function begin(

$stubFilesProvider = $container->getByType(StubFilesProvider::class);

$filesCallback = static function () use ($currentWorkingDirectoryFileHelper, $stubFilesProvider, $fileFinder, $pathRoutingParser, $paths): array {
$filesCallback = static function () use ($currentWorkingDirectoryFileHelper, $stubFilesProvider, $fileFinder, $pathRoutingParser, $paths, $errorOutput): array {
if (count($paths) === 0) {
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
throw new InceptionNotSuccessfulException();
}
$fileFinderResult = $fileFinder->findFiles($paths);
$files = $fileFinderResult->getFiles();

Expand Down
9 changes: 8 additions & 1 deletion src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\File\FileMonitorResult;
use PHPStan\File\FileReader;
use PHPStan\File\FileWriter;
use PHPStan\File\PathNotFoundException;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Process\ProcessHelper;
use PHPStan\Process\ProcessPromise;
Expand Down Expand Up @@ -417,7 +418,13 @@ private function reanalyseAfterFileChanges(
$projectConfigArray = $inceptionResult->getProjectConfigArray();

$resultCacheManager = $this->resultCacheManagerFactory->create();
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();

try {
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
} catch (InceptionNotSuccessfulException | PathNotFoundException) {
throw new ShouldNotHappenException();
}

$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $projectConfigArray, $inceptionResult->getErrorOutput());
if (count($resultCache->getFilesToAnalyse()) === 0) {
$result = $resultCacheManager->process(
Expand Down
9 changes: 8 additions & 1 deletion src/Command/FixerWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\Collectors\CollectedData;
use PHPStan\DependencyInjection\Container;
use PHPStan\File\PathNotFoundException;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\Registry as RuleRegistry;
use PHPStan\ShouldNotHappenException;
Expand Down Expand Up @@ -116,7 +117,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$resultCacheManager = $container->getByType(ResultCacheManagerFactory::class)->create();
$projectConfigArray = $inceptionResult->getProjectConfigArray();
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();

try {
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
} catch (PathNotFoundException | InceptionNotSuccessfulException) {
return 1;
}

$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $projectConfigArray, $inceptionResult->getErrorOutput());

$intermediateAnalyserResult = $analyserRunner->runAnalyser(
Expand Down
4 changes: 4 additions & 0 deletions src/Command/InceptionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Command;

use PHPStan\DependencyInjection\Container;
use PHPStan\File\PathNotFoundException;
use PHPStan\Internal\BytesHelper;
use function sprintf;

Expand Down Expand Up @@ -31,12 +32,15 @@ public function __construct(
}

/**
* @throws InceptionNotSuccessfulException
* @throws PathNotFoundException
* @return array{string[], bool}
*/
public function getFiles(): array
{
$callback = $this->filesCallback;

/** @throws InceptionNotSuccessfulException|PathNotFoundException */
return $callback();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} catch (PathNotFoundException $e) {
$inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('<error>%s</error>', $e->getMessage()));
return 1;
} catch (InceptionNotSuccessfulException) {
return 1;
}

$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);
Expand Down

0 comments on commit 4e37a2d

Please sign in to comment.