Skip to content

Commit

Permalink
[Parallel] Add SerializableInterface to File, RectorError and FileDif…
Browse files Browse the repository at this point in the history
…f value objects (#1178)
  • Loading branch information
TomasVotruba committed Nov 7, 2021
1 parent cb54cf0 commit 9614997
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ private function createFileDiff(): FileDiff
{
// This is by intention to test the array_unique functionality
$rectorWithLineChanges = [];
$rectorWithLineChanges[] = new RectorWithLineChange(new RectorWithChangelog(), 1);
$rectorWithLineChanges[] = new RectorWithLineChange(new RectorWithChangelog(), 1);
$rectorWithLineChanges[] = new RectorWithLineChange(new RectorWithOutChangelog(), 1);
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithChangelog::class, 1);
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithChangelog::class, 1);
$rectorWithLineChanges[] = new RectorWithLineChange(RectorWithOutChangelog::class, 1);

return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithLineChanges);
$smartFileInfo = new SmartFileInfo(__FILE__);

return new FileDiff($smartFileInfo->getRelativeFilePathFromCwd(), 'foo', 'foo', $rectorWithLineChanges);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function notifyNodeFileInfo(Node $node): void
return;
}

$rectorWithLineChange = new RectorWithLineChange($currentRector, $node->getLine());
$rectorWithLineChange = new RectorWithLineChange($currentRector::class, $node->getLine());
$file->addRectorClassWithLine($rectorWithLineChange);
}
}
49 changes: 46 additions & 3 deletions packages/ChangesReporting/ValueObject/RectorWithLineChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,68 @@
namespace Rector\ChangesReporting\ValueObject;

use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Parallel\Contract\SerializableInterface;

final class RectorWithLineChange
final class RectorWithLineChange implements SerializableInterface
{
/**
* @var string
*/
private const KEY_RECTOR_CLASS = 'rector_class';

/**
* @var string
*/
private const KEY_LINE = 'line';

/**
* @var class-string<RectorInterface>
*/
private string $rectorClass;

/**
* @param class-string<RectorInterface>|RectorInterface $rectorClass
*/
public function __construct(
private RectorInterface $rector,
string|RectorInterface $rectorClass,
private int $line
) {
if ($rectorClass instanceof RectorInterface) {
$rectorClass = $rectorClass::class;
}

$this->rectorClass = $rectorClass;
}

/**
* @return class-string<RectorInterface>
*/
public function getRectorClass(): string
{
return $this->rector::class;
return $this->rectorClass;
}

public function getLine(): int
{
return $this->line;
}

/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): SerializableInterface
{
return new self($json[self::KEY_RECTOR_CLASS], $json[self::KEY_LINE]);
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
self::KEY_RECTOR_CLASS => $this->rectorClass,
self::KEY_LINE => $this->line,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function createAutoloadError(
SmartFileInfo $smartFileInfo
): RectorError {
$message = $this->exceptionCorrector->getAutoloadExceptionMessageAndAddLocation($analysedCodeException);
return new RectorError($message, $smartFileInfo);
return new RectorError($message, $smartFileInfo->getRelativeFilePathFromCwd());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function createFileDiff(File $file, string $oldContent, string $newConten
{
// always keep the most recent diff
return new FileDiff(
$file->getSmartFileInfo(),
$file->getRelativeFilePath(),
$this->defaultDiffer->diff($oldContent, $newContent),
$this->consoleDiffer->diff($oldContent, $newContent),
$file->getRectorWithLineChanges()
Expand Down
15 changes: 15 additions & 0 deletions packages/Parallel/Contract/SerializableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Parallel\Contract;

use JsonSerializable;

interface SerializableInterface extends JsonSerializable
{
/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): self;
}
6 changes: 5 additions & 1 deletion src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ private function tryCatchWrapper(File $file, callable $callback, ApplicationPhas
throw $throwable;
}

$rectorError = new RectorError($throwable->getMessage(), $file->getSmartFileInfo(), $throwable->getLine());
$rectorError = new RectorError(
$throwable->getMessage(),
$file->getRelativeFilePath(),
$throwable->getLine()
);
$file->addRectorError($rectorError);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ final public function enterNode(Node $node)

// changed!
if ($this->changedNodeAnalyzer->hasNodeChanged($originalNode, $node)) {
$rectorWithLineChange = new RectorWithLineChange($this, $originalNode->getLine());
$rectorWithLineChange = new RectorWithLineChange($this::class, $originalNode->getLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);

// update parents relations - must run before connectParentNodes()
Expand Down
5 changes: 5 additions & 0 deletions src/ValueObject/Application/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,9 @@ public function getErrors(): array
{
return $this->rectorErrors;
}

public function getRelativeFilePath(): string
{
return $this->smartFileInfo->getRelativeFilePathFromCwd();
}
}
41 changes: 32 additions & 9 deletions src/ValueObject/Application/RectorError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@

namespace Rector\Core\ValueObject\Application;

use Symplify\SmartFileSystem\SmartFileInfo;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Parallel\Contract\SerializableInterface;

final class RectorError
final class RectorError implements SerializableInterface
{
/**
* @param class-string<RectorInterface>|null $rectorClass
*/
public function __construct(
private string $message,
private SmartFileInfo $fileInfo,
private string $relativeFilePath,
private ?int $line = null,
private ?string $rectorClass = null
) {
}

public function getRelativeFilePath(): string
{
return $this->fileInfo->getRelativeFilePathFromCwd();
}

public function getFileInfo(): SmartFileInfo
{
return $this->fileInfo;
return $this->relativeFilePath;
}

public function getMessage(): string
Expand All @@ -36,8 +35,32 @@ public function getLine(): ?int
return $this->line;
}

/**
* @return class-string<RectorInterface>|null
*/
public function getRectorClass(): ?string
{
return $this->rectorClass;
}

/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): SerializableInterface
{
return new self($json['message'], $json['relative_file_path'], $json['line'], $json['rector_class'],);
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'message' => $this->message,
'relative_file_path' => $this->relativeFilePath,
'line' => $this->line,
'rector_class' => $this->rectorClass,
];
}
}
2 changes: 1 addition & 1 deletion src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getChangedFileInfos(): array
{
$fileInfos = [];
foreach ($this->fileDiffs as $fileDiff) {
$fileInfos[] = $fileDiff->getFileInfo();
$fileInfos[] = new SmartFileInfo($fileDiff->getRelativeFilePath());
}

return array_unique($fileInfos);
Expand Down
67 changes: 54 additions & 13 deletions src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\SmartFileSystem\SmartFileInfo;
use Rector\Parallel\Contract\SerializableInterface;

final class FileDiff
final class FileDiff implements SerializableInterface
{
/**
* @var string
Expand All @@ -23,13 +23,33 @@ final class FileDiff
private const FIRST_LINE_KEY = 'first_line';

/**
* @param RectorWithLineChange[] $rectorWithLineChanges
* @var string
*/
private const KEY_RELATIVE_FILE_PATH = 'relative_file_path';

/**
* @var string
*/
private const KEY_DIFF = 'diff';

/**
* @var string
*/
private const KEY_DIFF_CONSOLE_FORMATTED = 'diff_console_formatted';

/**
* @var string
*/
private const KEY_RECTORS_WITH_LINE_CHANGES = 'rectors_with_line_changes';

/**
* @param RectorWithLineChange[] $rectorsWithLineChanges
*/
public function __construct(
private SmartFileInfo $smartFileInfo,
private string $relativeFilePath,
private string $diff,
private string $diffConsoleFormatted,
private array $rectorWithLineChanges = []
private array $rectorsWithLineChanges = []
) {
}

Expand All @@ -45,20 +65,15 @@ public function getDiffConsoleFormatted(): string

public function getRelativeFilePath(): string
{
return $this->smartFileInfo->getRelativeFilePath();
}

public function getFileInfo(): SmartFileInfo
{
return $this->smartFileInfo;
return $this->relativeFilePath;
}

/**
* @return RectorWithLineChange[]
*/
public function getRectorChanges(): array
{
return $this->rectorWithLineChanges;
return $this->rectorsWithLineChanges;
}

/**
Expand All @@ -67,7 +82,7 @@ public function getRectorChanges(): array
public function getRectorClasses(): array
{
$rectorClasses = [];
foreach ($this->rectorWithLineChanges as $rectorWithLineChange) {
foreach ($this->rectorsWithLineChanges as $rectorWithLineChange) {
$rectorClasses[] = $rectorWithLineChange->getRectorClass();
}

Expand All @@ -86,6 +101,32 @@ public function getFirstLineNumber(): ?int
return (int) $match[self::FIRST_LINE_KEY] - 1;
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
self::KEY_RELATIVE_FILE_PATH => $this->relativeFilePath,
self::KEY_DIFF => $this->diff,
self::KEY_DIFF_CONSOLE_FORMATTED => $this->diffConsoleFormatted,
self::KEY_RECTORS_WITH_LINE_CHANGES => $this->rectorsWithLineChanges,
];
}

/**
* @param array<string, mixed> $json
*/
public static function decode(array $json): SerializableInterface
{
return new self(
$json[self::KEY_RELATIVE_FILE_PATH],
$json[self::KEY_DIFF],
$json[self::KEY_DIFF_CONSOLE_FORMATTED],
$json[self::KEY_RECTORS_WITH_LINE_CHANGES],
);
}

/**
* @template TType as object
* @param array<class-string<TType>> $rectorClasses
Expand Down

0 comments on commit 9614997

Please sign in to comment.