Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/ChangesReporting/Output/Factory/JsonOutputFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Rector\ChangesReporting\Output\Factory;

use Nette\Utils\Json;
use Rector\Parallel\ValueObject\Bridge;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;

/**
* @see \Rector\Tests\ChangesReporting\Output\Factory\JsonOutputFactoryTest
*/
final class JsonOutputFactory
{
public static function create(ProcessResult $processResult, Configuration $configuration): string
{
$errorsJson = [
'totals' => [
'changed_files' => $processResult->getTotalChanged(),
],
];

// We need onlyWithChanges: false to include all file diffs
$fileDiffs = $processResult->getFileDiffs(onlyWithChanges: false);
ksort($fileDiffs);
foreach ($fileDiffs as $fileDiff) {
$filePath = $configuration->isReportingWithRealPath()
? ($fileDiff->getAbsoluteFilePath() ?? '')
: $fileDiff->getRelativeFilePath()
;

if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
$errorsJson[Bridge::FILE_DIFFS][] = [
'file' => $filePath,
'diff' => $fileDiff->getDiff(),
'applied_rectors' => $fileDiff->getRectorClasses(),
];
}

// for Rector CI
$errorsJson['changed_files'][] = $filePath;
}

$systemErrors = $processResult->getSystemErrors();
$errorsJson['totals']['errors'] = count($systemErrors);

$errorsData = self::createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
if ($errorsData !== []) {
$errorsJson['errors'] = $errorsData;
}

return Json::encode($errorsJson, pretty: true);
}

/**
* @param SystemError[] $errors
* @return mixed[]
*/
private static function createErrorsData(array $errors, bool $absoluteFilePath): array
{
$errorsData = [];

foreach ($errors as $error) {
$errorDataJson = [
'message' => $error->getMessage(),
'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath(),
];

if ($error->getRectorClass() !== null) {
$errorDataJson['caused_by'] = $error->getRectorClass();
}

if ($error->getLine() !== null) {
$errorDataJson['line'] = $error->getLine();
}

$errorsData[] = $errorDataJson;
}

return $errorsData;
}
}
67 changes: 2 additions & 65 deletions src/ChangesReporting/Output/JsonOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Rector\ChangesReporting\Output;

use Nette\Utils\Json;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Parallel\ValueObject\Bridge;
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;

final readonly class JsonOutputFormatter implements OutputFormatterInterface
Expand All @@ -25,67 +23,6 @@ public function getName(): string

public function report(ProcessResult $processResult, Configuration $configuration): void
{
$errorsJson = [
'totals' => [
'changed_files' => $processResult->getTotalChanged(),
],
];

$fileDiffs = $processResult->getFileDiffs();
ksort($fileDiffs);
foreach ($fileDiffs as $fileDiff) {
$filePath = $configuration->isReportingWithRealPath()
? ($fileDiff->getAbsoluteFilePath() ?? '')
: $fileDiff->getRelativeFilePath()
;

$errorsJson[Bridge::FILE_DIFFS][] = [
'file' => $filePath,
'diff' => $fileDiff->getDiff(),
'applied_rectors' => $fileDiff->getRectorClasses(),
];

// for Rector CI
$errorsJson['changed_files'][] = $filePath;
}

$systemErrors = $processResult->getSystemErrors();
$errorsJson['totals']['errors'] = count($systemErrors);

$errorsData = $this->createErrorsData($systemErrors, $configuration->isReportingWithRealPath());
if ($errorsData !== []) {
$errorsJson['errors'] = $errorsData;
}

$json = Json::encode($errorsJson, pretty: true);
echo $json . PHP_EOL;
}

/**
* @param SystemError[] $errors
* @return mixed[]
*/
private function createErrorsData(array $errors, bool $absoluteFilePath): array
{
$errorsData = [];

foreach ($errors as $error) {
$errorDataJson = [
'message' => $error->getMessage(),
'file' => $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath(),
];

if ($error->getRectorClass() !== null) {
$errorDataJson['caused_by'] = $error->getRectorClass();
}

if ($error->getLine() !== null) {
$errorDataJson['line'] = $error->getLine();
}

$errorsData[] = $errorDataJson;
}

return $errorsData;
echo JsonOutputFactory::create($processResult, $configuration) . PHP_EOL;
}
}
47 changes: 47 additions & 0 deletions tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\ChangesReporting\Output\Factory;

use PHPUnit\Framework\TestCase;
use Rector\ChangesReporting\Output\Factory\JsonOutputFactory;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
use Rector\ValueObject\Reporting\FileDiff;

final class JsonOutputFactoryTest extends TestCase
{
public function testReportShouldShowNumberOfChangesWithNoDiffs(): void
{
$actualOutput = JsonOutputFactory::create(
new ProcessResult(
[new SystemError('Some error message', 'some/file.php', 1)],
[
new FileDiff(
'some/file.php',
'--- Original' . PHP_EOL . '+++ New' . PHP_EOL .
'@@ -38,5 +39,6 @@' . PHP_EOL .
'return true;' . PHP_EOL . '}' . PHP_EOL,
'diff console formatted',
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
),
new FileDiff(
'some/file_foo.php',
'',
'',
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
),
],
2
),
new Configuration(showDiffs: false)
);

$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/without_diffs.json');
$this->assertSame($expectedOutput, $actualOutput);
}
}
17 changes: 17 additions & 0 deletions tests/ChangesReporting/Output/Fixtures/without_diffs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"totals": {
"changed_files": 2,
"errors": 1
},
"changed_files": [
"some/file.php",
"some/file_foo.php"
],
"errors": [
{
"message": "Some error message",
"file": "some/file.php",
"line": 1
}
]
}