Skip to content

Commit cd8be43

Browse files
authored
Update text for FileProccessor to use Rector interface contract (#6289)
1 parent 6f30aff commit cd8be43

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract;
6+
7+
use Rector\Core\Contract\Rector\RectorInterface;
8+
9+
interface TextRectorInterface extends RectorInterface
10+
{
11+
public function refactorContent(string $content): string;
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Rector;
6+
7+
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract\TextRectorInterface;
8+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
9+
10+
final class ChangeTextRector implements TextRectorInterface
11+
{
12+
public function refactorContent(string $content): string
13+
{
14+
return str_replace('Foo', 'Bar', $content);
15+
}
16+
17+
public function getRuleDefinition(): RuleDefinition
18+
{
19+
// just for docs
20+
}
21+
}

tests/Application/ApplicationFileProcessor/Source/TextFileProcessor.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,37 @@
55
namespace Rector\Core\Tests\Application\ApplicationFileProcessor\Source;
66

77
use Rector\Core\Contract\Processor\FileProcessorInterface;
8+
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Contract\TextRectorInterface;
89
use Rector\Core\ValueObject\Application\File;
910

1011
final class TextFileProcessor implements FileProcessorInterface
1112
{
13+
/**
14+
* @var TextRectorInterface[]
15+
*/
16+
private $textRectors;
17+
18+
/**
19+
* @param TextRectorInterface[] $textRectors
20+
*/
21+
public function __construct(array $textRectors)
22+
{
23+
$this->textRectors = $textRectors;
24+
}
25+
1226
/**
1327
* @param File[] $files
1428
*/
1529
public function process(array $files): void
1630
{
1731
foreach ($files as $file) {
18-
$this->processFile($file);
32+
$fileContent = $file->getFileContent();
33+
34+
foreach ($this->textRectors as $textRector) {
35+
$fileContent = $textRector->refactorContent($fileContent);
36+
}
37+
38+
$file->changeFileContent($fileContent);
1939
}
2040
}
2141

@@ -32,12 +52,4 @@ public function getSupportedFileExtensions(): array
3252
{
3353
return ['txt'];
3454
}
35-
36-
private function processFile($file): void
37-
{
38-
$oldFileContent = $file->getFileContent();
39-
$changedFileContent = str_replace('Foo', 'Bar', $oldFileContent);
40-
41-
$file->changeFileContent($changedFileContent);
42-
}
4355
}

tests/Application/ApplicationFileProcessor/config/configured_rule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
declare(strict_types=1);
44

5+
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\Rector\ChangeTextRector;
56
use Rector\Core\Tests\Application\ApplicationFileProcessor\Source\TextFileProcessor;
67
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
78

89
return static function (ContainerConfigurator $containerConfigurator): void {
910
$services = $containerConfigurator->services();
1011
$services->set(TextFileProcessor::class);
12+
13+
$services->set(ChangeTextRector::class);
1114
};

0 commit comments

Comments
 (0)