skipper = $skipper; $this->currentFileProvider = $currentFileProvider; $this->currentRectorProvider = $currentRectorProvider; $this->postRectors = $this->sortByPriority($postRectors); } /** * @param Stmt[] $nodes * @return Stmt[] */ public function traverse(array $nodes) : array { foreach ($this->postRectors as $postRector) { if ($this->shouldSkipPostRector($postRector)) { continue; } $this->currentRectorProvider->changeCurrentRector($postRector); $nodeTraverser = new \PhpParser\NodeTraverser(); $nodeTraverser->addVisitor($postRector); $nodes = $nodeTraverser->traverse($nodes); } return $nodes; } /** * @param PostRectorInterface[] $postRectors * @return PostRectorInterface[] */ private function sortByPriority(array $postRectors) : array { $postRectorsByPriority = []; foreach ($postRectors as $postRector) { if (isset($postRectorsByPriority[$postRector->getPriority()])) { throw new \Rector\Core\Exception\ShouldNotHappenException(); } $postRectorsByPriority[$postRector->getPriority()] = $postRector; } \krsort($postRectorsByPriority); 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); } }