2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-03-19 00:07:07 +00:00
|
|
|
namespace Rector\ChangesReporting\Output;
|
2019-05-28 14:18:05 +00:00
|
|
|
|
2021-06-21 19:24:28 +00:00
|
|
|
use RectorPrefix20210621\Nette\Utils\Json;
|
2021-04-10 16:22:25 +00:00
|
|
|
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
|
2020-03-19 00:07:07 +00:00
|
|
|
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Configuration\Configuration;
|
2021-04-12 10:34:04 +00:00
|
|
|
use Rector\Core\ValueObject\ProcessResult;
|
2021-06-21 19:24:28 +00:00
|
|
|
use RectorPrefix20210621\Symplify\SmartFileSystem\SmartFileSystem;
|
2021-05-10 22:23:08 +00:00
|
|
|
final class JsonOutputFormatter implements \Rector\ChangesReporting\Contract\Output\OutputFormatterInterface
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const NAME = 'json';
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\Configuration\Configuration
|
2019-05-28 14:47:55 +00:00
|
|
|
*/
|
|
|
|
private $configuration;
|
2020-07-24 12:21:34 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Symplify\SmartFileSystem\SmartFileSystem
|
2020-07-24 12:21:34 +00:00
|
|
|
*/
|
|
|
|
private $smartFileSystem;
|
2021-04-10 14:54:24 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\ChangesReporting\Annotation\RectorsChangelogResolver
|
2021-04-10 14:54:24 +00:00
|
|
|
*/
|
2021-04-10 16:22:25 +00:00
|
|
|
private $rectorsChangelogResolver;
|
2021-06-21 19:24:28 +00:00
|
|
|
public function __construct(\Rector\Core\Configuration\Configuration $configuration, \RectorPrefix20210621\Symplify\SmartFileSystem\SmartFileSystem $smartFileSystem, \Rector\ChangesReporting\Annotation\RectorsChangelogResolver $rectorsChangelogResolver)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
$this->configuration = $configuration;
|
2020-07-24 12:21:34 +00:00
|
|
|
$this->smartFileSystem = $smartFileSystem;
|
2021-04-10 16:22:25 +00:00
|
|
|
$this->rectorsChangelogResolver = $rectorsChangelogResolver;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getName() : string
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2019-05-28 14:47:55 +00:00
|
|
|
return self::NAME;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function report(\Rector\Core\ValueObject\ProcessResult $processResult) : void
|
2019-05-28 14:18:05 +00:00
|
|
|
{
|
2021-05-14 21:57:41 +00:00
|
|
|
$errorsArray = ['meta' => ['config' => $this->configuration->getMainConfigFilePath()], 'totals' => ['changed_files' => \count($processResult->getFileDiffs()), 'removed_and_added_files_count' => $processResult->getRemovedAndAddedFilesCount(), 'removed_node_count' => $processResult->getRemovedNodeCount()]];
|
2021-04-12 10:34:04 +00:00
|
|
|
$fileDiffs = $processResult->getFileDiffs();
|
2021-05-09 20:15:43 +00:00
|
|
|
\ksort($fileDiffs);
|
2019-05-28 14:18:05 +00:00
|
|
|
foreach ($fileDiffs as $fileDiff) {
|
2019-05-28 20:38:21 +00:00
|
|
|
$relativeFilePath = $fileDiff->getRelativeFilePath();
|
2021-04-10 16:22:25 +00:00
|
|
|
$appliedRectorsWithChangelog = $this->rectorsChangelogResolver->resolve($fileDiff->getRectorClasses());
|
2021-05-09 20:15:43 +00:00
|
|
|
$errorsArray['file_diffs'][] = ['file' => $relativeFilePath, 'diff' => $fileDiff->getDiff(), 'applied_rectors' => $fileDiff->getRectorClasses(), 'applied_rectors_with_changelog' => $appliedRectorsWithChangelog];
|
2019-05-28 14:49:31 +00:00
|
|
|
// for Rector CI
|
2019-05-28 16:28:09 +00:00
|
|
|
$errorsArray['changed_files'][] = $relativeFilePath;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2021-04-12 10:34:04 +00:00
|
|
|
$errors = $processResult->getErrors();
|
2021-05-09 20:15:43 +00:00
|
|
|
$errorsArray['totals']['errors'] = \count($errors);
|
2020-12-12 13:53:10 +00:00
|
|
|
$errorsData = $this->createErrorsData($errors);
|
|
|
|
if ($errorsData !== []) {
|
|
|
|
$errorsArray['errors'] = $errorsData;
|
|
|
|
}
|
2021-06-21 19:24:28 +00:00
|
|
|
$json = \RectorPrefix20210621\Nette\Utils\Json::encode($errorsArray, \RectorPrefix20210621\Nette\Utils\Json::PRETTY);
|
2020-12-12 13:53:10 +00:00
|
|
|
$outputFile = $this->configuration->getOutputFile();
|
|
|
|
if ($outputFile !== null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$this->smartFileSystem->dumpFile($outputFile, $json . \PHP_EOL);
|
2020-12-12 13:53:10 +00:00
|
|
|
} else {
|
2021-05-09 20:15:43 +00:00
|
|
|
echo $json . \PHP_EOL;
|
2020-12-12 13:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed[] $errors
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function createErrorsData(array $errors) : array
|
2020-12-12 13:53:10 +00:00
|
|
|
{
|
|
|
|
$errorsData = [];
|
2019-05-28 14:18:05 +00:00
|
|
|
foreach ($errors as $error) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$errorData = ['message' => $error->getMessage(), 'file' => $error->getRelativeFilePath()];
|
2019-05-28 14:18:05 +00:00
|
|
|
if ($error->getRectorClass()) {
|
|
|
|
$errorData['caused_by'] = $error->getRectorClass();
|
|
|
|
}
|
|
|
|
if ($error->getLine() !== null) {
|
|
|
|
$errorData['line'] = $error->getLine();
|
|
|
|
}
|
2020-12-12 13:53:10 +00:00
|
|
|
$errorsData[] = $errorData;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
2020-12-12 13:53:10 +00:00
|
|
|
return $errorsData;
|
2019-05-28 14:18:05 +00:00
|
|
|
}
|
|
|
|
}
|