rector/packages/PostRector/Application/PostFileProcessor.php

85 lines
2.9 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
2020-03-31 18:39:08 +02:00
namespace Rector\PostRector\Application;
use PhpParser\Node\Stmt;
2020-03-31 18:39:08 +02:00
use PhpParser\NodeTraverser;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
2020-03-31 18:39:08 +02:00
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use RectorPrefix20210903\Symplify\Skipper\Skipper\Skipper;
2020-03-31 20:46:33 +02:00
final class PostFileProcessor
{
/**
2020-03-31 20:46:33 +02:00
* @var PostRectorInterface[]
*/
2020-03-31 20:46:33 +02:00
private $postRectors = [];
/**
* @var \Symplify\Skipper\Skipper\Skipper
*/
private $skipper;
/**
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
/**
* @var \Rector\Core\Logging\CurrentRectorProvider
*/
private $currentRectorProvider;
/**
2020-03-31 18:39:08 +02:00
* @param PostRectorInterface[] $postRectors
*/
public function __construct(\RectorPrefix20210903\Symplify\Skipper\Skipper\Skipper $skipper, \Rector\Core\Provider\CurrentFileProvider $currentFileProvider, \Rector\Core\Logging\CurrentRectorProvider $currentRectorProvider, array $postRectors)
{
$this->skipper = $skipper;
$this->currentFileProvider = $currentFileProvider;
$this->currentRectorProvider = $currentRectorProvider;
$this->postRectors = $this->sortByPriority($postRectors);
}
/**
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function traverse(array $nodes) : array
{
2020-03-31 20:46:33 +02:00
foreach ($this->postRectors as $postRector) {
if ($this->shouldSkipPostRector($postRector)) {
continue;
}
$this->currentRectorProvider->changeCurrentRector($postRector);
$nodeTraverser = new \PhpParser\NodeTraverser();
2020-03-31 20:46:33 +02:00
$nodeTraverser->addVisitor($postRector);
$nodes = $nodeTraverser->traverse($nodes);
}
2020-03-31 20:46:33 +02:00
return $nodes;
}
/**
2020-03-31 20:46:33 +02:00
* @param PostRectorInterface[] $postRectors
2020-04-01 02:05:51 +02:00
* @return PostRectorInterface[]
*/
private function sortByPriority(array $postRectors) : array
{
2020-03-31 20:46:33 +02:00
$postRectorsByPriority = [];
foreach ($postRectors as $postRector) {
if (isset($postRectorsByPriority[$postRector->getPriority()])) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
2020-03-31 20:46:33 +02:00
$postRectorsByPriority[$postRector->getPriority()] = $postRector;
}
\krsort($postRectorsByPriority);
2020-04-01 02:05:51 +02:00
return $postRectorsByPriority;
}
private function shouldSkipPostRector(\Rector\PostRector\Contract\Rector\PostRectorInterface $postRector) : bool
{
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return \false;
}
$smartFileInfo = $file->getSmartFileInfo();
return $this->skipper->shouldSkipElementAndFileInfo($postRector, $smartFileInfo);
}
}