Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions .github/workflows/e2e_with_cache.yaml

This file was deleted.

1 change: 1 addition & 0 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void
do {
$errors[] = $throwable->getMessage();
} while ($throwable = $throwable->getPrevious());

echo Json::encode([
'fatal_errors' => $errors,
]);
Expand Down
8 changes: 0 additions & 8 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

use OndraM\CiDetector\CiDetector;
use Rector\Bootstrap\ExtensionConfigResolver;
use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -27,12 +25,6 @@
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/rector_cached_files');
$rectorConfig->containerCacheDirectory(sys_get_temp_dir());

// use faster in-memory cache in CI.
// CI always starts from scratch, therefore IO intensive caching is not worth it
if (new CiDetector()->isCiDetected()) {
$rectorConfig->cacheClass(MemoryCacheStorage::class);
}

// load internal rector-* extension configs
$extensionConfigResolver = new ExtensionConfigResolver();
foreach ($extensionConfigResolver->provide() as $extensionConfigFile) {
Expand Down
1 change: 1 addition & 0 deletions e2e/applied-rule-removed-node-with-cache/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;

return static function (RectorConfig $rectorConfig): void {
// force file cache to verify the persisted cache across runs, even in CI
$rectorConfig->cacheClass(FileCacheStorage::class);

$rectorConfig->paths([
Expand Down
1 change: 1 addition & 0 deletions e2e/timeout-file-not-cached/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
// force file cache to verify the persisted cache across runs, even in CI
$rectorConfig->cacheClass(FileCacheStorage::class);
$rectorConfig->parallel(0);

Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ parameters:
- rules-tests
- utils
- scripts
- e2e/e2eTestRunnerWithCache.php
- e2e/e2eTestRunner.php

scanDirectories:
Expand Down
25 changes: 11 additions & 14 deletions src/Caching/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Caching;

use OndraM\CiDetector\CiDetector;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
use Rector\Configuration\Option;
Expand All @@ -22,24 +23,20 @@ public function __construct(
*/
public function create(): Cache
{
$cacheDirectory = SimpleParameterProvider::provideStringParameter(Option::CACHE_DIR);

$cacheClass = FileCacheStorage::class;

if (SimpleParameterProvider::hasParameter(Option::CACHE_CLASS)) {
$cacheClass = SimpleParameterProvider::provideStringParameter(Option::CACHE_CLASS);
// in CI the workspace is ephemeral and usually starts from scratch,
// so a file cache that is never read again is only wasted IO → use faster in-memory cache
if (new CiDetector()->isCiDetected()) {
return new Cache(new MemoryCacheStorage());
}

if ($cacheClass === FileCacheStorage::class) {
// ensure cache directory exists
if (! $this->fileSystem->exists($cacheDirectory)) {
$this->fileSystem->mkdir($cacheDirectory);
}
$cacheDirectory = SimpleParameterProvider::provideStringParameter(Option::CACHE_DIR);

$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem);
return new Cache($fileCacheStorage);
// ensure cache directory exists
if (! $this->fileSystem->exists($cacheDirectory)) {
$this->fileSystem->mkdir($cacheDirectory);
}

return new Cache(new MemoryCacheStorage());
$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->fileSystem);
return new Cache($fileCacheStorage);
}
}
7 changes: 5 additions & 2 deletions src/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,14 @@ public function containerCacheDirectory(string $directoryPath): void
/**
* @param class-string<CacheStorageInterface> $cacheClass
*/
#[Deprecated(message: <<<'TXT'
Cache storage is selected automatically: file cache locally, in-memory cache in CI,
where the ephemeral workspace makes writing a cache that is never re-read wasted IO.
The passed value is ignored.
TXT)]
public function cacheClass(string $cacheClass): void
{
Assert::isAOf($cacheClass, CacheStorageInterface::class);

SimpleParameterProvider::setParameter(Option::CACHE_CLASS, $cacheClass);
}

/**
Expand Down
12 changes: 0 additions & 12 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Rector\Configuration;

use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;

final class Option
{
public const string SOURCE = 'source';
Expand Down Expand Up @@ -137,15 +134,6 @@ final class Option
*/
public const string CACHE_DIR = 'cache_dir';

/**
* Cache backend. Most of the time we cache in files, but in ephemeral environment (e.g. CI), a faster `MemoryCacheStorage` can be useful.
* @internal Use RectorConfig::cacheClass() instead
*
* @var class-string<CacheStorageInterface>
* @internal
*/
public const string CACHE_CLASS = FileCacheStorage::class;

public const string DEBUG = 'debug';

public const string XDEBUG = 'xdebug';
Expand Down
13 changes: 3 additions & 10 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ final class RectorConfigBuilder
*/
private array $fileExtensions = [];

/**
* @var null|class-string<CacheStorageInterface>
*/
private ?string $cacheClass = null;

private ?string $cacheDirectory = null;

private ?string $containerCacheDirectory = null;
Expand Down Expand Up @@ -295,10 +290,6 @@ public function __invoke(RectorConfig $rectorConfig): void
$rectorConfig->fileExtensions($this->fileExtensions);
}

if ($this->cacheClass !== null) {
$rectorConfig->cacheClass($this->cacheClass);
}

if ($this->cacheDirectory !== null) {
$rectorConfig->cacheDirectory($this->cacheDirectory);
}
Expand Down Expand Up @@ -806,6 +797,9 @@ public function withFileExtensions(array $fileExtensions): self
}

/**
* The $cacheClass argument is deprecated and ignored. Cache storage is selected automatically:
* file cache locally, in-memory cache in CI.
*
* @param class-string<CacheStorageInterface>|null $cacheClass
*/
public function withCache(
Expand All @@ -814,7 +808,6 @@ public function withCache(
?string $containerCacheDirectory = null
): self {
$this->cacheDirectory = $cacheDirectory;
$this->cacheClass = $cacheClass;
$this->containerCacheDirectory = $containerCacheDirectory;

return $this;
Expand Down
12 changes: 6 additions & 6 deletions tests/Bin/RectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ final class RectorTest extends TestCase
public static function outputProvider(): Iterator
{
yield 'Version' => [
'command' => PHP_BINARY . ' bin/rector --version',
'expectedOutput' => "Rector @package_version@" . PHP_EOL,
'command' => PHP_BINARY . ' bin/rector --version',
'expectedOutput' => 'Rector @package_version@' . PHP_EOL,
];
yield 'Exception with previous console output' => [
'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php',
'expectedOutput' => PHP_EOL . " [ERROR] Rector\\NodeTypeResolver\\DependencyInjection\\PHPStanServicesFactory " . PHP_EOL . PHP_EOL . " [ERROR] Unexpected item 'parameters › invalidParameters'. " . PHP_EOL . PHP_EOL,
'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php',
'expectedOutput' => PHP_EOL . ' [ERROR] Rector\\NodeTypeResolver\\DependencyInjection\\PHPStanServicesFactory ' . PHP_EOL . PHP_EOL . " [ERROR] Unexpected item 'parameters › invalidParameters'. " . PHP_EOL . PHP_EOL,
];
yield 'Exception with previous console output in JSON format' => [
'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php --output-format json',
'command' => PHP_BINARY . ' bin/rector -c tests/Bin/config/incorrect-phpstan-files.php --output-format json',
'expectedOutput' => '{"fatal_errors":["Rector\\\\NodeTypeResolver\\\\DependencyInjection\\\\PHPStanServicesFactory","Unexpected item \'parameters › invalidParameters\'."]}',
];
}
Expand All @@ -35,6 +35,6 @@ public function testConsoleOutput(string $command, string $expectedOutput): void
{
$process = Process::fromShellCommandline($command);
$process->run();
$this->assertSame($expectedOutput, preg_replace("/ +/", " ", $process->getOutput()));
$this->assertSame($expectedOutput, preg_replace('/ +/', ' ', $process->getOutput()));
}
}
2 changes: 0 additions & 2 deletions tests/Caching/Detector/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/_rector_cached_files_test');
$rectorConfig->cacheClass(MemoryCacheStorage::class);
};
2 changes: 0 additions & 2 deletions tests/Caching/ValueObject/Storage/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/_rector_cached_files_test');
$rectorConfig->cacheClass(MemoryCacheStorage::class);
};