Skip to content

Commit

Permalink
make file processor always return string, handle diff outside
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 10, 2021
1 parent 9c1cdae commit 5364e4b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function addFileDiff(SmartFileInfo $smartFileInfo, string $newContent, st
$this->consoleDiffer->diff($oldContent, $newContent),
$rectorChanges
);

$this->fileDiffs[$smartFileInfo->getRealPath()] = $fileDiff;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ private function processFileInfo(SmartFileInfo $originalFileInfo): string
// mimic post-rectors
return $this->fileProcessor->printToString($originalFileInfo);
} elseif (Strings::match($originalFileInfo->getFilename(), StaticNonPhpFileSuffixes::getSuffixRegexPattern())) {
$nonPhpFileChange = $this->nonPhpFileProcessor->process($originalFileInfo);
return $nonPhpFileChange !== null ? $nonPhpFileChange->getNewContent() : '';
return $this->nonPhpFileProcessor->process($originalFileInfo);
}

$message = sprintf(
Expand Down
50 changes: 50 additions & 0 deletions rules/Composer/ErrorReporting/ComposerJsonChangeReporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Composer\ErrorReporting;

use Rector\Core\Configuration\Configuration;
use Symfony\Component\Process\Process;
use Symplify\ComposerJsonManipulator\Printer\ComposerJsonPrinter;
use Symplify\ComposerJsonManipulator\ValueObject\ComposerJson;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ComposerJsonChangeReporter
{
/**
* @var string
*/
private const COMPOSER_UPDATE = 'composer update';

/**
* @var Configuration
*/
private $configuration;

/**
* @var ComposerJsonPrinter
*/
private $composerJsonPrinter;

public function __construct(Configuration $configuration, ComposerJsonPrinter $composerJsonPrinter)
{
$this->configuration = $configuration;
$this->composerJsonPrinter = $composerJsonPrinter;
}

public function reportFileContentChange(ComposerJson $composerJson, SmartFileInfo $smartFileInfo): void
{
if ($this->configuration->isDryRun()) {
return;
}

$this->composerJsonPrinter->print($composerJson, $smartFileInfo);

$process = new Process(explode(' ', self::COMPOSER_UPDATE), getcwd());
$process->run(function (string $type, string $message): void {
// $type is always err https://github.com/composer/composer/issues/3795#issuecomment-76401013
echo $message;
});
}
}
44 changes: 4 additions & 40 deletions rules/Composer/Processor/ComposerFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
namespace Rector\Composer\Processor;

use Rector\Composer\Modifier\ComposerModifier;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange;
use Symfony\Component\Process\Process;
use Symplify\ComposerJsonManipulator\ComposerJsonFactory;
use Symplify\ComposerJsonManipulator\Printer\ComposerJsonPrinter;
use Symplify\ComposerJsonManipulator\ValueObject\ComposerJson;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ComposerFileProcessor implements FileProcessorInterface
{
/**
* @var string
*/
private const COMPOSER_UPDATE = 'composer update';

/**
* @var ComposerJsonFactory
*/
Expand All @@ -36,28 +27,21 @@ final class ComposerFileProcessor implements FileProcessorInterface
*/
private $composerModifier;

/**
* @var Configuration
*/
private $configuration;

public function __construct(
ComposerJsonFactory $composerJsonFactory,
ComposerJsonPrinter $composerJsonPrinter,
Configuration $configuration,
ComposerModifier $composerModifier
) {
$this->composerJsonFactory = $composerJsonFactory;
$this->composerJsonPrinter = $composerJsonPrinter;
$this->configuration = $configuration;
$this->composerModifier = $composerModifier;
}

public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange
public function process(SmartFileInfo $smartFileInfo): string
{
// to avoid modification of file
if (! $this->composerModifier->enabled()) {
return null;
return $smartFileInfo->getContents();
}

$composerJson = $this->composerJsonFactory->createFromFileInfo($smartFileInfo);
Expand All @@ -66,15 +50,10 @@ public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange

// nothing has changed
if ($oldComposerJson->getJsonArray() === $composerJson->getJsonArray()) {
return null;
return $smartFileInfo->getContents();
}

$oldContent = $this->composerJsonPrinter->printToString($oldComposerJson);
$newContent = $this->composerJsonPrinter->printToString($composerJson);

$this->reportFileContentChange($composerJson, $smartFileInfo);

return new NonPhpFileChange($oldContent, $newContent);
return $this->composerJsonPrinter->printToString($composerJson);
}

public function supports(SmartFileInfo $smartFileInfo): bool
Expand All @@ -89,19 +68,4 @@ public function getSupportedFileExtensions(): array
{
return ['json'];
}

private function reportFileContentChange(ComposerJson $composerJson, SmartFileInfo $smartFileInfo): void
{
if ($this->configuration->isDryRun()) {
return;
}

$this->composerJsonPrinter->print($composerJson, $smartFileInfo);

$process = new Process(explode(' ', self::COMPOSER_UPDATE), getcwd());
$process->run(function (string $type, string $message): void {
// $type is always err https://github.com/composer/composer/issues/3795#issuecomment-76401013
echo $message;
});
}
}
3 changes: 1 addition & 2 deletions src/Contract/Processor/FileProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

namespace Rector\Core\Contract\Processor;

use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange;
use Symplify\SmartFileSystem\SmartFileInfo;

interface FileProcessorInterface
{
public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange;
public function process(SmartFileInfo $smartFileInfo): string;

public function supports(SmartFileInfo $smartFileInfo): bool;

Expand Down
30 changes: 14 additions & 16 deletions src/NonPhpFile/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Rector\Core\Configuration\Configuration;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\FileSystem\FilesFinder;
use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

Expand Down Expand Up @@ -60,8 +59,8 @@ public function __construct(
*/
public function runOnPaths(array $paths): void
{
$nonPhpFileInfos = $this->collectNonPhpFiles($paths);
$this->runNonPhpFileProcessors($nonPhpFileInfos);
$fileInfos = $this->findFileInfos($paths);
$this->runNonPhpFileProcessors($fileInfos);
}

/**
Expand All @@ -75,26 +74,19 @@ private function runNonPhpFileProcessors(array $nonPhpFileInfos): void
continue;
}

$nonPhpFileChange = $nonPhpFileProcessor->process($nonPhpFileInfo);
if (! $nonPhpFileChange instanceof NonPhpFileChange) {
$oldContent = $nonPhpFileInfo->getContents();
$newContent = $nonPhpFileProcessor->process($nonPhpFileInfo);
if ($oldContent === $newContent) {
continue;
}

$this->errorAndDiffCollector->addFileDiff(
$nonPhpFileInfo,
$nonPhpFileChange->getNewContent(),
$nonPhpFileChange->getOldContent()
);
$this->errorAndDiffCollector->addFileDiff($nonPhpFileInfo, $oldContent, $newContent);

if ($this->configuration->isDryRun()) {
return;
}

$this->smartFileSystem->dumpFile(
$nonPhpFileInfo->getPathname(),
$nonPhpFileChange->getNewContent()
);
$this->smartFileSystem->chmod($nonPhpFileInfo->getRealPath(), $nonPhpFileInfo->getPerms());
$this->dumpFileInfo($nonPhpFileInfo, $newContent);
}
}
}
Expand All @@ -103,7 +95,7 @@ private function runNonPhpFileProcessors(array $nonPhpFileInfos): void
* @param string[] $paths
* @return SmartFileInfo[]
*/
private function collectNonPhpFiles(array $paths): array
private function findFileInfos(array $paths): array
{
$fileExtensions = $this->resolveSupportedFileExtensions();

Expand All @@ -130,4 +122,10 @@ private function resolveSupportedFileExtensions(): array

return array_unique($fileExtensions);
}

private function dumpFileInfo(SmartFileInfo $nonPhpFileInfo, string $newContent): void
{
$this->smartFileSystem->dumpFile($nonPhpFileInfo->getPathname(), $newContent);
$this->smartFileSystem->chmod($nonPhpFileInfo->getRealPath(), $nonPhpFileInfo->getPerms());
}
}
12 changes: 2 additions & 10 deletions src/NonPhpFile/NonPhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange;
use Rector\Core\ValueObject\StaticNonPhpFileSuffixes;
use Rector\PSR4\Collector\RenamedClassesCollector;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand Down Expand Up @@ -41,7 +40,7 @@ public function __construct(
$this->nonPhpFileClassRenamer = $nonPhpFileClassRenamer;
}

public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange
public function process(SmartFileInfo $smartFileInfo): string
{
$oldContents = $smartFileInfo->getContents();

Expand All @@ -50,14 +49,7 @@ public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange
$this->renamedClassesCollector->getOldToNewClasses()
);

$newContents = $this->nonPhpFileClassRenamer->renameClasses($oldContents, $classRenames);

// nothing has changed
if ($oldContents === $newContents) {
return null;
}

return new NonPhpFileChange($oldContents, $newContents);
return $this->nonPhpFileClassRenamer->renameClasses($oldContents, $classRenames);
}

public function supports(SmartFileInfo $smartFileInfo): bool
Expand Down
11 changes: 3 additions & 8 deletions tests/NonPhpFile/Source/TextNonPhpFileProcessor.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

namespace Rector\Core\Tests\NonPhpFile\Source;


use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange;
use Symplify\SmartFileSystem\SmartFileInfo;

final class TextNonPhpFileProcessor implements FileProcessorInterface
{

public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange
public function process(SmartFileInfo $smartFileInfo): string
{
$oldContent = $smartFileInfo->getContents();
$newContent = str_replace('Foo', 'Bar', $oldContent);

return new NonPhpFileChange($oldContent, $newContent);
return str_replace('Foo', 'Bar', $oldContent);
}

public function supports(SmartFileInfo $smartFileInfo): bool
Expand Down

0 comments on commit 5364e4b

Please sign in to comment.