Skip to content

Commit

Permalink
[DX] Move PostFileProcessor to FileProcessor, as always should run to…
Browse files Browse the repository at this point in the history
…gether (#4952)
  • Loading branch information
TomasVotruba committed Sep 9, 2023
1 parent ee6e3e0 commit b82ad4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function isStringName(string $resolvedName, string $desiredName): bool
}

$containsWildcard = false;
foreach(self::REGEX_WILDCARD_CHARS as $char) {
foreach (self::REGEX_WILDCARD_CHARS as $char) {
if (str_contains($desiredName, $char)) {
$containsWildcard = true;
break;
Expand Down
6 changes: 3 additions & 3 deletions packages/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\PostRector\Application;

use PhpParser\Node\Stmt;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Provider\CurrentFileProvider;
Expand Down Expand Up @@ -47,8 +47,8 @@ public function __construct(
}

/**
* @param Stmt[] $stmts
* @return Stmt[]
* @param Node[] $stmts
* @return Node[]
*/
public function traverse(array $stmts): array
{
Expand Down
10 changes: 8 additions & 2 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\PostRector\Application\PostFileProcessor;

final class FileProcessor
{
public function __construct(
private readonly NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private readonly RectorParser $rectorParser,
private readonly RectorNodeTraverser $rectorNodeTraverser,
private readonly PostFileProcessor $postFileProcessor,
) {
}

Expand All @@ -30,10 +32,14 @@ public function parseFileInfoToLocalCache(File $file): void
$file->hydrateStmtsAndTokens($newStmts, $oldStmts, $oldTokens);
}

public function refactor(File $file): void
public function process(File $file): void
{
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts());

$file->changeNewStmts($newStmts);
// apply post rectors
$postNewStmts = $this->postFileProcessor->traverse($newStmts);

// this is needed for new tokens added in "afterTraverse()"
$file->changeNewStmts($postNewStmts);
}
}
23 changes: 8 additions & 15 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Parallel\ValueObject\Bridge;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
Expand All @@ -33,13 +32,12 @@ final class PhpFileProcessor

public function __construct(
private readonly FormatPerservingPrinter $formatPerservingPrinter,
private readonly FileProcessor $fileProcessor,
private readonly SymfonyStyle $symfonyStyle,
private readonly FileDiffFactory $fileDiffFactory,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly PostFileProcessor $postFileProcessor,
private readonly ErrorFactory $errorFactory,
private readonly FilePathHelper $filePathHelper
private readonly FileProcessor $fileProcessor,
private readonly SymfonyStyle $symfonyStyle,
private readonly FileDiffFactory $fileDiffFactory,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly ErrorFactory $errorFactory,
private readonly FilePathHelper $filePathHelper
) {
}

Expand Down Expand Up @@ -68,14 +66,9 @@ public function process(File $file, Configuration $configuration): array
$rectorWithLineChanges = null;
do {
$file->changeHasChanged(false);
$this->fileProcessor->refactor($file);
$this->fileProcessor->process($file);

// 3. apply post rectors
$newStmts = $this->postFileProcessor->traverse($file->getNewStmts());
// this is needed for new tokens added in "afterTraverse()"
$file->changeNewStmts($newStmts);

// 4. print to file or string
// 3. print to file or string
// important to detect if file has changed
$this->printFile($file, $configuration);

Expand Down

0 comments on commit b82ad4c

Please sign in to comment.