Skip to content
Merged
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
18 changes: 15 additions & 3 deletions packages/file-system-rector/src/FileSystemFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\FileSystemRector;

use Rector\Core\Logging\CurrentRectorProvider;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand All @@ -20,20 +21,31 @@ final class FileSystemFileProcessor
*/
private $fileSystemRectors = [];

/**
* @var CurrentRectorProvider
*/
private $currentRectorProvider;

/**
* @param FileSystemRectorInterface[] $fileSystemRectors
*/
public function __construct(CurrentFileInfoProvider $currentFileInfoProvider, array $fileSystemRectors = [])
{
public function __construct(
CurrentFileInfoProvider $currentFileInfoProvider,
CurrentRectorProvider $currentRectorProvider,
array $fileSystemRectors = []
) {
$this->currentFileInfoProvider = $currentFileInfoProvider;
$this->fileSystemRectors = $fileSystemRectors;
$this->currentRectorProvider = $currentRectorProvider;
}

public function processFileInfo(SmartFileInfo $smartFileInfo): void
{
$this->currentFileInfoProvider ->setCurrentFileInfo($smartFileInfo);
$this->currentFileInfoProvider->setCurrentFileInfo($smartFileInfo);

foreach ($this->fileSystemRectors as $fileSystemRector) {
$this->currentRectorProvider->changeCurrentRector($fileSystemRector);

$fileSystemRector->refactor($smartFileInfo);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\PostRector\Application\PostFileProcessor;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
use TypeError;
Expand Down Expand Up @@ -70,6 +71,11 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
*/
private $parserFactory;

/**
* @var PostFileProcessor
*/
private $postFileProcessor;

/**
* @required
*/
Expand All @@ -82,7 +88,8 @@ public function autowireAbstractFileSystemRector(
RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
Configuration $configuration,
BetterStandardPrinter $betterStandardPrinter,
ParameterProvider $parameterProvider
ParameterProvider $parameterProvider,
PostFileProcessor $postFileProcessor
): void {
$this->parser = $parser;
$this->parserFactory = $parserFactory;
Expand All @@ -93,6 +100,7 @@ public function autowireAbstractFileSystemRector(
$this->configuration = $configuration;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->parameterProvider = $parameterProvider;
$this->postFileProcessor = $postFileProcessor;
}

/**
Expand Down Expand Up @@ -125,6 +133,8 @@ protected function parseFileInfoToNodesWithoutScope(SmartFileInfo $smartFileInfo
*/
protected function printNodesToFilePath(array $nodes, string $fileDestination): void
{
$nodes = $this->postFileProcessor->traverse($nodes);

$fileContent = $this->formatPerservingPrinter->printToString(
$nodes,
$this->oldStmts,
Expand All @@ -139,6 +149,8 @@ protected function printNodesToFilePath(array $nodes, string $fileDestination):
*/
protected function printNewNodesToFilePath(array $nodes, string $fileDestination): void
{
$nodes = $this->postFileProcessor->traverse($nodes);

// 1. if nodes are the same, prefer format preserving printer
try {
$dummyLexer = new Lexer();
Expand Down
8 changes: 7 additions & 1 deletion src/Testing/PHPUnit/AbstractFileSystemRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ private function createContainerWithProvidedRector(): void

FileSystem::write($configFileTempPath, $yamlContent);

$this->bootKernelWithConfigs(RectorKernel::class, [$configFileTempPath]);
// for 3rd party testing with services defined in configs
$configFilePaths = [$configFileTempPath];
if ($this->provideConfig() !== '') {
$configFilePaths[] = $this->provideConfig();
}

$this->bootKernelWithConfigs(RectorKernel::class, $configFilePaths);
}

private function createConfigFileTempPath(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Testing/PHPUnit/AbstractGenericRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ protected function getRectorClass(): string
return '';
}

protected function provideConfig(): string
{
// can be implemented
return '';
}

/**
* @return array<string, mixed[]>
*/
Expand Down
6 changes: 0 additions & 6 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ protected function doTestFileWithoutAutoload(string $file): void
$this->autoloadTestFixture = true;
}

protected function provideConfig(): string
{
// can be implemented
return '';
}

protected function doTestFile(string $file): void
{
$smartFileInfo = new SmartFileInfo($file);
Expand Down