| Current Path : /home/rtorresani/www/vendor/rector/rector/packages/PostRector/Application/ |
| Current File : //home/rtorresani/www/vendor/rector/rector/packages/PostRector/Application/PostFileProcessor.php |
<?php
declare (strict_types=1);
namespace Rector\PostRector\Application;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\Skipper\Skipper\Skipper;
final class PostFileProcessor
{
/**
* @var PostRectorInterface[]
*/
private $postRectors = [];
/**
* @readonly
* @var \Rector\Skipper\Skipper\Skipper
*/
private $skipper;
/**
* @readonly
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
/**
* @readonly
* @var \Rector\Core\Logging\CurrentRectorProvider
*/
private $currentRectorProvider;
/**
* @param PostRectorInterface[] $postRectors
*/
public function __construct(Skipper $skipper, CurrentFileProvider $currentFileProvider, CurrentRectorProvider $currentRectorProvider, array $postRectors)
{
$this->skipper = $skipper;
$this->currentFileProvider = $currentFileProvider;
$this->currentRectorProvider = $currentRectorProvider;
$this->postRectors = $this->sortByPriority($postRectors);
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
public function traverse(array $stmts) : array
{
foreach ($this->postRectors as $postRector) {
if ($this->shouldSkipPostRector($postRector)) {
continue;
}
$this->currentRectorProvider->changeCurrentRector($postRector);
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($postRector);
$stmts = $nodeTraverser->traverse($stmts);
}
return $stmts;
}
/**
* @param PostRectorInterface[] $postRectors
* @return PostRectorInterface[]
*/
private function sortByPriority(array $postRectors) : array
{
$postRectorsByPriority = [];
foreach ($postRectors as $postRector) {
if (isset($postRectorsByPriority[$postRector->getPriority()])) {
$errorMessage = \sprintf('There are multiple post rectors with the same priority: %d. Use different one for your new PostRector', $postRector->getPriority());
throw new ShouldNotHappenException($errorMessage);
}
$postRectorsByPriority[$postRector->getPriority()] = $postRector;
}
\krsort($postRectorsByPriority);
return $postRectorsByPriority;
}
private function shouldSkipPostRector(PostRectorInterface $postRector) : bool
{
$file = $this->currentFileProvider->getFile();
if (!$file instanceof File) {
return \false;
}
$filePath = $file->getFilePath();
if ($this->skipper->shouldSkipElementAndFilePath($postRector, $filePath)) {
return \true;
}
if ($postRector instanceof PostRectorDependencyInterface) {
$dependencies = $postRector->getRectorDependencies();
foreach ($dependencies as $dependency) {
if ($this->skipper->shouldSkipElementAndFilePath($dependency, $filePath)) {
return \true;
}
}
}
return \false;
}
}