Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update text for FileProccessor to use Rector interface contract #6289

Merged
merged 1 commit into from
May 2, 2021
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
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);
};