rector/vendor/rector/rector-nette/src/FileProcessor/LatteFileProcessor.php
Tomas Votruba f7bb838601 Updated Rector to commit f0425bc3cb3d5855da1bff9c38b872a9003e357b
f0425bc3cb Improve scope, PHPStan extension types (#2747)
2022-08-09 13:39:17 +00:00

64 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Nette\FileProcessor;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Nette\Contract\Rector\LatteRectorInterface;
use Rector\Parallel\ValueObject\Bridge;
final class LatteFileProcessor implements FileProcessorInterface
{
/**
* @var LatteRectorInterface[]
*/
private $latteRectors;
/**
* @var \Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory
*/
private $fileDiffFactory;
/**
* @param LatteRectorInterface[] $latteRectors
*/
public function __construct(array $latteRectors, FileDiffFactory $fileDiffFactory)
{
$this->latteRectors = $latteRectors;
$this->fileDiffFactory = $fileDiffFactory;
}
/**
* @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
*/
public function process(File $file, Configuration $configuration) : array
{
$systemErrorsAndFileDiffs = [Bridge::SYSTEM_ERRORS => [], Bridge::FILE_DIFFS => []];
$oldFileContent = $file->getFileContent();
$fileContent = $file->getFileContent();
foreach ($this->latteRectors as $latteRector) {
$fileContent = $latteRector->changeContent($fileContent);
}
$file->changeFileContent($fileContent);
if ($oldFileContent === $fileContent) {
return $systemErrorsAndFileDiffs;
}
$fileDiff = $this->fileDiffFactory->createFileDiff($file, $oldFileContent, $fileContent);
$systemErrorsAndFileDiffs[Bridge::FILE_DIFFS][] = $fileDiff;
return $systemErrorsAndFileDiffs;
}
public function supports(File $file, Configuration $configuration) : bool
{
$fileInfo = $file->getSmartFileInfo();
return $fileInfo->hasSuffixes($this->getSupportedFileExtensions());
}
/**
* @return string[]
*/
public function getSupportedFileExtensions() : array
{
return ['latte'];
}
}