Skip to content
Open
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
2 changes: 2 additions & 0 deletions e2e/applied-no-diffs-format-json/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
$rectorConfig->rules([
RemoveAlwaysElseRector::class,
]);

$rectorConfig->importNames();
};
14 changes: 14 additions & 0 deletions e2e/applied-no-diffs-format-json/src/AliasedSameShortName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App;

use Vendor\Config\Repository;
use Vendor\Contracts\Repository as RepositoryContract;

final class AliasedSameShortName
{
public function run(RepositoryContract $repositoryContract): Repository
{
return new Repository();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App;

use ArrayIterator;

$iterator = new /** @extends \ArrayIterator<string, mixed> */ class([]) extends ArrayIterator {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$flags = FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::UNIX_PATHS;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$first = new RuntimeException();
$second = new RuntimeException();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function run(RuntimeException $first): RuntimeException
{
return $first;
}
4 changes: 3 additions & 1 deletion src/ChangesReporting/Output/Factory/JsonOutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public static function create(
}

// for Rector CI
$errorsJson['changed_files'][] = $filePath;
if ($fileDiff->hasChanged()) {
$errorsJson['changed_files'][] = $filePath;
}
}

$systemErrors = $processResult->getSystemErrors();
Expand Down
8 changes: 7 additions & 1 deletion src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function createFileDiffWithLineChanges(
$consoleDiff = $shouldShowDiffs ? $this->colorConsoleDiffFormatter->format($diff) : '';

// always keep the most recent diff
return new FileDiff($relativeFilePath, $diff, $consoleDiff, $rectorsWithLineChanges);
return new FileDiff(
$relativeFilePath,
$diff,
$consoleDiff,
$rectorsWithLineChanges,
$file->hasChanged()
);
}
}
2 changes: 2 additions & 0 deletions src/Parallel/ValueObject/BridgeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ final class BridgeItem
public const string RECTOR_CLASS = 'rector_class';

public const string RECTORS_WITH_LINE_CHANGES = 'rectors_with_line_changes';

public const string HAS_CHANGED = 'has_changed';
}
12 changes: 10 additions & 2 deletions src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct(
private string $relativeFilePath,
private string $diff,
private string $diffConsoleFormatted,
private array $rectorsWithLineChanges = []
private array $rectorsWithLineChanges = [],
private bool $hasChanged = true
) {
Assert::allIsInstanceOf($rectorsWithLineChanges, RectorWithLineChange::class);
}
Expand All @@ -44,6 +45,11 @@ public function getDiff(): string
return $this->diff;
}

public function hasChanged(): bool
{
return $this->hasChanged;
}

public function getDiffConsoleFormatted(): string
{
return $this->diffConsoleFormatted;
Expand Down Expand Up @@ -128,7 +134,7 @@ public function getLastLineNumber(): ?int
}

/**
* @return array{relative_file_path: string, diff: string, diff_console_formatted: string, rectors_with_line_changes: RectorWithLineChange[]}
* @return array{relative_file_path: string, diff: string, diff_console_formatted: string, rectors_with_line_changes: RectorWithLineChange[], has_changed: bool}
*/
public function jsonSerialize(): array
{
Expand All @@ -137,6 +143,7 @@ public function jsonSerialize(): array
BridgeItem::DIFF => $this->diff,
BridgeItem::DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted,
BridgeItem::RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges,
BridgeItem::HAS_CHANGED => $this->hasChanged,
];
}

Expand All @@ -156,6 +163,7 @@ public static function decode(array $json): self
$json[BridgeItem::DIFF],
$json[BridgeItem::DIFF_CONSOLE_FORMATTED],
$rectorWithLineChanges,
$json[BridgeItem::HAS_CHANGED],
);
}
}
35 changes: 35 additions & 0 deletions tests/ChangesReporting/Output/Factory/JsonOutputFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,39 @@ public function testReportShouldAttributeEachRectorToItsLine(): void
$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/with_diffs.json');
$this->assertSame($expectedOutput, $actualOutput);
}

public function testReportShouldSkipUnchangedFileWithNoDiffs(): void
{
$actualOutput = JsonOutputFactory::create(
new ProcessResult(
[new SystemError('Some error message', 'some/file.php', 1)],
[
new FileDiff(
'some/file.php',
"--- Original\n+++ New\n@@ -38,5 +39,6 @@\nreturn true;\n}\n",
'diff console formatted',
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
),
new FileDiff(
'some/file_foo.php',
'',
'',
[new RectorWithLineChange(StrStartsWithRector::class, 38)]
),
new FileDiff(
'some/file_unchanged.php',
'',
'',
[new RectorWithLineChange(StrStartsWithRector::class, 5)],
hasChanged: false
),
],
2
),
new Configuration(showDiffs: false)
);

$expectedOutput = (string) file_get_contents(__DIR__ . '/../Fixtures/without_diffs.json');
$this->assertSame($expectedOutput, $actualOutput);
}
}
9 changes: 9 additions & 0 deletions tests/ValueObject/Reporting/FileDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@

final class FileDiffTest extends TestCase
{
public function testDecodeShouldKeepUnchangedState(): void
{
$fileDiff = new FileDiff('some/file.php', '', '', [], hasChanged: false);

$decodedFileDiff = FileDiff::decode($fileDiff->jsonSerialize());

$this->assertFalse($decodedFileDiff->hasChanged());
}

public function testGetFirstLineNumberShouldReturnFirstLineNumberRegardingHunk(): void
{
$fileDiff = new FileDiff(
Expand Down
Loading