Skip to content

Commit

Permalink
Update text for FileProccessor to use Rector interface contract (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored May 2, 2021
1 parent 6f30aff commit cd8be43
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract;

use Rector\Core\Contract\Rector\RectorInterface;

interface TextRectorInterface extends RectorInterface
{
public function refactorContent(string $content): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Rector;

use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract\TextRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ChangeTextRector implements TextRectorInterface
{
public function refactorContent(string $content): string
{
return str_replace('Foo', 'Bar', $content);
}

public function getRuleDefinition(): RuleDefinition
{
// just for docs
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@
namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source;

use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract\TextRectorInterface;
use Rector\Core\ValueObject\Application\File;

final class TextFileProcessor implements FileProcessorInterface
{
/**
* @var TextRectorInterface[]
*/
private $textRectors;

/**
* @param TextRectorInterface[] $textRectors
*/
public function __construct(array $textRectors)
{
$this->textRectors = $textRectors;
}

/**
* @param File[] $files
*/
public function process(array $files): void
{
foreach ($files as $file) {
$this->processFile($file);
$fileContent = $file->getFileContent();

foreach ($this->textRectors as $textRector) {
$fileContent = $textRector->refactorContent($fileContent);
}

$file->changeFileContent($fileContent);
}
}

Expand All @@ -32,12 +52,4 @@ public function getSupportedFileExtensions(): array
{
return ['txt'];
}

private function processFile($file): void
{
$oldFileContent = $file->getFileContent();
$changedFileContent = str_replace('Foo', 'Bar', $oldFileContent);

$file->changeFileContent($changedFileContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

declare(strict_types=1);

use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Rector\ChangeTextRector;
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\TextFileProcessor;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(TextFileProcessor::class);

$services->set(ChangeTextRector::class);
};

0 comments on commit cd8be43

Please sign in to comment.