Skip to content

Commit

Permalink
Enable changed files cache by default (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 22, 2021
1 parent b7359ed commit 697c20e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 47 deletions.
3 changes: 0 additions & 3 deletions build/target-repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
// is your PHP version different from the one your refactor to? [default: your PHP version], uses PHP_VERSION_ID format
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_72);

// Run Rector only on changed files
$parameters->set(Option::ENABLE_CACHE, true);

// Path to phpstan with extensions, that PHPSTan in Rector uses to determine types
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, getcwd() . '/phpstan-for-config.neon');
};
Expand Down
1 change: 0 additions & 1 deletion config/parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, null);

// cache
$parameters->set(Option::ENABLE_CACHE, false);
$parameters->set(Option::CACHE_DIR, sys_get_temp_dir() . '/rector_cached_files');
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
public function filterFileInfos(array $phpFileInfos): array
{
// cache stuff
if (! $this->configuration->isCacheEnabled() || $this->configuration->shouldClearCache()) {
if ($this->configuration->shouldClearCache()) {
$this->changedFilesDetector->clear();
return $phpFileInfos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function processNodes(array $nodes, SmartFileInfo $smartFileInfo): array
/** @var MutatingScope $scope */
$this->nodeScopeResolver->processNodes($nodes, $scope, $nodeCallback);

$this->reportCacheDebugAndSaveDependentFiles($smartFileInfo, $this->dependentFiles);
$this->changedFilesDetector->addFileWithDependencies($smartFileInfo, $this->dependentFiles);

return $nodes;
}
Expand Down Expand Up @@ -139,10 +139,6 @@ private function resolveClassOrInterfaceScope(Class_ | Interface_ $classLike, Sc

private function resolveDependentFiles(Node $node, MutatingScope $mutatingScope): void
{
if (! $this->configuration->isCacheEnabled()) {
return;
}

try {
$dependentFiles = $this->dependencyResolver->resolveDependencies($node, $mutatingScope);
foreach ($dependentFiles as $dependentFile) {
Expand All @@ -153,19 +149,6 @@ private function resolveDependentFiles(Node $node, MutatingScope $mutatingScope)
}
}

/**
* @param string[] $dependentFiles
*/
private function reportCacheDebugAndSaveDependentFiles(SmartFileInfo $smartFileInfo, array $dependentFiles): void
{
if (! $this->configuration->isCacheEnabled()) {
return;
}

// save for cache
$this->changedFilesDetector->addFileWithDependencies($smartFileInfo, $dependentFiles);
}

private function resolveClassName(Class_ | Interface_ | Trait_ $classLike): string
{
if (property_exists($classLike, 'namespacedName')) {
Expand Down
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,4 @@
]);

$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan-for-rector.neon');
$parameters->set(Option::ENABLE_CACHE, true);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Rector\Tests\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\Fixture;

use PhpParser\Node;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Dependency\DependencyResolver;

class SkipForeachAssign
{
/**
Expand All @@ -14,13 +18,12 @@ class SkipForeachAssign
$this->dependentFiles = [];
}

private function resolveDependentFiles(Node $node, MutatingScope $mutatingScope): void
{
if (! $this->configuration->isCacheEnabled()) {
return;
}

foreach ($this->dependencyResolver->resolveDependencies($node, $mutatingScope) as $dependentFile) {
private function resolveDependentFiles(
DependencyResolver $dependencyResolver,
Node $node,
MutatingScope $mutatingScope
): void {
foreach ($dependencyResolver->resolveDependencies($node, $mutatingScope) as $dependentFile) {
$this->dependentFiles[] = $dependentFile;
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ final class Configuration

private string $outputFormat;

private bool $isCacheEnabled = false;

/**
* @var string[]
*/
Expand All @@ -41,7 +39,6 @@ final class Configuration

public function __construct(ParameterProvider $parameterProvider)
{
$this->isCacheEnabled = (bool) $parameterProvider->provideParameter(Option::ENABLE_CACHE);
$this->fileExtensions = (array) $parameterProvider->provideParameter(Option::FILE_EXTENSIONS);
$this->paths = (array) $parameterProvider->provideParameter(Option::PATHS);
$this->parameterProvider = $parameterProvider;
Expand Down Expand Up @@ -92,11 +89,6 @@ public function shouldClearCache(): bool
return $this->shouldClearCache;
}

public function isCacheEnabled(): bool
{
return $this->isCacheEnabled;
}

/**
* @return string[]
*/
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ final class Option
public const CLEAR_CACHE = 'clear-cache';

/**
* @deprecated Cache is enabled by default
* @var string
*/
public const ENABLE_CACHE = 'enable_cache';
Expand Down
4 changes: 0 additions & 4 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v

private function invalidateCacheChangedFiles(ProcessResult $processResult): void
{
if (! $this->configuration->isCacheEnabled()) {
return;
}

foreach ($processResult->getChangedFileInfos() as $changedFileInfo) {
$this->changedFilesDetector->invalidateFile($changedFileInfo);
}
Expand Down
15 changes: 11 additions & 4 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\FileSystem;

use Nette\Utils\Strings;
use Rector\Caching\UnchangedFilesFilter;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function __construct(
private FinderSanitizer $finderSanitizer,
private FileSystemFilter $fileSystemFilter,
private SkippedPathsResolver $skippedPathsResolver,
private UnchangedFilesFilter $unchangedFilesFilter
) {
}

Expand All @@ -46,14 +48,17 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes): array
{
$filesAndDirectories = $this->filesystemTweaker->resolveWithFnmatch($source);

$files = $this->fileSystemFilter->filterFiles($filesAndDirectories);
$filePaths = $this->fileSystemFilter->filterFiles($filesAndDirectories);
$directories = $this->fileSystemFilter->filterDirectories($filesAndDirectories);


$smartFileInfos = [];
foreach ($files as $file) {
$smartFileInfos[] = new SmartFileInfo($file);
foreach ($filePaths as $filePath) {
$smartFileInfos[] = new SmartFileInfo($filePath);
}

$smartFileInfos = $this->unchangedFilesFilter->filterAndJoinWithDependentFileInfos($smartFileInfos);

return array_merge($smartFileInfos, $this->findInDirectories($directories, $suffixes));
}

Expand Down Expand Up @@ -81,7 +86,9 @@ private function findInDirectories(array $directories, array $suffixes): array

$this->addFilterWithExcludedPaths($finder);

return $this->finderSanitizer->sanitize($finder);
$smartFileInfos = $this->finderSanitizer->sanitize($finder);

return $this->unchangedFilesFilter->filterAndJoinWithDependentFileInfos($smartFileInfos);
}

/**
Expand Down

0 comments on commit 697c20e

Please sign in to comment.